projecteuler Summation of primes

时间:2023-03-09 15:18:48
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:

 import java.util.Scanner;

 public class Main {
     public static void main(String[] args)
     {
         Scanner input = new Scanner(System.in);
         long start = System.currentTimeMillis();
         System.out.println(su(2000000));
         long end = System.currentTimeMillis();
         System.out.println(end-start);
     }
     /*
    *  判断是否为素数
    * /
     static boolean sum(int n)
     {
         boolean isPrime=true;
         int s=(int)Math.sqrt(n);
         for(int i=s;i>1;i--)
         {
             if(n%i==0)
             {
                 isPrime=false;
             }
         }
         return isPrime;
     }
     /*
    *  循环遍历素数
    *  求和
    */
     static long su(int n)
     {
         long sum=0;
         for(int i=2;i<n;i++)
         {
             if(sum(i)== true)
             {
                 sum += i;
             }
         }
         return sum;
     }
 }

projecteuler  Summation of primes

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