Aditya kumar
Aditya kumar

Reputation: 76

Dynamic programming Very large data value

  import java.io.*;
 import java.util.*;

 public class Solution {

public static void main(String[] args) {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */

    Scanner s= new Scanner(System.in);
    int t1=s.nextInt();
    int t2= s.nextInt();
    int n= s.nextInt();
    double arr[]= new double[20];
    for(int i=0;i<20;i++){
    arr[i]=-1;

    }
    arr[1]= t1;
    arr[2]=t2;

    if(arr[n]!=-1){
        System.out.println((long)arr[n]);
    }

    else{
        for(int i=3;i<=n;i++){
           arr[i]= arr[i-2] + Math.pow(arr[i-1],2);
        }
        System.out.println((long)arr[n]);
    }



}
    }

This code is a modified Fibonacci series. I want to calculate the tenth digit in this sequence. But the result is very big I want to ask in which type I should cast the answer ? I have used long but it failed...please suggest any other type ...

Upvotes: 0

Views: 176

Answers (1)

kremerd
kremerd

Reputation: 1636

There's a special type for this kind of computation called BigInteger. Find more infos here.

Edit:

You can create a BigInteger from a String

BigInteger bigInt = new BigInteger("24");

or from an integer type

BigInteger fromInt = BigInteger.valueOf(24);

Upvotes: 1

Related Questions