Hey can i have some help to build a .php file with the following questions

1.Generate a list of prime numbers from 1 to 500.

2.Count the number of prime numbers in the list.

3.Count How many are odd and even prime numbers in the list.

4.Work out the middle prime numbers in the list that seperate the list into two

5.Add all the odd prime number and all the even prime number in the list together.

Wish i can have an answer asap THANKS!!!

# Prime number from 1 to 500

Started by signum12, Oct 08 2006 10:06 AM

6 replies to this topic

### #1

Posted 08 October 2006 - 10:06 AM

### #2

Posted 08 October 2006 - 10:15 AM

sounds like homework to me........

by the way to help you along,

Prime Number -> a number that is only divisible by itself and 1................lets look at part 3 of your list.....after reading this definition the number of odd prime numbers will be the total number of all prime numbers in your list when you get it working.....there is no such thing as an even prime number because it will be at least divisible my itself, 1 and 2, maybe even more, thus not making it a prime number...teacher head off now...thanks for listening

by the way to help you along,

Prime Number -> a number that is only divisible by itself and 1................lets look at part 3 of your list.....after reading this definition the number of odd prime numbers will be the total number of all prime numbers in your list when you get it working.....there is no such thing as an even prime number because it will be at least divisible my itself, 1 and 2, maybe even more, thus not making it a prime number...teacher head off now...thanks for listening

you cannot affect the past but you can ruin a perfectly good present by worrying about the future

*********************************************************************

The <A HREF="http://www.php.net/d...php">MANUAL</A> is actually a useful resource

*********************************************************************

The <A HREF="http://www.php.net/d...php">MANUAL</A> is actually a useful resource

### #3

Posted 08 October 2006 - 09:56 PM

What about 2there is no such thing as an even prime number

### #4

Posted 08 October 2006 - 10:41 PM

paul and brendan are right; you will only get one even prime number, and that is 2.

as for your request, the definition of a prime number is not going to change soon. you can do this manually for the range of 1 to 500, but i suppose i'll give you something to work with.

some theory helps in explaining how this function will work. first, a number that is not prime will always be divisible by a prime - this is because all factorizable integers are a product of primes. next, 1, 2 and 3 are givens. while 1 cannot be used in the prime factorization check (since by definition, all integers are divisible by 1), 2 and 3 definitely can be used to get us started.

what this function does is it starts off with 1, 2 and 3 as its first primes. it then cycles from 4 onward and checks if the current number it's on is divisible by any of our current primes.

as for your request, the definition of a prime number is not going to change soon. you can do this manually for the range of 1 to 500, but i suppose i'll give you something to work with.

some theory helps in explaining how this function will work. first, a number that is not prime will always be divisible by a prime - this is because all factorizable integers are a product of primes. next, 1, 2 and 3 are givens. while 1 cannot be used in the prime factorization check (since by definition, all integers are divisible by 1), 2 and 3 definitely can be used to get us started.

what this function does is it starts off with 1, 2 and 3 as its first primes. it then cycles from 4 onward and checks if the current number it's on is divisible by any of our current primes.

**EDIT: will NOT continue this later as the others have taken over and provided some sieves.**### #5

Posted 08 October 2006 - 10:45 PM

There are all kinds of methods, I like the

me!

**sieve of atkin**, it great for long ranges, but for a short range a simple if() block works well!<? function get_primes ( $s, $e ) { $out = array (); for ( $i = $s; $i <= $e; $i++ ) { if ( ( $i == 1 ) OR ( $i == 2 ) OR ( $i == 3 ) OR ( $i == 5 ) ) { $out[] = $i; } else if ( ( $i % 2 != 0 ) AND ( $i % 3 != 0 ) AND ( $i % 5 != 0 ) ) { $out[] = $i; } } return ( $out ); } $from = 40; $to = 500; $out = get_primes ( $from, $to ); echo '<pre>'; print_r ( $out ); echo '</pre>'; ?>

me!

### #6

Posted 09 October 2006 - 06:16 AM

Can I ask what class you got a homework assignment in PHP for? Seems like most introductory programming courses aren't taught in scripting languages, and rightfully so.

### #7

Posted 09 October 2006 - 07:06 AM

printf,

That method is only good for a a range from 1 to 5^2

For example, your output contains 49, 77 and about 40 other products of primes > 5.

EDIT PS - Sieve of Barandyknees

That method is only good for a a range from 1 to 5^2

For example, your output contains 49, 77 and about 40 other products of primes > 5.

EDIT PS - Sieve of Barandyknees

<?php $a = range(0,500); $limit = ceil(sqrt(500)); for ($i=2; $i < $limit; $i++) { for ($j=$i*2; $j<=500; $j+=$i) { $a[$j] = ''; } } foreach ($a as $n) { if ($n) { echo "$n <br />"; } } ?>

*If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.*

Donations gratefully received

|baaGrid| easy data tables - and more |baaChart| easy line, column and pie charts |

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users