Jump to content

Prime number from 1 to 500


signum12

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!!!
Link to comment
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
Link to comment
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]
Link to comment
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!
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.