Help with Prime Number code

I thought it would be fun to take a programming. I have no prior experience with this kind of stuff and it turns out I’m really bad at it.

I was given a project were I had to make a program that can list all of the prime numbers between two and any random number.

this is all I’ve managed to do.

Page 1
[php]<?php

$MAX = 0; // HIGHEST VALUE COMES FROM THE SCREEN //
$X = 3; // COUNTER //
$FLAG = TRUE; //DETEMINES WHETHER PRIME OR NOT, TRUE=PRIME//

$R = 0; // REMAINDER //
$I = 2; // MOD //
//PROMPT MAX
//GET MAX

Enter a Number:

?>[/php]

Page 2
[php]<?php

$MAX = 0; // HIGHEST VALUE COMES FROM THE SCREEN //
$X = 3; // COUNTER //
$FLAG = TRUE; //DETEMINES WHETHER PRIME OR NOT, TRUE=PRIME//
$R = 0; // REMAINDER //
$I = 2;

ECHO “2 is a prime number”;
IF($MAX==“1”) //THEN
$FLAG=FALSE;
//ENDIF;

WHILE($X<=$MAX) // EVAL EVERY NO FROM X TO MAX //
$I=2;

WHILE($I<$X)
$R=$X%$I;
IF($R==0) //THEN
$I=$X;
$FLAG=FALSE; // AT THIS POINT NOT PRIME //
//ENDIF

($I++);
//ENDWHILE // I < X //
If($flag=TRUE);
ECHO “$X IS PRIME”;

$X++ ;
//ENDWHILE // X < MAX //

?>[/php]

When I run it it says “Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\xampp\hw3p2.php on line 26”. Line 26 is where it says WHILE($I<$X).

I know this is some really sloppy and amateur, but any help I can get to make it work would be greatly appreciated.

Couple of different ways of doing that. Some research found a few good scripts already done up.
http://www.talkincode.com/php-function-to-detect-a-prime-number-396.html
http://www.geekpedia.com/code14_Check-for-prime-numbers.html

and what i searched for
http://search.yahoo.com/search;_ylt=AuOa5pjcDoQGzxfgC9uLOwubvZx4?p=php+prime+numbers&toggle=1&cop=mss&ei=UTF-8&fr=yfp-t-701

for your script help:
page 1 - you need to either echo the html part, or move the ?> to before the html start point. You have the same variable info on page 2, why is it on both pages? Your value is wrong for the number input. This is what it should look like

[code]

Enter a Number:
?>[/code]

on page 2, at no point does $flag ever equal true. this statement:
If($flag=TRUE);
ECHO “$X IS PRIME”;

will never execute because the condition isn’t going to be met.

You have ($++) in a single while loop, doing nothing by counting . if $i is supposed to start at 2 and then increment, then you need to start $i before the loop, then use $i++ to increment it by 1 or $i + 2 to count by 2’s, and so on. and why is everything in caps? Not really sure why you have the user enter any input, as it’ll never be used. I presume $max is supposed to be the user’s number? that needs to be changed to $_POST[‘num’] (or whatever you want to call that text box).

One further note, at this early point in your learning, you should learn about HTML-FORMS a bit first.
Basically a form is HTML. It POSTS out to your PHP code which should display your results created.

So, you do not need the PHP code in the first part of your project. Then, follow Richei’s help…

Good luck, post back here with new questions or your finished project for others to read…

I don’t know why there is a max, I don’t know why it is in caps, I just have no idea what I’m doing. Thanks for the help, but I think I’m a little more lost now then I was before.

I was just given this…

Prime number BEGIN MAX = 0 // HIGHEST VALUE COMES FROM THE SCREEN // X = 3 // COUNTER // FLAG = TRUE //DETEMINES WHETHER PRIME OR NOT, TRUE=PRIME//

$R = 0 // REMAINDER //
$I = 2 // MOD //
PROMPT MAX
GET MAX
IF (MAX==1) THEN
FLAG = FALSE
END IF

WHILE X <= MAX // EVAL EVERY NO FROM X TO MAX //
I=2

WHILE I < X
$R = $X % $I
IF (R==0) THEN
FLAG = FALSE // AT THIS POINT NOT PRIME //
I = X
ENDIF

I++
END WHILE // I < X //
IF (FLAG = TRUE) THEN
ECHO X IS PRIME

X++
END WHILE // X < MAX //
END

$flag=TRUE;
$R=($X%$I);
IF($R==“0”)
$I=$X;
$FLAG=FALSE;

and I had to make it into php.

I didn’t mean for that to sound unappreciative, I’m just not getting it.

I’ve looked at every example I could find online and none of it looks even close to what I’ve worked with so far.

That’s called pseudo code. some coders write out their code in long hand to see how it all flows. Its not actually usable code, which is why yours is timing out on you. What you copied wasn’t ment for user input, it probably just an example. Have a look at those examples i posted and go from there.

Sponsor our Newsletter | Privacy Policy | Terms of Service