Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By jim829

Thread: BigInteger Problems

  1. #1
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default BigInteger Problems

    Hi! This won't compile:

    Java Code:
    import java.math.BigInteger;
    class Problem48 {
      public static void main (String[] args) {
        BigInteger sum = new BigInteger(0);
        for(int x = 1; x <= 1000; x++) {
            
          sum = sum.add(BigInteger.valueOf(((long)Math.pow(x,x))));
          }
        }
       System.out.println(sum.toString()); // <---this highlights and saying something about needing an <identifier>?
      }
    }
    All I want to do is find the sum of all selfpowers of integer from 1 to 1000: What is wrong with my code?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,034
    Rep Power
    6

    Default Re: BigInteger Problems

    The error message is because the print statement is outside of the main method. But your problems are far worse than that. You need to read the BigInteger API carefully. Hint: Math.pow() won't work (at least not the way you probably want).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: BigInteger Problems

    My female intuition suggests that it should be possible to find x^x given the value (x-1)^(x-1) by induction, but I didn't have my espresso yet ...

    kind regards,

    Jos (<--- sleepy)
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: BigInteger Problems

    By induction? Can you expand on that? Maybe give a small example?

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,034
    Rep Power
    6

    Default Re: BigInteger Problems

    In the meantime, the BigInteger class can solve your problem. Did you read it?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: BigInteger Problems

    Quote Originally Posted by AlexGraal View Post
    By induction? Can you expand on that? Maybe give a small example?
    I think the way to go is by using Newton's binomium; if you know (x-1)^(x-1) (and you know that for x == 1, you can figure out x^x.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: BigInteger Problems

    Yes I did - and I specifically looked at this:
    BigInteger.valueOf(long val): Returns a BigInteger whose value is equal to that of the specified long.
    So I see no issue logically to doing
    Java Code:
    BigInteger.add( BigInteger.valueOf((long) Math.pow(x,x)))
    However, I'm still having issues with my declaration and initialization line,
    Java Code:
    BigInteger sum = new BigInteger(0);
    I don't see why I'm getting an error there - Dr Java says that BigInteger class has private access or something like that?
    Last edited by AlexGraal; 01-13-2014 at 12:59 AM.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,034
    Rep Power
    6

    Default Re: BigInteger Problems

    Quote Originally Posted by AlexGraal View Post
    Yes I did - and I specifically looked at this:


    So I see no issue logically to doing
    Java Code:
    BigInteger.add( Math.valueOf((long) Math.pow(x,x)))
    However, I'm still having issues with my declaration and initialization line,
    Java Code:
    BigInteger sum = new BigInteger(0);
    Well, you need to read the documentation again because there is no BigInteger constructor which takes an int as an argument.
    And the problem with Math.pow(x,x) is that x is a regular old int and Math.pow takes a double. Now it will do a fine conversion for you and return a double. But that double has limited accuracy (long before x gets to 1000). So you need to re-read the BigInteger documentation carefully. You may notice a method similar to the one you are using.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: BigInteger Problems

    Ok, I'm going to go through what I see happening and you correct me where I'm wrong.


    First, we have Math.pow(). From the API:
    pow(double a, double b)
    Returns the value of the first argument raised to the power of the second argument.
    So, I'll need to change the inputs to be doubles. Anyway, my main method:

    Java Code:
      public static void main (String[] args) {
        BigInteger sum = new BigInteger("0");
        for(double x = 1; x <= 1000; x++) {
            
          sum = sum.add(BigInteger.valueOf(((long)Math.pow(x,x))));
          }
       System.out.println(sum.toString());
      }
    In line two, I'm creating a BigInteger called sum with a value of 0.
    Then, I set a forloop that starts at 1 and goes up to 1000 in increments of 1.

    The next line,
    Java Code:
    sum = sum.add(BigInteger.valueOf(((long)Math.pow(x,x))));
    Is setting the value of sum as sum + the value of x^x, which is converted to a long, then converted to a BigInteger.

    Am I missing something here? It makes sense to me to do Math.pow, convert that to a long (so that the next part will work), then convert the long to a BigInteger.
    Last edited by AlexGraal; 01-13-2014 at 01:09 AM.

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,034
    Rep Power
    6

    Default Re: BigInteger Problems

    You cannot use the Math class with BigInteger unless the arguments show it takes a BigInteger object. BigInteger is a very special class that implements arbitrary precision using internal vectors. It has nothing to do with the typical int or long primitives. For example, a long is 64 bits wide which equates to roughly 20 digits. Compare that to 1000^1000 which is 3001 digits.

    What I have been steering you toward is the BigInteger.pow() function. Try these to see the difference.

    Java Code:
    BigInteger b1 = BigInteger.valueOf((long)Math.pow(25,25));
     System.out.println(b1);
     System.out.println(Math.pow(25,25));
     System.out.println(new BigInteger("25").pow(25));
    Regards,
    Jim
    AlexGraal likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. BigInteger
    By 7cardcha in forum New To Java
    Replies: 9
    Last Post: 10-25-2011, 07:06 AM
  2. BigDecimal and BigInteger
    By jon80 in forum New To Java
    Replies: 8
    Last Post: 11-28-2010, 12:15 PM
  3. BigInteger value of
    By ccie007 in forum New To Java
    Replies: 2
    Last Post: 09-26-2010, 04:46 PM
  4. BigInteger
    By windows.login in forum New To Java
    Replies: 8
    Last Post: 07-13-2010, 02:10 PM
  5. very new... Biginteger problems
    By willemien in forum New To Java
    Replies: 6
    Last Post: 05-02-2010, 04:17 PM

Posting Permissions

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