-3
BigInteger b = BigInteger.valueOf(calcFib(Integer.parseInt(args[0])));

I try to run this to create a new BigInteger for a large Fibonacci number, but whenever I do, this error gets thrown:

error: cannot find symbol

    BigInteger b = BigInteger.valueOf(calcFib(Integer.parseInt(args[0])));
    ^
symbol: class BigInteger

location: class FibonacciCalculator 

I've imported java.lang.Object if that helps. Am still new to programming so please don't judge :P

Edit: the original is done in main, however now I'm getting a new problem with this line of code after importing java.math.BigInteger

public static BigInteger calcFib (int n)
{
    if(n == 1 || n == 2)
        return 1;
    else
    {
        return calcFib(n-1) + calcFib(n-2);
    }
}

both parts of the if/else statements throw errors, the if says int can 't be converted to BigInteger, and the else says bad operand types. Again sorry for my ignorance and thanks for the help!

0xdb
  • 3,539
  • 1
  • 21
  • 37
Darth7urtle
  • 1
  • 1
  • 4

2 Answers2

2

BigInteger class is defined inside java.math.BigInteger so try to import java.math.BigInteger.

Shiv Kumar
  • 1,034
  • 1
  • 9
  • 21
1

In the second part of your question, you need to change the code slightly if you are using BinIntegers rather than the int or long types, as you can't use the usual + operator or automatically convert between int and BigInteger.

Revised code should look like this:

public static BigInteger calcFib(int n)
{
    if(n == 1 || n == 2)
        return BigInteger.ONE;
    else
    {
        return calcFib(n - 1).add(calcFib(n - 2));
    }
}

In an ideal world, you would probably also check for zero and negative numbers and handle these by throwing an exception.

clstrfsck
  • 14,715
  • 4
  • 44
  • 59
  • Thanks! Worked fine after that, now for the long haul of making my computer find the 999th fibonacci number. :P – Darth7urtle Oct 18 '17 at 04:12
  • Hi @Darth7urtle, I think you will be waiting approximately forever to calculate the 999th number using the branching recursive algorithm, as this will need approximately 2^999 (= approx 10^300) additions. See if you can find a linear time complexity fib. – clstrfsck Oct 18 '17 at 05:47
  • damn I never actually did the calculation for how many additions, thanks for the heads up! @msandiford – Darth7urtle Oct 19 '17 at 05:16