# (Help) Quotient summation with prime numbers

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-15-2008, 02:24 AM
SapphireSpark
(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:
http://fc27.deviantart.com/fs34/f/20...5973820f48.jpg

I just know how to calculate a prime number as a boolean value from class, using

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.
• 10-15-2008, 03:42 AM
Norm
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.
• 10-15-2008, 04:16 AM
SapphireSpark
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?
• 10-15-2008, 05:01 AM
SapphireSpark
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.
• 10-15-2008, 06:57 AM
emceenugget
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.
• 10-15-2008, 01:55 PM
Norm
Quote:

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.
• 10-15-2008, 05:28 PM
SapphireSpark
Quote:

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?
• 10-15-2008, 06:56 PM
Norm
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)
• 10-20-2008, 08:47 AM
SapphireSpark
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?
• 10-20-2008, 08:50 AM
SapphireSpark
Also, I've been testing the code of

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.
• 10-20-2008, 08:52 AM
SapphireSpark
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?
• 10-20-2008, 08:56 AM
SapphireSpark
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?
• 10-20-2008, 09:13 AM
SapphireSpark
With all that asked, here's what I have so far:

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?
• 10-20-2008, 02:25 PM
Norm
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
• 10-20-2008, 07:11 PM
SapphireSpark
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:

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?
• 10-20-2008, 07:31 PM
emceenugget
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.
• 10-20-2008, 07:35 PM
SapphireSpark
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?
• 10-20-2008, 07:59 PM
Norm
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.
• 10-20-2008, 08:10 PM
SapphireSpark
^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:
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?
• 10-20-2008, 08:28 PM
Norm
Try debugging your code by adding println() statements to show variable values and execution flow.
Quote:

changed that to simply if(isPrime){
What code are you showing vs what you are changing?
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last