2
 public static double jackpotChance(int k, int highestNumber, int m)
    {
        long temp = 1;
        long tempK = 1;
        for(int i = 0; i < k; i++)
        {
            temp = temp * (highestNumber-i);
            System.out.println(temp);
        }
        for(int u = 0; u < k; u++)
        {
            tempK = tempK * (k-u);
            System.out.println(tempK);
        }
        double jackpotChance;
        jackpotChance = (temp/tempK)*m;
        return jackpotChance;
    }

Going above ~30 for highestNumber will give a false number for temp thus giving me a false number for jackpotChance. I have heard of BigNumber in the java.math class, and I tried to use that, but it wouldn't let me convert to between number types for some reason. Any advice would be much appreciated. Yes this is a homework problem, so if you can give advice without giving the answer that'd be great thanks :)

edit: added the BigInteger for reference. The error I'm getting is it can't convert the 1 into a BigInteger.

 public static BigInteger jackpotChance(int k, int highestNumber, int m)
    {
        BigInteger temp = 1;
        BigInteger tempK = 1;
        for(int i = 0; i < k; i++)
        {
            temp = temp * (highestNumber-i);
            System.out.println(temp);
        }
         for(int u = 0; u < k; u++)
        {
            tempK = tempK * (k-u);
            System.out.println(tempK);
        }
        BigDecimal jackpotChance;
        jackpotChance = (temp/tempK)*m;
        return jackpotChance;
    }
Andrew
  • 21
  • 6

2 Answers2

1

Why don't you use doubles too for temp and tempk? That would solve your problem.

Aimert
  • 146
  • 6
  • Is this an answer or a comment? – Yassin Hajaj Nov 15 '15 at 23:41
  • does double store more digits than long? I thought they were the same just one could store decimals and one couldn't. – Andrew Nov 15 '15 at 23:43
  • @YassinHajaj guess it's a comment. Sorry, I'm fairly new here and i wasn't able to comment a while ago. Andrew you can check that out by looking at these values: System.out.println(Long.MAX_VALUE); System.out.println(Double.MAX_VALUE); – Aimert Nov 15 '15 at 23:45
0

Working with big numbers, I think the best option if you're not using decimals is to use BigInteger.

It has a special BigInteger#multiply method that would solve your problem here.

Do not forget that a BigInteger object is immutable so when performing an operation on it, do not forget to assign its value to a variable.


Example

BigInteger big = new BigInteger("10");
big = big.multiply(new BigInteger("20")); // value of big = 200;
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89