Page: [1] [2] |
Dingbats
; Send PM
Posts: 1970
Threads: 50 Mood: Optative Money: £268.57 (D) (+ Friend)
|
I'm trying to create a PHP script that checks whether a number is a prime number or not. Just because I'm bored. But I can't get it to work properly. This is what I've written:
\n
for($i = $_GET["number"]; $i <= ($_GET["number"] / 2); $i++)
{
if(($_GET["number"] % $i) == 0)
{
$yes = true;
break;
}
}
}
?>
Prime number counter
Prime number counter
if(isset($_GET["number"]))
{
echo "Last number: ";
if($yes)
{
echo "Prime number!";
}
else
{
echo "Not a prime number.";
}
}
?>
Enter a number and press 'check'
to check if it is a prime number.
\n
I get no errors, but it's not working right. It says that all numbers above zero aren't prime numbers and that all numbers below or exact zero are prime numbers. What is wrong?
Edit: Yes, I have to write $_GET["number"] instead of $number, because of some weird setting somewhere.
|
08.03.04 15:06 Post #1 | Last edited: 08.03.04 15:08 (Dingbats - 2 times) |
[Softbrain Games] [Hide Sig (2)] [Profile] [Quote] |
thor
Statusless Send PM Posts:
Threads: Money: £0.00 (D) (+ Friend)
|
y'know when it says the form action is prime.php? shouldn't it be $PHP_SELF ?
|
08.03.04 16:06 Post #2 | [Hide Sig (0)] [Profile] [Quote] |
Zogger!
Looking For Status Send PM Posts: 3954
Threads: 62 Money: £93.82 (D) (+ Friend)
|
The last iteration of your for loop will always return yes as true. It needs to be less than, not thes than or equal to
And if this hasn't been posted already while I've been pondering over it, no it doesn't need to be $PHP_SELF, providing the page that script is on is called prime.php. It only really needs to be $PHP_SELF if he's changing the page name all the time.
edit: lol managed to post that 8 times in a row. Must be something wrong wit the double post checker.
________________
You know I'm a dancing machine |
08.03.04 16:34 Post #3 | Last edited: 08.03.04 16:39 (ZoGgEr! - 1 times) |
[Hide Sig (8)] [Profile] [Quote] |
Dingbats
; Send PM
Posts: 1970
Threads: 50 Mood: Optative Money: £268.57 (D) (+ Friend)
|
I'll try it. And yep, the page is called prime.php.
Edit: No, it still doesn't work. Anyone else?
|
08.03.04 17:39 Post #4 | Last edited: 08.03.04 17:46 (Dingbats - 1 times) |
[Softbrain Games] [Hide Sig (2)] [Profile] [Quote] |
Zogger!
Looking For Status Send PM Posts: 3954
Threads: 62 Money: £93.82 (D) (+ Friend)
|
oh it's divided by two... hmm...
________________
You know I'm a dancing machine |
08.03.04 17:56 Post #5 | [Hide Sig (8)] [Profile] [Quote] |
Dingbats
; Send PM
Posts: 1970
Threads: 50 Mood: Optative Money: £268.57 (D) (+ Friend)
|
Yep, it is. Since eg. 183/182 can't be working, get it?
|
08.03.04 17:58 Post #6 | [Softbrain Games] [Hide Sig (2)] [Profile] [Quote] |
CBWhiz
Looking For Status Send PM Posts: 1044
Threads: 130 Money: £1343.20 (D) (+ Friend)
|
\n
<?PHP\n
for($i = $_GET["number"]; $i <= ($_GET["number"] / 2); $i++)<br />
\n?>
\n
should be
\n
<?PHP\n
for($i = $_GET["number"]; $i >= ($_GET["number"] / 2); $i--)
\n?>
\n
corse thats untested and only what i see at first glance
|
08.03.04 22:58 Post #7 | [Hide Sig (3)] [Profile] [Quote] |
C1
Looking For Status Send PM Posts: 0
Threads: 0 Money: £0.18 (D) (+ Friend)
|
wtf is a prime # counter? And what's the point of it? Help in math class lol?
|
09.03.04 04:29 Post #8 | [Hide Sig (2)] [Profile] [Quote] |
The Pope
Decisive Send PM
Posts: 5183
Threads: 123 Mood: Refreshed Money: £201.52 (D) (+ Friend)
|
You know prime numbers right? Well this lets you type in a number and tells you if it is a prime number or not.
________________
Learn to look, look to learn. |
09.03.04 07:49 Post #9 | [Hide Sig (14)] [Profile] [Quote] |
Dingbats
; Send PM
Posts: 1970
Threads: 50 Mood: Optative Money: £268.57 (D) (+ Friend)
|
Thanks, Pope! Didn't think of that.
Edit: WTF!!! It didn't work. Anyone else, please?:?
|
09.03.04 18:32 Post #10 | Last edited: 09.03.04 18:34 (Dingbats - 1 times) |
[Softbrain Games] [Hide Sig (2)] [Profile] [Quote] |
C1
Looking For Status Send PM Posts: 0
Threads: 0 Money: £0.18 (D) (+ Friend)
|
Yes but what's the point pope. I kinda knew that part.
|
09.03.04 21:13 Post #11 | [Hide Sig (2)] [Profile] [Quote] |
The Pope
Decisive Send PM
Posts: 5183
Threads: 123 Mood: Refreshed Money: £201.52 (D) (+ Friend)
|
Dingbats explained he felt bored and so decided to code something interesting. There was no actual reason.
I wish ppl would read threads fully...
________________
Learn to look, look to learn. |
09.03.04 21:24 Post #12 | [Hide Sig (14)] [Profile] [Quote] |
C1
Looking For Status Send PM Posts: 0
Threads: 0 Money: £0.18 (D) (+ Friend)
|
Ah, i didnt see the text at the top of the code.
|
09.03.04 21:24 Post #13 | [Hide Sig (2)] [Profile] [Quote] |
Dingbats
; Send PM
Posts: 1970
Threads: 50 Mood: Optative Money: £268.57 (D) (+ Friend)
|
HELPHELPHELPHELPHEL PNOW 1!1!
No, seriously. I've tried to change \n for($i = $_GET["number"]; $i <= ($_GET["number"] / 2); $i++) \n to \n for($i = 0; $i <= ($_GET["number"] / 2); $i++) \n, but that didn't work either. Please, can anyone help me?
|
10.03.04 18:41 Post #14 | [Softbrain Games] [Hide Sig (2)] [Profile] [Quote] |
ReadMe
Absent Send PM Posts: 2820
Threads: 85 Money: £43.42 (D) (+ Friend)
|
so if i'm following this script right, it tries to divide the number by all numbers less than it up to half it's value to see if you get whole number division, seems about right. I'd do it like this:
\n
<?PHP\n
$num = $_GET['number'];
for ($i=ceil($num/2);$i>1;$i--) {
if (($num % $i) == 0) {
$factor = true;
break;
}
}
$msg = ($factor) ? 'Sorry, not prime' : 'Wahey! Prime number' ;
\n?>
\n
FOOTNOTE:
if you need to use $_GET, then $PHP_SELF wont work either.
needs to be $_SERVER['PHP_SELF'] since register globals is off. This is a much better way to work btw - avoid any variable conflicts between GET vars and SESSION vars for example - always use superglobals.
________________
Cant be arsed to remake my sig. |
10.03.04 22:17 Post #15 | Last edited: 10.03.04 22:22 (ReadMe - 1 times) |
[Hide Sig (7)] [Profile] [Quote] |
Page: [1] [2] |
Your Comments: