Friday 16 June 2017

Write a program to find the sum of the first 1000 prime numbers.


Description:
Write a program to find the sum of the first 1000 prime numbers.

Code:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.primesum;
 
public class Main {
 
    public static void main(String args[]){
         
        int number = 2;
        int count = 0;
        long sum = 0;
        while(count < 1000){
            if(isPrimeNumber(number)){
                sum += number;
                count++;
            }
            number++;
        }
        System.out.println(sum);
    }
     
    private static boolean isPrimeNumber(int number){
         
        for(int i=2; i<=number/2; i++){
            if(number % i == 0){
                return false;
            }
        }
        return true;
    }
}

Output:
3682913

No comments:

Post a Comment