I know there is one which is used in all kind of .NET dictionaries and hashtables in:
internal static class HashHelpers
And so on. The numbers that remain are prime: 2, 3, 5, 7, 11, 13, 17, 19, 23, and 29.
This c++ code prints out the following prime numbers: 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97. But I don't think that's the way my book wants it to be written.
First, take the number N as input. Then use a for loop to iterate the numbers from 1 to N. Then check for each number to be a prime number. If it is a prime number, print it.
I cannot answer your question regarding the availability of HashHelpers, but here are ways of implementing it yourself.
Here a post with some imperative implementations on generating prime numbers: Most elegant way to generate prime numbers
Additionally, you can do it in LINQ:
var odds =
from n in Enumerable.Range(0, int.MaxValue)
select 3 + (long) n * 2;
var primes = (new[] { 2L }).Concat(
from p in odds
where ! odds.TakeWhile(odd => odd * odd <= p).Any(odd => p % odd == 0)
select p);
Source: http://jacobcarpenter.wordpress.com/2008/03/26/linq-to-prime-numbers/
Edit: Don't use int.MaxValue in your initial range. Limit this to something appropriate.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With