C#
#include<stdio.h>
#include<math.h>
#include<stdbool.h>
bool bo(int i);
int main(){
long long sum = 2L;
for(int i=3;i<2000000;i+=2){
if(bo(i)) sum +=i;
}
printf("%lld",sum);
}
bool bo(int i){
if(i == 2 || i ==3) return true;
for(int j = 3;j < sqrt(i)+1;j++){
if (i % j == 0)
{
return false;
}
}
return true;
}
JAVA
package exercise;
public class _质数求和 {
private static boolean isPrime(int n) {
if (n == 2 || n == 3) {
return true;
}
if (n % 2 == 0) {
return false;
}
for (int i = 3; i < Math.sqrt(n) + 1; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
long num = 2L;
for(int i=3;i<2000000;i+=2){
if (isPrime(i))
num+=i;
}
System.out.println(num);
}
}
结果:143042032112