projecteuler Summation of primes

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

Find the sum of all the primes below two million.

译文:

10以下的素数之和为17,求出2000000以下的素数之和。

=======================

第一次code:

 1 import java.util.Scanner;
 2 
 3 public class Main {
 4     public static void main(String[] args) 
 5     {
 6         Scanner input = new Scanner(System.in);
 7         long start = System.currentTimeMillis();
 8         System.out.println(su(2000000));
 9         long end = System.currentTimeMillis();
10         System.out.println(end-start);
11     }
12     /*
13    *  判断是否为素数
14    * /
15     static boolean sum(int n)
16     {
17         boolean isPrime=true;
18         int s=(int)Math.sqrt(n);
19         for(int i=s;i>1;i--)
20         {
21             if(n%i==0)
22             {
23                 isPrime=false;
24             }
25         }
26         return isPrime;
27     }
28     /*
29    *  循环遍历素数
30    *  求和
31    */
32     static long su(int n)
33     {
34         long sum=0;
35         for(int i=2;i<n;i++)
36         {
37             if(sum(i)== true)
38             {
39                 sum += i;
40             }
41         }
42         return sum;
43     }
44 }

结果为142813828922,时间效率为8257毫秒。

posted on 2016-09-04 22:09 niithub 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/niithub/p/5840513.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值