Skip to content

Instantly share code, notes, and snippets.

@josehenriqueventura
Created May 27, 2019 20:21
Show Gist options
  • Save josehenriqueventura/7011df4341f11f49ea194799c4a22673 to your computer and use it in GitHub Desktop.
Save josehenriqueventura/7011df4341f11f49ea194799c4a22673 to your computer and use it in GitHub Desktop.
import java.io.*;
import java.math.*;
import java.util.*;
public class Solution {
private static BigInteger fibonacci(BigInteger count){
if(count.compareTo(BigInteger.ZERO) <= 0){
return BigInteger.ZERO;
}else if(count.compareTo(BigInteger.ONE) == 0){
return BigInteger.ONE;
}
BigInteger previous=BigInteger.ZERO;
BigInteger current=BigInteger.ONE;
BigInteger next=BigInteger.ZERO;
for(int i=0 ; i<=count.intValue() ; i++){
previous = current;
current = next;
next = previous.add(current);
}
return current;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(scanner.hasNextInt()){
int n = scanner.nextInt();
System.out.println(fibonacci(BigInteger.valueOf(n)));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment