穷举法求某个数是否是素数:用n除以1~sqrt(n)里面的每一个数。如果找不到能除尽的数,那么n就是素数。代码好简单,略。筛选法求1~N范围内的素数:1.先找到第一个素数p2.去除p的倍数,既非素数3.在剩下的数里面找到大于p的最小的数,它也是素数,用它更新p,重复2,3,直到到达N.0~1亿以内的素数个数,Time cost(milli-second)Numbers of primes in 1~100=25 Time cost=0Numbers of primes in 1~1000=168 Time cost=0Numbers of primes in 1~10000=1229 Time cost=0Numbers of primes in 1~100000=9592 Time cost=7Numbers of primes in 1~1000000=78498 Time cost=53Numbers of primes in 1~10000000=664579 Time cost=502Numbers of primes in 1~100000000=5761455 Time cost=8665Press any key to continue . . .
Copyright @ sailing Powered by: .Text and ASP.NET Theme by: .NET Monster