FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Where is the problem in this simple code?





imagefree
Code:
<?php
//find a prime number

for( $prime=2; $prime < 500;$prime++)
{
for( $count=2; $count<$prime/2 ; $count++)
{
if( $prime % $count == 0 )
break;
}
if( $prime % $count == 0)
continue;
else if( $prime == ($count-1)*2)
continue; //This is because if this condition fulfills, it means that the inner loop is terminated because of Text Expression failed. So, the number cant be prime in this case and program is sent back to start of loop.
printf("$prime is prime #");
}

?>


please mention all the problems in it (because its not working properly).
Thanks
Star Wars Fanatic
It works perfectly fine for me. The only thing I would recommend is always putting the curly braces in the loops and if statements, even if there is only one line of code:



Code:
<?php
//find a prime number

for( $prime = 2; $prime < 500; $prime ++)   {
   for( $count=2; $count < $prime/2; $count ++)   {
      if( $prime % $count == 0 )   {
         break;
      }
   }
   if( $prime % $count == 0)   {
      continue;
   }
   else if( $prime == ( $count - 1 ) * 2 )   {
      continue; //This is because if this condition fulfills, it means that the inner loop is terminated because of Text Expression failed. So, the number cant be prime in this case and program is sent back to start of loop.
   }
   printf("$prime is prime #<br />\n");
}

?>


That's all I did above, added curly braces, changed the print a little to make it prettier, and added some tabs/spaces here and there.
If it still doesn't work, it might be a problem in the rest of your code, try running this just by itself.
desertwind
Yep. The code seems to be working perfectly fine for me also. Check for the other part of code.
Related topics
Header("Location: PROBLEM");
Mail with PHP (Simple Tutorial)
Simple and Detailed Mail Script
Upload file
Problem index page
MySQL problem :'(
Simple code to make your web page !
C++ code problem
[Visual Basic] Divide X equation
Image posting problem
How to build simple PHP upload form
25 year old BSD bug fixed :p
Might Sound Crazy But Ultra simple code For Pinball game
Problem with code that handles arrays and database updates
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.