Java进阶之欧拉工程 第十篇【持续更新】

原题如下:

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.

翻译:

低于10的素数的和是2+3+5+7=17,

要求所有低于2 000 000的素数之和。


解题思路:由于素数的出现并没有较为简单的规律,所以这道题还是采用对所有奇数的遍历,最后加上2,得到答案142913828922,代码如下:

 

package oula10;


public class Launcher {
	public static final long max= 2000000L;
	public static void main(String[] args) {
		
		     long sum=2L;
		        for(long i= 3L;i<max; i+=2){  
		          if(is_prime(i)){
		        	  sum+=i;
		        	
		          } 
		        }
		        System.out.println(sum);
		 
		
}
	public static boolean is_prime(long i) //检测是否为素数
    {  
          
        for(int j=2;j<=Math.sqrt(i);j++){  
            if(i%j==0)  
            {  
                return false;  
            }  
        }  
            return true;  
    }  
  
  
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值