Home > OS >  Fibonacci numbers java.lang.OutOfMemoryError: Java heap space
Fibonacci numbers java.lang.OutOfMemoryError: Java heap space

Time:10-15

I try to add Fibonacci numbers to List and get:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

I need this numbers in a List or in array, to manipulate it further.

Here is my code:

    int a;
    int b = 0;
    int c = 1;

    int prod = 2932589879121
        

    List<Integer> list = new ArrayList<>();
      for (int i = 0; i < prod; i  ) {
            a = b;
            b = c;
            c = a   b;
            list.add(c);
        }

How can I fix it?

My full code:

public static long[] productFib(long prod) {
        long[] result = new long[3];

        int a;
        int b = 0;
        int c = 1;

        int first = 0;
        int second = 0;


        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < prod; i  ) {
            a = b;
            b = c;
            c = a   b;
            list.add(c);
        }

        int index = 0;
        while (true) {
            first = list.get(index);
            second = list.get(index   1);
            if (first * second == prod) {
                result[0] = list.get(index);
                result[1] = list.get(index   1);
                result[2] = 1;
                break;
            } else if (first * second > prod) {
                result[0] = list.get(index);
                result[1] = list.get(index   1);
                result[2] = 0;
                break;
            }
            index  ;
        }

        return result;

    }

I need to find if multiplication of two fibonacci numbers from list give another fibonacci number. Work fine for small numbers.

Change code to:

public static long[] productFib(long prod) {
    long[] result = new long[3];

    long a;
    long b = 0;
    long c = 1;

    for (int i = 0; i < prod; i  ) {
        a = b;
        b = c;
        c = a   b;
        if (a * b == prod) {
            result[0] = a;
            result[1] = b;
            result[2] = 1;
            break;
        } else if (a * b > prod) {
            result[0] = a;
            result[1] = b;
            result[2] = 0;
            break;
        }
    }

    return result;
}

Work fine

CodePudding user response:

The value of prod is out of range. In java int (signed) has the range of -2147483648 to 2147483647. This should throw a compilation error "integer number too large".

CodePudding user response:

Work fine:

public static long[] productFib(long prod) {
    long[] result = new long[3];

    long a;
    long b = 0;
    long c = 1;

    for (int i = 0; i < prod; i  ) {
        a = b;
        b = c;
        c = a   b;
        if (a * b == prod) {
            result[0] = a;
            result[1] = b;
            result[2] = 1;
            break;
        } else if (a * b > prod) {
            result[0] = a;
            result[1] = b;
            result[2] = 0;
            break;
        }
    }

    return result;
}
  • Related