Created
May 27, 2019 20:21
-
-
Save josehenriqueventura/7011df4341f11f49ea194799c4a22673 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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