# Thread: (Help) Quotient summation with prime numbers

1. Member Join Date
Oct 2008
Posts
38
Rep Power
0

## (Help) Quotient summation with prime numbers

Hi. I'm writing a program that calculates the product of a summation with the formula of (1 + (1/a prime number squared)). This summaton continues until the number in the denominator reaches the user-specified prime number n.

I have a picture demonstrating this here: I just know how to calculate a prime number as a boolean value from class, using

Java Code:
```for (int d = 2; d<=n-1; d++) {

if (n%d==0) {
isPrime=false```
Can I apply that to calculate all the prime numbers up until n? I've been to the Java Tip topic "Computing Prime Numbers", but that uses things we haven't used in class yet, like long and arrays.

I've learned from my previous topic that a summation like this uses the for and do statements, but I can't figure out how to apply them to this problem. Please help.
Last edited by SapphireSpark; 10-25-2008 at 09:33 AM.  Reply With Quote

2. ## I'm not sure what your question is?
Have you tried putting your code in a method that returns true/false if its arg is prime?
To test it write a loop to call it with the a series of numbers and println() the results.
A later optimization might be to pass the starting value for the search, instead of always using 2.  Reply With Quote

3. Member Join Date
Oct 2008
Posts
38
Rep Power
0

## My question is how do I compute all the prime numbers up to the user-inputed prime number n, and then place those in a summation with a sequence of (1+[1/2squared]) times (1+[1/3squared]) times (1+[1/5squared])...and continueing until the user-inputed n is squared in the denominator.

How do I get Java to do that?  Reply With Quote

4. Member Join Date
Oct 2008
Posts
38
Rep Power
0

## And I don't know exactly what an arg is, I started this class only a month ago. How do you pass the starting value? We're pretty much limited to if, else, for, and switch statements, and we've only been using int, double, and boolean values, not the long or array statements I found in the other prime number tip topic.  Reply With Quote

5. Senior Member Join Date
Sep 2008
Posts
564
Rep Power
13

## You can easily solve your problem with the piece of code you supplied. You don't need to look at the other topic, just understand how to use loops. In your case, you would need to learn how to iterate through a range of numbers.  Reply With Quote

6. ## what an arg is
arg is short for argument. In the call to aMethod below, args are its arguments, the values passed to the method:
aMethod(args)

Do you have a text book? You'll need to know these things to be able to discuss them on the fourm.  Reply With Quote

7. Member Join Date
Oct 2008
Posts
38
Rep Power
0

## You can easily solve your problem with the piece of code you supplied.
How? It spits out a boolean answer. I need to plug in every prime number until n into the summation. Doesn't it need to calculate the actual prime numbers for that?  Reply With Quote

8. ## Ask the method: is x a prime number?
if it returns true, then x is prime then use x in the equation
if it returns false, do NOT use x in the equation.
(vary x from 3 to n by 2)  Reply With Quote

9. Member Join Date
Oct 2008
Posts
38
Rep Power
0

## Ok, then if "x" returns true, should it be in the loop, or outside it? Would the loop also include the first term of the summation?  Reply With Quote

10. Member Join Date
Oct 2008
Posts
38
Rep Power
0

## Also, I've been testing the code of

Java Code:
```for (int d = 2; d<=n-1; d++) {

if (n%d==0) {
isPrime=false```
But even the prime numbers always have a n%d=0 on the very last check of d. That would make every possible input not prime. So it doesn't sound like the right way to compute prime numbers.
Last edited by SapphireSpark; 10-20-2008 at 10:15 AM.  Reply With Quote

11. Member Join Date
Oct 2008
Posts
38
Rep Power
0

## Am I checking it wrong by starting out with d=2, then making sure d is less than/equal to n-1, then adding 1 to d, and finally checking what n%i equals before starting the whole thing over, with d increased by 1?  Reply With Quote

12. Member Join Date
Oct 2008
Posts
38
Rep Power
0

## The way I'm checking it on paper, it would work if the middle term in the for loop was d<=n-2, so is my sequence wrong? But I know I can't change that, since its straight from class notes. I thought that for loop was working right, but is there an exception to it somewhere?
Last edited by SapphireSpark; 10-20-2008 at 10:14 AM.  Reply With Quote

13. Member Join Date
Oct 2008
Posts
38
Rep Power
0

## With all that asked, here's what I have so far:

Java Code:
```    import javax.swing.JOptionPane;
public class Project7 {

public static void main(String[] args) {

String input = JOptionPane.showInputDialog("Enter number");

int n = Integer.parseInt(input);
int sum = 1;

boolean isPrime = true;

for (int i=2; i<=n -1; i++) {
if (n % i ==0) {
isPrime = false;
}

if(isPrime=true){
sum = sum * (1+(1/(i*i)));
}

}
JOptionPane.showMessageDialog(null, "The sum is:" + sum);

}
}```
But that's not turning out the correct answers when I test it out. Am I testing it on paper wrong, or is the loop wrong?  Reply With Quote

14. ## It would help if you would show the results of testing your code.
Try debugging your code by adding println()s to show all the values and computations as they are done.

Your logic seems to say if n % i != 0 then a number is prime.
Is that what you want? You need to test all possible i values before finding that the number is prime. Your code only tests the first value of i  Reply With Quote

15. Member Join Date
Oct 2008
Posts
38
Rep Power
0

## Yes, when n%i=0, then i is a factor of n, thus its not a prime. I've updated my loop, now its capable of finding prime numbers, and the sum uses the prime numbers, not i:

Java Code:
```    import javax.swing.JOptionPane;
public class Project7 {

public static void main(String[] args) {

String input = JOptionPane.showInputDialog("Enter number");

int n = Integer.parseInt(input);
double sum = 1;

boolean isPrime = true;

for (int i=2; i<=n/2; i++) {
if (n % i ==0) {
isPrime = false;
break;
}
if(isPrime=true){
sum = sum * (1+(1/(i*i)));
JOptionPane.showMessageDialog(null, "The sum at this moment is:" + sum);
}

}
JOptionPane.showMessageDialog(null, "The sum is:" + sum);

}
}```
Now my only concerns are if the loop will test all values of i until n/2 (which are all that are necessary), and if it will include the first term of the summation, (1+(1/2squared)). Does it look like it will?

And why isn't it continuing the summation through all the prime numbers until n?
Last edited by SapphireSpark; 10-20-2008 at 08:50 PM.  Reply With Quote

16. Senior Member Join Date
Sep 2008
Posts
564
Rep Power
13

## Big hints here:

1. Look at what you're doing with the 'isPrime' variable. I'd assume that the compiler is giving you a warning about it as well.

2. Look at what your loop is doing. Understand what 'i' and 'n' are. I recommend writing pseudocode with detailed descriptions.  Reply With Quote

17. Member Join Date
Oct 2008
Posts
38
Rep Power
0

## Actually, I can't compile anything at the moment, I keep getting a message saying that Netbeans can't find "build.xml". I'm trying to fix that since the program's installed on my flash drive. I've edited the code above on my own recently, as well, but I don't think it's using all the prime numbers until n in the summation. Is it? Or do I need to make another variable to represent the prime numbers in between?

What's wrong with isPrime and the loop, emcee?
Last edited by SapphireSpark; 10-20-2008 at 09:00 PM.  Reply With Quote

18. ## The boolean value by itself is sufficient in the if condition. You don't need to compare it to true or false. The boolean variable itself is true or false.  Reply With Quote

19. Member Join Date
Oct 2008
Posts
38
Rep Power
0

## ^Ok, I've changed that to simply if(isPrime){, but I really have bigger problems with the code. How do I get every prime number in between 2 and n to be used in the summation? Here's what I currently have:
Java Code:
```    public static void main(String[] args) {

String input = JOptionPane.showInputDialog("Enter number");

int n = Integer.parseInt(input);
double sum = 1;

boolean isPrime = true;

for (int i=2; i<=n/2; i++) {
if (n % i ==0) {
isPrime = false;
break;
}
if(isPrime=true){
sum = sum * (1+(1/(n*n)));
JOptionPane.showMessageDialog(null, "The sum at this moment is:" + sum);
}

}
JOptionPane.showMessageDialog(null, "The sum is:" + sum);```
Should i somehow become every prime number in between 2 and n, or is something else wrong with it?
Last edited by SapphireSpark; 10-20-2008 at 09:13 PM.  Reply With Quote

20. ## Try debugging your code by adding println() statements to show variable values and execution flow.
changed that to simply if(isPrime){
What code are you showing vs what you are changing?  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•