Prime numbers c# [closed]
I´m sorry but I have to ask a very simple question. My problem is that I want to generate the prime numbers until a maximum number.
here´s my code:
for (int i = 2; i <= max - 1; i++)
{
System.Threading.Thread.Sleep(1000);
if (Progress != null)
{
if (max%i == 0)
Console.WriteLine(i);
}
}
My code doesn´t work and I don´t know why..
Can you please help me?
Hint 1: Algorithm optimization
You don't need to go all the way to the max number. You need only to go to its square root.
Think with me with this example: if 100 is divided by 50, it was first caught as being divided by 2. So you only need to go up to 10, any divisor you find after that, its counterpart would have been found before.
But this is only how you optimally find whether a specific number is prime or not.
Hint 2: IsNumberPrime()
Method
First of all, you have to have a method to determine whether a specific number is prime or not. Keep in mind hint 1 here.
Hint 3: Doing It Right This Time
After you have followed hints 1 and 2, now you can loop and determine the highest prime number lower then your max number.
Conclusion
Sorry, no code here, just general orientation. Doing your homework for you will not help you. This answer will.
While you can check every number for primality, that's a waste of work. There are many numbers which you don't even have to consider. Even numbers except 2, for one. There are more tricks, such as something about multiples of 6 (google it)
If you want to generate prime numbers, use an algorithm that generates prime numbers. Seems obvious, right? But an algorithm that tests for primality is not in that category. The Sieve of Eratosthenes is nice, the Sieve of Atkin is nicer.
If you're going to check number for primality anyway (but seriously, don't), trial division is the worst sane algorithm to do so unless your numbers are always tiny (say, smaller than 10k). Use the deterministic version of the Miller Rabin test with known upper bounds (not the full one, because you DO have known upper bounds, otherwise you'd be generating all PositiveInfinity primes).
I'm just gonna be stupid and give you some code I wrote for an informatics class some time ago. Everything is ulong because it was first designed as a prime number check for some waaaaaaayy too high numbers.
ulong min = 0;
ulong max = 100000;
ulong i = 0;
ulong sqrt = 0;
ulong j = 0;
bool prime = true;
for (i = min; i <= max; i += 2)
{
prime = true;
sqrt = Math.Sqrt(i) + 1;
if (i % 2 == 0) prime = false;
else for (j = 3; j < sqrt; j += 2)
{
if (i % j == 0)
{
prime = false;
break;
}
}
if(prime)
{
Console.WriteLine(i);
}
}
It is slightly edited so I'm not entirely sure if this will work as expected, but it can't be too hard to edit it yourself.
Method to check if a number is prime:
bool IsPrime(int number)
{
for (int i = 2; i < sqrt(number); i++)
{
if (number%i == 0) return false;
}
return true;
}
Call this method for all integers up to max:
for (int crt = 1; crt <= max; crt++)
{
if (IsPrime(crt))
{
Console.WriteLine(crt);
}
}
精彩评论