Results 1 to 20 of 28
 10152008, 03:24 AM #1Member
 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 userspecified 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<=n1; d++) { if (n%d==0) { isPrime=false
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; 10252008 at 09:33 AM.
 10152008, 04:42 AM #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.
 10152008, 05:16 AM #3Member
 Join Date
 Oct 2008
 Posts
 38
 Rep Power
 0
My question is how do I compute all the prime numbers up to the userinputed 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 userinputed n is squared in the denominator.
How do I get Java to do that?
 10152008, 06:01 AM #4Member
 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.
 10152008, 07:57 AM #5Senior Member
 Join Date
 Sep 2008
 Posts
 564
 Rep Power
 7
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.
 10152008, 02:55 PM #6what an arg is
aMethod(args)
Do you have a text book? You'll need to know these things to be able to discuss them on the fourm.
 10152008, 06:28 PM #7Member
 Join Date
 Oct 2008
 Posts
 38
 Rep Power
 0
You can easily solve your problem with the piece of code you supplied.
 10152008, 07:56 PM #8
Use the answer!
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)
 10202008, 09:47 AM #9Member
 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?
 10202008, 09:50 AM #10Member
 Join Date
 Oct 2008
 Posts
 38
 Rep Power
 0
Also, I've been testing the code of
Java Code:for (int d = 2; d<=n1; d++) { if (n%d==0) { isPrime=false
Last edited by SapphireSpark; 10202008 at 10:15 AM.
 10202008, 09:52 AM #11Member
 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 n1, then adding 1 to d, and finally checking what n%i equals before starting the whole thing over, with d increased by 1?
 10202008, 09:56 AM #12Member
 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<=n2, 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; 10202008 at 10:14 AM.
 10202008, 10:13 AM #13Member
 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); } }
 10202008, 03:25 PM #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
 10202008, 08:11 PM #15Member
 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); } }
And why isn't it continuing the summation through all the prime numbers until n?Last edited by SapphireSpark; 10202008 at 08:50 PM.
 10202008, 08:31 PM #16Senior Member
 Join Date
 Sep 2008
 Posts
 564
 Rep Power
 7
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.
 10202008, 08:35 PM #17Member
 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; 10202008 at 09:00 PM.
 10202008, 08:59 PM #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.
 10202008, 09:10 PM #19Member
 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);
Last edited by SapphireSpark; 10202008 at 09:13 PM.
 10202008, 09:28 PM #20
Similar Threads

printing two smallest numbers from a series of numbers
By trofyscarz in forum New To JavaReplies: 2Last Post: 10152008, 12:46 AM 
(Help) Fraction Summation and Exponents
By SapphireSpark in forum New To JavaReplies: 19Last Post: 10092008, 05:01 AM 
Prime numbers
By tercius in forum New To JavaReplies: 3Last Post: 05042008, 07:05 AM 
Computing prime numbers in Java
By Java Tip in forum java.langReplies: 0Last Post: 04122008, 09:39 PM 
Prime numbers
By gapper in forum New To JavaReplies: 3Last Post: 02072008, 11:09 AM
Bookmarks