Task 67 - Test-cases exceed range of long type (java)

Back to Problem Solutions forum

nolanh08     2015-08-11 02:28:44
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] + " ");
}
}

Disregard first post.

Just curious, why do the test-cases have to be so long?

Rodion (admin)     2015-08-11 03:16:29
User avatar

Hi! Thanks for your question!

They are long mainly to help learning handling such a long values (which is not uncommon in industry). In Java you can use BigInteger for them.

However you can also solve this problem using no larger values than usual int-s or long-s :)

nolanh08     2015-08-11 03:39:43

Thank you for the quick reply! By the way, I LOVE Codeabbey. I started programming a couple of weeks ago. I have learned more than I thought would be possible because of this site.

Anyways...I tried implementing BigInteger into my code:

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] + " ");
}
}

Still no luck. The program just keeps iterating until 1000. Any ideas why this might be?

Thanks again!

Rodion (admin)     2015-08-11 04:35:22
User avatar

Hi! Thank you for your kind words. The site never can be what it is without its patient users :)

I added a line printing out next for debugging:

http://ideone.com/rut1lO

As you may see, next just is not changed - perhaps there is some mess with s and f... :(

I believe you will be able to continue this investigation - for it seems for "couple of weeks" in programming you do significant progress - my congratulations! :)

nolanh08     2015-08-11 17:05:47

Figured it out.

Here's my updated code in case you're curious. I appreciate the help. The only roadblock was figuring out how to implement BigInteger.

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++;
            }
            if(x> 1)
            {

                next = f.add(s);
                f = s;
                s= next;
                x++;
            }
        }
        if(x > 0)
        {

        A[i] = x-1;

        }
        else if(x == 0)
        {
            A[i] = x;
        }

    }
    for(int y = 0; y<A.length; y++)
        System.out.print(A[y] + " ");
}
}
Please login and solve 5 problems to be able to post at forum