This is a discussion on Sieve of Eratosthenes within the PHP Language forums, part of the PHP Programming Forums category; hi there! i have to program the sieve of eratosthenes in php as a homework. after i had created an ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
hi there!
i have to program the sieve of eratosthenes in php as a homework. after i had created an html file where the maximum is set i wrote a php script which doesn't work properly - actually it doesn't work at all. and here it is: 1 <?php 2 $m = $_POST['max']; 3 4 for ($i=2; $i<=$m; $i=$i+1) 5 {$prime='true'; 6 for ($n=2; $n<=sqrt($m); n=n+1) 7 { 8 if (i/n=0) 9 {$prime='false';} 10 if ($prime='true') 11 {echo $i;} 12 } 13 } 14 15 ?> i constantly get the following error: Parse error: parse error, unexpected '=', expecting ')' in C:\Programme\winampp\xampp\htdocs\primzahl.php on line 6 but i cannot find a mistake in line 6. can you? i hope so. but maybe the mistake lies somewhere else too. i don't know. i hope someone can help. thanx. |
|
|||
|
I noticed that Message-ID:
<c1769b91.0412290643.296fbccd@posting.google.com > from MSiegel contained the following: >i have to program the sieve of eratosthenes in php as a homework. >after i had created an html file where the maximum is set i wrote a >php script which doesn't work properly - actually it doesn't work at >all. But the sieve of Eratosthenes doesn't work the way you coded it. Your assignment was probably intended to get you working with arrays. A quick google found the following pseudocode PSEUDOPROGRAM: Set up an array of integers z[N]: for i from 0 to N, set z[i] = i. for i from 2 to N, mark all multiples of i by setting them to zero (or in php, you could unset the value in the array) for i from 2 to N, print all unmarked multiples. -- Geoff Berrow (put thecat out to email) It's only Usenet, no one dies. My opinions, not the committee's, mine. Simple RFDs http://www.ckdog.co.uk/rfdmaker/ |
|
|||
|
I noticed that Message-ID: <gdh5t01g3bhokb2qjf4bhse1feh4vnptsi@4ax.com>
from Geoff Berrow contained the following: >>i have to program the sieve of eratosthenes in php as a homework. >>after i had created an html file where the maximum is set i wrote a >>php script which doesn't work properly - actually it doesn't work at >>all. > >But the sieve of Eratosthenes doesn't work the way you coded it. http://ccins.camosun.bc.ca/~jbritton/jberatosthenes.htm -- Geoff Berrow (put thecat out to email) It's only Usenet, no one dies. My opinions, not the committee's, mine. Simple RFDs http://www.ckdog.co.uk/rfdmaker/ |
|
|||
|
On Wed, 29 Dec 2004 06:43:58 -0800, MSiegel wrote:
> Path: > nwrddc03.gnilink.net!cyclone2.gnilink.net!cyclone1 .gnilink.net!gnilink.net! > news.glorb.com!postnews.google.com!not-for-mail > From: msiegel@gmx.de (MSiegel) > Newsgroups: comp.lang.php > Subject: Sieve of Eratosthenes > Date: 29 Dec 2004 06:43:58 -0800 > Organization: http://groups.google.com > Lines: 34 > Message-ID: <c1769b91.0412290643.296fbccd@posting.google.com > > NNTP-Posting-Host: 145.254.77.150 > X-Trace: posting.google.com 1104331438 32691 127.0.0.1 (29 Dec 2004 14:43:58 > GMT) > X-Complaints-To: groups-abuse@google.com > NNTP-Posting-Date: Wed, 29 Dec 2004 14:43:58 +0000 (UTC) > Xref: cyclone1.gnilink.net comp.lang.php:74427 > X-Received-Date: Wed, 29 Dec 2004 09:43:59 EST (nwrddc03.gnilink.net) > MIME-Version: 1.0 > Content-Type: text/plain; charset=ISO-8859-1 > Content-Transfer-Encoding: 8bit > > > hi there! > > i have to program the sieve of eratosthenes in php as a homework. > after i had created an html file where the maximum is set i wrote a > php script which doesn't work properly - actually it doesn't work at > all. > > and here it is: > > 1 <?php > 2 $m = $_POST['max']; > 3 > 4 for ($i=2; $i<=$m; $i=$i+1) > 5 {$prime='true'; > 6 for ($n=2; $n<=sqrt($m); n=n+1) > 7 { > 8 if (i/n=0) > 9 {$prime='false';} > 10 if ($prime='true') > 11 {echo $i;} > 12 } > 13 } > 14 > 15 ?> > > i constantly get the following error: > > Parse error: parse error, unexpected '=', expecting ')' in > C:\Programme\winampp\xampp\htdocs\primzahl.php on line 6 > > but i cannot find a mistake in line 6. can you? i hope so. but maybe > the mistake lies somewhere else too. i don't know. > > i hope someone can help. thanx. In other news, line 8 will evaluate "true" only when $i is zero (never). Maybe you meant some other operator? I'm looking sort of in the direction of the middle-sized number keys... ;) |
|
|||
|
MSiegel wrote:
> hi there! > > i have to program the sieve of eratosthenes in php as a homework. > after i had created an html file where the maximum is set i wrote a > php script which doesn't work properly - actually it doesn't work at > all. > > and here it is: > > 1 <?php > 2 $m = $_POST['max']; > 3 > 4 for ($i=2; $i<=$m; $i=$i+1) > 5 {$prime='true'; > 6 for ($n=2; $n<=sqrt($m); n=n+1) > 7 { > 8 if (i/n=0) > 9 {$prime='false';} > 10 if ($prime='true') > 11 {echo $i;} > 12 } > 13 } > 14 > 15 ?> > > i constantly get the following error: You are constantly forgetting the $ infront of a variable name, which is required in PHP, unless it is a constant, and in that case, you can't assign to it. You might also want to have a look at the ++ operator. -- Tommy |
|
|||
|
"Steve" <schigh@comcast.net> wrote:
> Since this is homework, no one will just give you the answer, but > here's a few clues: > > Line 6: Look at the way you are declaring the variable 'n' And by "declaring" he doesn't mean the initial declaration ("$n = 2"), but the iterative step declaration ("n=n+2"). > Line 10: Something is wrong with your comparison statement > 'if($prime='true')' > Here's the list of comparison operators: http://www.php.net/manual/en/languag...comparison.php -Noah |
|
|||
|
Tommy Gildseth wrote:
> MSiegel wrote: > >> i have to program the sieve of eratosthenes in php as a homework. Well, since you're being so honest about it....other people have pointed out some of the *errors* in the script, but your programming style is abysmal. Have a read at http://pear.php.net/manual/en/standards.php for starters. Use readable variable names. >> 4 for ($i=2; $i<=$m; $i=$i+1) >> 5 {$prime='true'; >> 6 for ($n=2; $n<=sqrt($m); n=n+1) $m does not change throughout the life of the program but you are calculating it's square root $m^1.5 times! It should be calculated once, outside of both loops. Once you know it's not prime, why bother to continue the loop? >> 7 { >> 8 if (i/n=0) >> 9 {$prime='false';} >> 10 if ($prime='true') >> 11 {echo $i;} >> 12 } >> 13 } >> 14 >> 15 ?> There are also semantic errors in there too. HTH C. |
|
|||
|
"MSiegel" <msiegel@gmx.de> wrote in message
news:c1769b91.0412290643.296fbccd@posting.google.c om... > hi there! > > i have to program the sieve of eratosthenes in php as a homework. > after i had created an html file where the maximum is set i wrote a > php script which doesn't work properly - actually it doesn't work at > all. > > and here it is: > > 1 <?php > 2 $m = $_POST['max']; > 3 > 4 for ($i=2; $i<=$m; $i=$i+1) > 5 {$prime='true'; > 6 for ($n=2; $n<=sqrt($m); n=n+1) Along with everyone else's comments about your programming, try examining this little program: <?php $m = 100; for($i = 2; $i < 100; $i++) { echo "$i) " . sqrt($m) . "<br>\n"; } ?> You'll notice that the square root of $m does not change during the execution of the program, so calculating it every time you do a loop is: a) unessecary b) time consuming > 7 { > 8 if (i/n=0) The sieve of of eratosthenes is a question about multiplication and elimination, not division. If you have to divide something then you're doing it wrong. > 9 {$prime='false';} > 10 if ($prime='true') > 11 {echo $i;} > 12 } > 13 } > 14 > 15 ?> > > i constantly get the following error: > > Parse error: parse error, unexpected '=', expecting ')' in > C:\Programme\winampp\xampp\htdocs\primzahl.php on line 6 > > but i cannot find a mistake in line 6. can you? i hope so. but maybe > the mistake lies somewhere else too. i don't know. > > i hope someone can help. thanx. Pay particular attention to Geoff Berrow's advice and link. |
|
|||
|
CJ Llewellyn wrote:
<snip> > The sieve of of eratosthenes is a question about multiplication and > elimination, not division. If you have to divide something then you're doing > it wrong. <snip> After looking at the explanation of the algorithm here: http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes I'm wondering, How one goes about determining if a number is a multiple of another without dividing? NM |