Jump to content

Archived

This topic is now archived and is closed to further replies.

signum12

Prime number from 1 to 500

Recommended Posts

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!!!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
[quote author=paul2463 link=topic=110865.msg448796#msg448796 date=1160302539]
there is no such thing as an even prime number[/quote]
What about 2 ;)

Share this post


Link to post
Share on other sites
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.

[b]EDIT:  will NOT continue this later as the others have taken over and provided some sieves.[/b]

Share this post


Link to post
Share on other sites
There are all kinds of methods, I like the [b]sieve of atkin[/b], it great for long ranges, but for a short range a simple if() block works well!

[code]<?

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>';

?>[/code]


me!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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
[code]
<?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 />";
    }
}

?>[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.