Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Factorials in Java: Integer value larger than a long

  1. #1
    Join Date
    May 2009
    Location
    Canada
    Beans
    868
    Distro
    Ubuntu Development Release

    Factorials in Java: Integer value larger than a long

    Hey, I was calculating factorials in java for use in calculating combinations/permutations but the application would crash at anything over like 17. I tried using a float instead because they support larger values but I'd rather not have to cut off the decimal place every time and it doesn't even get that large. I tried a permutation/combination for a set size of 50 and it returned NaN.

    My question is, is there an integer type value that can get to really large numbers? Otherwise is there any type value that can reach very large numbers? I mean, the overall value after the division of multiple factorials doesn't end up that large. It's just in the middle steps you can get extremely large values before they are divided.
    Intel Core i7 970 6/12 (Cores/Threads) 3.2GHz 12MB Cache
    6GB DDR3
    1866MHz RAM | ATI Radeon HD 5770 1GB GDDR5
    60GB SSD
    (/) | 1TB 64MB Cache HDD (/home)
    Ubuntu Oneiric
    Ocelot 11.10 x64

  2. #2
    Join Date
    Sep 2009
    Location
    Canada, Montreal QC
    Beans
    1,807
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: Factorials in Java: Integer value larger than a long

    Java has a Bignum class that can hold large values.
    http://web.cs.mun.ca/~michael/java/j...ng.Bignum.html
    I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones.
    Freedom is measured in Stallmans.
    Projects: gEcrit

  3. #3
    Join Date
    May 2009
    Location
    Canada
    Beans
    868
    Distro
    Ubuntu Development Release

    Re: Factorials in Java: Integer value larger than a long

    Bignum doesn't seem to exist in Java.lang like it states it should. I've tried BigInteger but it seems too convoluted and not the best choice for a simple factorial method.
    Intel Core i7 970 6/12 (Cores/Threads) 3.2GHz 12MB Cache
    6GB DDR3
    1866MHz RAM | ATI Radeon HD 5770 1GB GDDR5
    60GB SSD
    (/) | 1TB 64MB Cache HDD (/home)
    Ubuntu Oneiric
    Ocelot 11.10 x64

  4. #4
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: Factorials in Java: Integer value larger than a long

    BigInteger is the standard Java way. Other libraris to implement arbitrary-precision integers exist http://en.wikipedia.org/wiki/Arbitra...etic#Libraries
    「明後日の夕方には帰ってるからね。」


  5. #5
    Join Date
    May 2009
    Location
    Canada
    Beans
    868
    Distro
    Ubuntu Development Release

    Re: Factorials in Java: Integer value larger than a long

    Quote Originally Posted by Bachstelze View Post
    BigInteger is the standard Java way. Other libraris to implement arbitrary-precision integers exist http://en.wikipedia.org/wiki/Arbitra...etic#Libraries
    I just don't particularly understand how to use BigIntegers. It doesn't seem to allow you to input values, all of it's methods are return methods. Some which let you add values to it's stored value then returns it. How could I go about using a BigInteger to calculate a factorial?
    Intel Core i7 970 6/12 (Cores/Threads) 3.2GHz 12MB Cache
    6GB DDR3
    1866MHz RAM | ATI Radeon HD 5770 1GB GDDR5
    60GB SSD
    (/) | 1TB 64MB Cache HDD (/home)
    Ubuntu Oneiric
    Ocelot 11.10 x64

  6. #6
    Join Date
    May 2009
    Beans
    522

    Re: Factorials in Java: Integer value larger than a long

    Quote Originally Posted by akand074 View Post
    I just don't particularly understand how to use BigIntegers. It doesn't seem to allow you to input values, all of it's methods are return methods. Some which let you add values to it's stored value then returns it. How could I go about using a BigInteger to calculate a factorial?
    Pass your desired number to the BigInteger constructor.

  7. #7
    Join Date
    May 2009
    Location
    Canada
    Beans
    868
    Distro
    Ubuntu Development Release

    Re: Factorials in Java: Integer value larger than a long

    Quote Originally Posted by unknownPoster View Post
    Pass your desired number to the BigInteger constructor.
    Oh I'm just reading up a bit more. Would I just have to pass it as a String? Because it didn't have any constructor set up for any int/long type value. So I guess I'll just send it as a String. I'll give that a try.
    Intel Core i7 970 6/12 (Cores/Threads) 3.2GHz 12MB Cache
    6GB DDR3
    1866MHz RAM | ATI Radeon HD 5770 1GB GDDR5
    60GB SSD
    (/) | 1TB 64MB Cache HDD (/home)
    Ubuntu Oneiric
    Ocelot 11.10 x64

  8. #8
    Join Date
    Jul 2007
    Location
    Austin, TX
    Beans
    Hidden!
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Factorials in Java: Integer value larger than a long

    Quote Originally Posted by akand074 View Post
    Oh I'm just reading up a bit more. Would I just have to pass it as a String? Because it didn't have any constructor set up for any int/long type value. So I guess I'll just send it as a String. I'll give that a try.
    Yep, the public constructor only takes a String. Technically there is another constructor that takes a long, but it's private.

  9. #9
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: Factorials in Java: Integer value larger than a long

    http://download.oracle.com/javase/6/...lang.String%29

    Passing a string works. Obviously you can't pass an int or a long since the value you want can't be represented as one. Another way is to use BigInteger.TEN and BigInteger.ONE. For example, to have the value 10^120 as a BigInteger:

    Code:
    BigInteger foo = BigInteger.ONE;
    for (int i=0; i<120; i++) {
        foo = foo.multiply(BigInteger.TEN);
    }
    And so on for other digits.
    「明後日の夕方には帰ってるからね。」


  10. #10
    Join Date
    May 2009
    Location
    Canada
    Beans
    868
    Distro
    Ubuntu Development Release

    Re: Factorials in Java: Integer value larger than a long

    Okay so that actually seemed to work and fixed that problem. Though how come I find that every time I fix one thing I break two others. Basically I had to change everything in the methods calling factorial to use a BigInteger, which worked fine when everything was a BigInteger. Seems to be causing me problems when I'm multiplying/dividing by doubles and such even though I converted the BigInteger to a double. Does anyone see anything wrong with either of these?

    Code:
    public static double calcProbability(int x, int n, double p) {
            return Factorial.calcFactorial(n).divide(
                    Factorial.calcFactorial(x).multiply(
                            Factorial.calcFactorial(n - x))).doubleValue()
                    * Math.pow(p, x) * Math.pow(1 - p, n - x);
        }
    Code:
    public static double calcProbability(int x, double lambda) {
            return new BigDecimal(String.valueOf((Math.pow(lambda, x) * Math.pow(
                    Math.E, lambda * -1)))).divide(
                    new BigDecimal((Factorial.calcFactorial(x)))).doubleValue();
        }
    Last edited by akand074; August 18th, 2011 at 08:59 PM. Reason: Used autoformater to satisfy Bachstelze
    Intel Core i7 970 6/12 (Cores/Threads) 3.2GHz 12MB Cache
    6GB DDR3
    1866MHz RAM | ATI Radeon HD 5770 1GB GDDR5
    60GB SSD
    (/) | 1TB 64MB Cache HDD (/home)
    Ubuntu Oneiric
    Ocelot 11.10 x64

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

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