georgimanov

Tribonacci

May 26th, 2014
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.86 KB | None | 0 0
  1. import java.math.BigInteger;
  2. import java.util.Scanner;
  3.  
  4. public class Tribonacci {
  5.     public static void main(String[] args) {
  6.         BigInteger tN, t1, t2, t3;
  7.         long element = 0;
  8.  
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.         t1 = scanner.nextBigInteger();
  12.         t2 = scanner.nextBigInteger();
  13.         t3 = scanner.nextBigInteger();
  14.         element = scanner.nextLong();
  15.  
  16.         tN = Tribonacci(t1, t2, t3, element);
  17.         System.out.println(tN);
  18.     }
  19.  
  20.     public static BigInteger Tribonacci(BigInteger t1, BigInteger t2, BigInteger t3, long element) {
  21.         BigInteger tN = null;
  22.        
  23.         if ( element < 4){
  24.             if ( element ==  1) { return t1;
  25.             }else if ( element == 2) { return t2;
  26.             }else if ( element == 3) { return t3;}
  27.             }else {
  28.                 for (int i = 0; i < element - 3; i++) {
  29.                     tN = t1.add(t2).add(t3);
  30.                     t1 = t2;
  31.                     t2 = t3;
  32.                     t3 = tN;
  33.                 }
  34.             }
  35.         return t3;
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment