Page 1 of 2 12 LastLast
Results 1 to 20 of 28
  1. #1
    SapphireSpark is offline Member
    Join Date
    Oct 2008
    Posts
    38
    Rep Power
    0

    Unhappy (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.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,873
    Rep Power
    25

    Default

    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.

  3. #3
    SapphireSpark is offline Member
    Join Date
    Oct 2008
    Posts
    38
    Rep Power
    0

    Default

    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?

  4. #4
    SapphireSpark is offline Member
    Join Date
    Oct 2008
    Posts
    38
    Rep Power
    0

    Default

    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.

  5. #5
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    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.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,873
    Rep Power
    25

    Default

    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.

  7. #7
    SapphireSpark is offline Member
    Join Date
    Oct 2008
    Posts
    38
    Rep Power
    0

    Default

    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?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,873
    Rep Power
    25

    Default

    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)

  9. #9
    SapphireSpark is offline Member
    Join Date
    Oct 2008
    Posts
    38
    Rep Power
    0

    Default

    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. #10
    SapphireSpark is offline Member
    Join Date
    Oct 2008
    Posts
    38
    Rep Power
    0

    Default

    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.

  11. #11
    SapphireSpark is offline Member
    Join Date
    Oct 2008
    Posts
    38
    Rep Power
    0

    Default

    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?

  12. #12
    SapphireSpark is offline Member
    Join Date
    Oct 2008
    Posts
    38
    Rep Power
    0

    Default

    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.

  13. #13
    SapphireSpark is offline Member
    Join Date
    Oct 2008
    Posts
    38
    Rep Power
    0

    Default

    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?

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,873
    Rep Power
    25

    Default

    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

  15. #15
    SapphireSpark is offline Member
    Join Date
    Oct 2008
    Posts
    38
    Rep Power
    0

    Default

    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.

  16. #16
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    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.

  17. #17
    SapphireSpark is offline Member
    Join Date
    Oct 2008
    Posts
    38
    Rep Power
    0

    Default

    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.

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,873
    Rep Power
    25

    Default

    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.

  19. #19
    SapphireSpark is offline Member
    Join Date
    Oct 2008
    Posts
    38
    Rep Power
    0

    Default

    ^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.

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,873
    Rep Power
    25

    Default

    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?

Page 1 of 2 12 LastLast

Similar Threads

  1. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-15-2008, 12:46 AM
  2. (Help) Fraction Summation and Exponents
    By SapphireSpark in forum New To Java
    Replies: 19
    Last Post: 10-09-2008, 05:01 AM
  3. Prime numbers
    By tercius in forum New To Java
    Replies: 3
    Last Post: 05-04-2008, 07:05 AM
  4. Computing prime numbers in Java
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-12-2008, 09:39 PM
  5. Prime numbers
    By gapper in forum New To Java
    Replies: 3
    Last Post: 02-07-2008, 11:09 AM

Posting Permissions

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