signum12 Posted October 8, 2006 Share Posted October 8, 2006 Hey can i have some help to build a .php file with the following questions1.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 two5.Add all the odd prime number and all the even prime number in the list together.Wish i can have an answer asap THANKS!!! Quote Link to comment https://forums.phpfreaks.com/topic/23340-prime-number-from-1-to-500/ Share on other sites More sharing options...
paul2463 Posted October 8, 2006 Share Posted October 8, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/23340-prime-number-from-1-to-500/#findComment-105791 Share on other sites More sharing options...
brendandonhue Posted October 8, 2006 Share Posted October 8, 2006 [quote author=paul2463 link=topic=110865.msg448796#msg448796 date=1160302539]there is no such thing as an even prime number[/quote]What about 2 ;) Quote Link to comment https://forums.phpfreaks.com/topic/23340-prime-number-from-1-to-500/#findComment-105995 Share on other sites More sharing options...
akitchin Posted October 8, 2006 Share Posted October 8, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/23340-prime-number-from-1-to-500/#findComment-106011 Share on other sites More sharing options...
printf Posted October 8, 2006 Share Posted October 8, 2006 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! Quote Link to comment https://forums.phpfreaks.com/topic/23340-prime-number-from-1-to-500/#findComment-106012 Share on other sites More sharing options...
Hi I Am Timbo Posted October 9, 2006 Share Posted October 9, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/23340-prime-number-from-1-to-500/#findComment-106119 Share on other sites More sharing options...
Barand Posted October 9, 2006 Share Posted October 9, 2006 printf,That method is only good for a a range from 1 to 5^2For 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] Quote Link to comment https://forums.phpfreaks.com/topic/23340-prime-number-from-1-to-500/#findComment-106126 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.