# Printing Sum of Primes of a number

• July 11th, 2010, 03:25 PM
CodeNewb
Printing Sum of Primes of a number
I'm trying to print the sum of the prime numbers of a given number from the console. I think my test in my while loop might be wrong, but I'm not 100% sure. Any help, I would appreciate. Thanks in advance.

Code :

import java.util.*;

public class PrimeSum
{
public static int isPrime(int num)
{
int sum = 0;
int factor = 1;
while(factor <= num)
{
if(num % factor != 0)
{
sum += factor;
factor ++;
}
else
{
factor ++;
}
}
return sum;
}

public static void main(String[] args)
{
System.out.println("The program gets the sum of all prime numbers.");

Scanner scan = new Scanner(System.in);

System.out.print("Enter a number: ");
int num = scan.nextInt();

int sum = isPrime(num);

System.out.println(sum);
}
}

• July 11th, 2010, 04:18 PM
helloworld922
Re: Printing Sum of Primes of a number
Did you mean the sum of all prime factors of a number, or the sum of all prime numbers less than (and/or equal) to the number? The two will provide very different results.
• July 11th, 2010, 05:05 PM
CodeNewb
Re: Printing Sum of Primes of a number
The later...
• July 11th, 2010, 05:55 PM
Norm
Re: Printing Sum of Primes of a number
Try debugging your code by printing out the results of the tests so you can see what is happening. Add some println()s to the loop in the isPrime() method.
• July 11th, 2010, 06:19 PM
aussiemcgr
Re: Printing Sum of Primes of a number
Ok, I'm not that interested in coding this out, so I'll run you through what is going on with your code.

Lets call isPrime(5). This should return 3 (unless you consider 1 a prime number, which would result in 4).
Now what that does return is 9. Wrong answer :(. So, what is happening?

We start the method.
Sum = 0. Factor = 1. num = 5.
Start the loop. 1 <= 5? Yes. num%factor != 0? No, that equals 0. Increase factor.
2 <= 5? Yes. num%factor != 0? Yes, that equals 1. sum = 0+2. Increase factor.
3 <= 5? Yes. num%factor != 0? Yes, that equals 2. sum = 2+3. Increase factor.
4 <= 5? Yes. num%factor != 0? Yes, that equals 1. sum = 5+4. Increase factor.
5 <= 5? Yes. num%factor != 0? No, that equals 0. Increase factor.
6 <= 5? No. Return 9.

So, if you arent aware of what mod ( % ) does, it returns the remainder from division. Which means. When we do 5%1, we are actually doing 5/1 and returning the extra. So, 5/1 = 5 with a remainder of 0. 5/2 = 2 with a remainder of 1. 5/3 = 1 with a remainder of 2. 5/4 = 1 with a remainder of 1. 5/5 = 1 with a remainder of 0. Mod is getting its answer from the remainder of the integer division. Hopefully that can help you.
• July 11th, 2010, 10:26 PM
CodeNewb
Re: Printing Sum of Primes of a number
So that shouldn't even be part of the test. I'm confused then about what I need to do, I have been scratching my head all day it seems... I thank you for the help.
• July 12th, 2010, 12:13 AM
CodeNewb
Re: Printing Sum of Primes of a number
I tried going back to the basic of is a number prime and working it that way, and I sort of see the problem... How do I kept the sum going even though that loop is changing it?

Code :

import java.util.*;

public class PrimeSum3
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter a number: ");
int input = scan.nextInt();
boolean prime = true;

int num = 0;
int start = 2;
while(start < input)
{
if(input % start == 0)
{
prime = false;
}
if(prime == true)
{
num = start;
System.out.print(num + " " + start);  //for test purposes
num += num;
}
System.out.print(" " + num + " .");  //for test purposes
start++;
}

if(prime == false)
{
System.out.println(input + " is not prime");
}
else if(prime == true)
{
System.out.println(input + " is prime");
}
System.out.println(num);
}
}

Code :

//Output
2 2 4 .3 3 6 .4 4 8 .5 is prime
8

• July 12th, 2010, 07:55 AM
Norm
Re: Printing Sum of Primes of a number
Your debug output should be a bit more verbose. It's hard to tell what the list of numbers you've posted means:
println("num=" + num + ", start=" + start);

I have no idea what this means:
System.out.print(" " + num + " .");
• July 12th, 2010, 10:32 AM
kamalivy
Re: Printing Sum of Primes of a number
You need to go step-by-step. First find whether a number is prime (so, write a method which tells whether a given number is prime) and then add it to the sum (write a method which adds only prime numbers).

The following works for you:

public class SumOfPrimes{

public boolean isPrime(int x){
for(int i=2;i<x;i++){
if(x%i == 0)
return false;
}
return true;
}

public int sumOfAllPrimes(int x){
int sum = 0;
for(int i=2;i<x;i++){
if(isPrime(i)){
sum = sum+i;
System.out.println("sum: "+sum);
}
}
return sum;
}

public static void main(String[] args){
int number = 6;
SumOfPrimes sop = new SumOfPrimes();
System.out.println("sum of all primes less than the given number is: "+sop.sumOfAllPrimes(number));
}
}
• July 12th, 2010, 01:25 PM
CodeNewb
Re: Printing Sum of Primes of a number
I doubt I would of ever figured it out... Thanks for you help.

I like this by the way, I'm just learning java and I think this is neat...
Code :

SumOfPrimes sop = new SumOfPrimes();
System.out.println("sum of all primes less than the given number is: "+sop.sumOfAllPrimes(number));