Algorithm for finding prime number algorithm __

Source: Internet
Author: User
Tags bool square root

On the internet to see a lot of algorithms to find prime numbers, are checking from 2 to n-1 the number can be divisible by n, can be not prime, the reverse is prime, this is certainly correct, but wasted some unnecessary checks, in fact, as long as the check from 2 to sqrt (n) The number is possible, because if a number has a factor, Then it must have a factor not less than the square root of the number.
public static void Primenum (int maxnum)
{
for (int i = 3; I <= maxnum; i++)
{
bool IsPrime = true;
for (int j = 2; J <= Math.sqrt (i); j + +)
{
if (i% j = 0)
{
IsPrime = false;
break;//a factor proves to be composite, exit the loop immediately.
}
}
if (IsPrime)
{
Console.Write (i.tostring () + "");
}
}
}
The sieve will be a better algorithm, see my other article: Since a composite can always be decomposed into the product of several prime numbers, if the prime number (originally known only 2 is prime) is removed, then the rest is prime.
For example, to find a prime number within 100, first 2 is prime, the multiples of 2 are removed, at this time 3 is not removed, can be considered a prime number, so the multiples of 3 are removed, and then 5, then 7, 7, because 8,9,10 was just removed, And within 100 of any composite must have a factor of less than 10 (100 of the root), so, remove, 2,3,5,7 a multiple after the rest are prime.
The program can be solved this way by introducing the Boolean type array a[i], if I is prime, a[i]=true, otherwise a[i]=false. So the cross-out I can be expressed as a[i]=false.
Find prime numbers within n
void Sieve (int n)
{
Bool[] A = new bool[n+1];
for (int i = 2; I <= n; i++) a[i] = true;
for (int i = 2; I <= math.sqrt (n); i++)
{
if (A[i])
for (int j = i; j*i <= N; j + +) A[j * I] = false;
}
for (int i = 0; I <= N; i++)
{
if (A[i])
Console.Write ("{0},", i.ToString ());
}
}

If you remove the last loop to display the results, run sieve (10000000) as long as 1 seconds, and the last time the algorithm Primenum (10000000) is more than 71 seconds.

Http://www.cnblogs.com/guoxiaocong/archive/2005/12/27/305611.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.