1

I am working on a program that is supposed to return the position of a given number along the Fibonacci Sequence.

Simple enough, but the test-cases on Codeabbey are over 100 digits long. This is more than the long primitive data type can handle. I know I need to use BigInteger, but I am not sure how to implement it into my code. I read that BigInteger is immutable? What does this mean?

Here is my code:

import java.util.Scanner;
class codeabbey67
{
    public static void main(String[] Args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Sets: ");
        int sets = input.nextInt();
        long A[] = new long[sets];
        for(int i = 0; i<sets; i++)
        {
            long f = 0;
            long s = 1;
            long next = 0;
            long j = 0;
            System.out.print("\nVal: ");
            long val = input.nextLong();
            while(next != val)
            {
                if(j<= 1)
                {
                    next = 1;
                    j++;
                }
                next = f+s;
                f = s;
                s = next;
                j++;
            }
            A[i] = j;
        }
        System.out.println("\nRESULTS: ");
        for(int j = 0; j<A.length; j++)
            System.out.print(A[j] + " ");
    }
}

EDIT: Here is my updated code with BigInteger. Still no luck.

import java.util.Scanner;
import java.math.BigInteger;

class codeabbey67
{
    public static void main(String[] Args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("\n\nSets: ");
        int sets = input.nextInt();
        int A[] = new int[sets];
        for(int i = 0; i<sets; i++)
        {
            BigInteger f = BigInteger.ZERO;
            BigInteger s = BigInteger.ONE;
            BigInteger next = BigInteger.ZERO;
            BigInteger j = BigInteger.ZERO;
            System.out.print("\nVAL: ");
            BigInteger val = input.nextBigInteger();
            int x = 0;
            while(!next.equals(val) && x!= 1000) //until current value at position in sequence equals desired value 
            {
                if(x<= 1)
                {
                    next = BigInteger.ONE;
                    x++;
                }
                next = f.add(s);
                s=next;
                x++;
            }
            A[i] = x;
        }
        for(int y = 0; y<A.length; y++)
            System.out.print(A[y] + " ");
    }
}

EDIT: Figured it out. Thanks for all of the help!

JDI
  • 19
  • 4

2 Answers2

1

BigInteger comes with methods that can be used to modify the numerical value stored within it. This may be useful to learn how to use BigInteger.

Immutable means that you cannot modify an existing object, you can only create a new one. Think of a class such as java.awt.Color: none of the fields of that class are editable, thus it is immutable. Another example would be the String class.

Because the BigInteger method operations e.g. add (), subtract (), etc. all return a BigInteger object containing the new value after the said operation, you can reassign an existing BigInteger reference variable to the BigInteger object returned by an operation like thus:

BigInteger sum = new BigInteger ("0", 10);
sum = sum.add (new BigInteger ("123", 10)); //sum’s value is now 123

In your case, since you are already using a long, you can use the BigInteger method valueOf (), which accepts a long parameter and returns a BigInteger object consisting of the long value. E.g.

BigInteger sum = BigInteger.valueOf (123);//sum’s value is now set to 123
Community
  • 1
  • 1
  • @NolanHamilton There isn’t really a way around using the BigInteger methods to perform operations, so there isn’t much you can do there. On a side note, you can use the compareTo () method in the BigInteger class to compare BigInteger objects. Just mentioning it in case you weren’t already doing so. –  Aug 11 '15 at 02:36
  • I'm aware. Thanks anyways. – JDI Aug 11 '15 at 02:57
0

Like @dabigone said, you can use BigInteger instead of implementing it yourself, I try to change your code using BigInteger for this Codeabbey67 as :

public class Codeabbey67 {
    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int N = in.nextInt();
    while (N-- > 0) {
        BigInteger bi = new BigInteger(in.next());
        BigInteger i = new BigInteger("0");
        BigInteger j = new BigInteger("1");
        int idx = 0;
        while (!i.equals(bi) && idx <= 1000) {
            j = i.add(j);
            i = j.subtract(i);
            idx++;
        }
        System.out.print(idx + " ");
    }
}
licc
  • 1
  • 1
  • 1
    Thank you. Out of curiosity, why might the test-cases be so damn long? I can't think of any reason why they couldn't just be 5-10 digits. – JDI Aug 11 '15 at 02:53
  • @NolanHamilton It seems no need to output the "\n\nSets: " and "\nVAL: " as my understanding of the Accepted result. Also, seems no need to store each index with an array, just print them one by one separated by space. – licc Aug 11 '15 at 09:11