读书人

Problem 十

发布时间: 2012-11-03 10:57:44 作者: rapoo

Problem 10
问题描述:
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.



public static boolean IsPrime(long number) {int begin = 2;int end = (int) Math.sqrt(number) + 1;for (int i = begin; i < end; i++) {if (number % i == 0)return false;}return true;}public static long sum(){long result = 0;int begin = 2;int end = 2000000;for(int i=begin; i<end; i++){if(IsPrime(i)){result += i;}}return result;}

读书人网 >编程

热点推荐