Search code examples
javacompiler-errorsdynamic-programmingfibonacci

Compiler error: possible lossy conversion from long to int___Fibonacci


This is my first post here so if I made some mistakes I am sorry. Also, coding is not necessarily my thing, I am trying to get the hang of it and doing my best. So basically, I have to solve this problem using dynamic programming:

Triponacci is a series where the nth value is equal to the sum of the previous 3 values. The initial 3 values (base values) of our series are {0, 1, 2}.Note that the very first value in our series is 0th value. Output will be in the form of a statement: Triponacci(2) = 2 The value in the parenthesis is the input value n. The number to the right of the equals sign is the value of the nth element of the series. Triponacci(0) = 0 Triponacci(3) = 3

I thought, ok easy peasy Fibonacci with an extra step, right? Well... this is what I did:

static long[] storage;

public static long trip(int n)
{
    if(n<=2)
        return n;
    if(storage[n]<0)
        return storage[n];
    long result= trip(n-1) + trip(n-2)+trip(n-3);
    storage[n]= result;
    return result;
}

public static void main(String[]args)
{
    Scanner scan= new Scanner(System.in);
    long n = scan.nextLong();
    storage= new long[n+1];
    long res= trip(n);
    System.out.println(res);
}

At first it looked fine to me but when I compiled it threw multiple errors at me.

Triponacci.java:22: error: incompatible types: possible lossy conversion from long to int
storage= new long\[n+1\];
^
Triponacci.java:23: error: incompatible types: possible lossy conversion from long to int
long res= trip(n);
^

What should I do to make it work? Thank you in advance for your time and answers.

I thought I should use long instead of int due to boundaries issues. expected to work fine but well.


Solution

  • You're asking for a long which is a primitive type that can hold larger numbers than ints. If your intent is to actually allow the user to enter 'Triponacci(4000000000123451)' - then you have a much bigger problem, that's way too large and requires BigInteger and probably a better algorithm than this. Using long can make sense, but only for the outputs (the sums - the value of the storage array). NOT for the inputs.

    Note that in java arrays must have int indices, so in that sense, you need a much more complicated algorithm in any case if you really intend for the user to be able to enter a number beyond the confines of int (which are: plus 2 billion to minus 2 billion, approximately).

    NB: Your code has a bug; if storage[n] is less than 0? I think you meant more than 0.

    static long[] storage;
    public static long trip(int n) {
      if (n <= 2) return n;
      if (storage[n] != 0) return storage[n];
      return storage[n] = trip(n-1) + trip(n-2) + trip(n-3);
    }
    
    public static void main(String[]args) {
      Scanner scan = new Scanner(System.in);
      int n = scan.nextInt();
      storage = new long[n+1];
      long res = trip(n);
      System.out.println(res);
    }