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()MethodFirst 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.