How to Find The Sum of The First 1000 Prime Numbers Java Program
Program:
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:
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
Program:
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:
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
To find the sum of the first 1000 prime numbers in Java, you can implement a method to check if a number is prime and then iterate through numbers to find and sum up the first 1000 prime numbers. Here is a sample Java program to achieve this:
public class SumOfFirst1000Primes {
public static void main(String[] args) {
int count = 0;
int num = 2;
long sum = 0;
while (count < 1000) {
if (isPrime(num)) {
sum += num;
count++;
}
num++;
}
System.out.println("The sum of the first 1000 prime numbers is: " + sum);
}
public static boolean isPrime(int num) {
if (num <= 1) {
return false;
}
if (num == 2) {
return true;
}
if (num % 2 == 0) {
return false;
}
for (int i = 3; i <= Math.sqrt(num); i += 2) {
if (num % i == 0) {
return false;
}
}
return true;
}
}
Explanation
Main Method:
count
keeps track of the number of prime numbers found.num
is the current number being checked.sum
accumulates the sum of the prime numbers.
Prime Checking Method:
isPrime(int num)
: This method returnstrue
ifnum
is a prime number andfalse
otherwise.- It checks if
num
is less than or equal to 1, in which case it is not prime. - It checks if
num
is 2, which is a prime number. - It checks if
num
is divisible by 2, which means it is not prime (except for 2 itself). - For numbers greater than 2 and not even, it checks divisibility starting from 3 up to the square root of
num
, incrementing by 2 to skip even numbers.
Finding and Summing Primes:
- The program uses a
while
loop to find the first 1000 prime numbers. - If a number is prime, it is added to the
sum
, andcount
is incremented. - The loop continues until
count
reaches 1000.
- The program uses a
Output:
- After finding the first 1000 primes, the sum is printed.
This program efficiently checks for prime numbers and sums the first 1000 primes. The isPrime
method uses the square root optimization to reduce the number of checks needed to determine if a number is prime.
No comments:
Write comments