Last active
July 9, 2016 18:01
-
-
Save Krasnyanskiy/28262de987382b4948055f8a36381be1 to your computer and use it in GitHub Desktop.
-java: algo
This file contains hidden or 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.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.math.BigInteger; | |
import java.util.concurrent.Callable; | |
import java.util.concurrent.atomic.AtomicReference; | |
import static com.jayway.awaitility.Awaitility.await; | |
import static java.lang.Integer.parseInt; | |
import static java.lang.System.in; | |
import static java.math.BigInteger.ONE; | |
import static java.math.BigInteger.ZERO; | |
import static java.util.concurrent.TimeUnit.MILLISECONDS; | |
/** | |
* @author Alexander Krasniansky | |
*/ | |
public class Fib { | |
public static void main(String[] args) throws IOException { | |
BufferedReader reader = new BufferedReader(new InputStreamReader(in)); | |
final int n = parseInt(reader.readLine()); | |
final AtomicReference<BigInteger> res = new AtomicReference<>(); | |
await().atMost(100, MILLISECONDS).until(new Callable<Boolean>() { | |
public Boolean call() throws Exception { | |
res.set(fib(n)); | |
return true; | |
} | |
}); | |
String resultAsString = res.get().toString(); | |
int length = resultAsString.length(); | |
System.out.println(resultAsString.charAt(length - 1)); | |
reader.close(); | |
} | |
private static BigInteger fib(int n) { | |
BigInteger[] ints = new BigInteger[n + 1]; | |
ints[0] = ZERO; | |
ints[1] = ONE; | |
for (int i = 2; i <= n; i++) { | |
ints[i] = ints[i - 1].add(ints[i - 2]); | |
ints[i - 2] = null; | |
} | |
return ints[ints.length - 1]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Solution for: https://stepic.org/lesson/%D0%A7%D0%B8%D1%81%D0%BB%D0%B0-%D0%A4%D0%B8%D0%B1%D0%BE%D0%BD%D0%B0%D1%87%D1%87%D0%B8-13228/step/7?course=%D0%90%D0%BB%D0%B3%D0%BE%D1%80%D0%B8%D1%82%D0%BC%D1%8B-%D1%82%D0%B5%D0%BE%D1%80%D0%B8%D1%8F-%D0%B8-%D0%BF%D1%80%D0%B0%D0%BA%D1%82%D0%B8%D0%BA%D0%B0-%D0%9C%D0%B5%D1%82%D0%BE%D0%B4%D1%8B&unit=3414
It does not work (Time Limit).