Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Java Program for Sum of Prime Problem

Sum of Prime Problem statement

As stated on Codeeval site:
https://www.codeeval.com/open_challenges/4/

I scored 100 with below solution on CodeEval. I recommend to look at the solution only if you are really stuck after trying hard by yourself.

Solution:


public class SumPrime {
public static void main(String args[])
{
int i = 1,j,s=0,d=0;
int count = 0;
int primeSum = 0;
while(i > 0){
s=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
s=s+1; //to count: Divided by how many numbers
}
if(s==2) //if divided by exactly 2 numbers, its prime
{
count++;
//System.out.println(i);
primeSum = primeSum + i;
d=d+1;
}
if(count == 1000)
{
System.out.println(primeSum);
break;
}
i++;
}
//System.out.println("the no. of prime no. are = "+d);
}
}



This post first appeared on Ctrl+alt+solve, please read the originial post: here

Share the post

Java Program for Sum of Prime Problem

×

Subscribe to Ctrl+alt+solve

Get updates delivered right to your inbox!

Thank you for your subscription

×