Skip to content

Commit 9588ce9

Browse files
committed
1 parent fee25db commit 9588ce9

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

kata/3-kyu/the-millionth-fibonacci-kata/README.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
# [The Millionth Fibonacci Kata](https://www.codewars.com/kata/the-millionth-fibonacci-kata "https://www.codewars.com/kata/53d40c1e2f13e331fc000c26")
22

3-
The year is 1214. One night, Pope Innocent III awakens to find the the archangel Gabriel floating before him. Gabriel thunders to the pope:
3+
The year is 1214. One night, Pope Innocent III awakens to find the archangel Gabriel floating before him. Gabriel thunders to the pope:
44

5-
> Gather all of the learned men in Pisa, especially Leonardo Fibonacci. In order for the crusades in the holy lands to be successful, these men must calculate the *millionth* number in Fibonacci's recurrence. Fail to do this, and your armies will never reclaim the holy land. It is His will.
5+
> Gather all the learned men in Pisa, especially Leonardo Fibonacci. In order for the crusades in the holy lands to be successful, these
6+
> men must calculate the *millionth* number in Fibonacci's recurrence. Fail to do this, and your armies will never reclaim the holy land. It
7+
> is His will.
68
79
The angel then vanishes in an explosion of white light.
810

9-
Pope Innocent III sits in his bed in awe. *How much is a million?* he thinks to himself. He never was very good at math.
11+
Pope Innocent III sits in his bed in awe. *How much is a million?* he thinks to himself. He was never very good at math.
1012

11-
He tries writing the number down, but because everyone in Europe is still using Roman numerals at this moment in history, he cannot represent this number. If he only knew about the invention of zero, it might make this sort of thing easier.
13+
He tries writing the number down, but because everyone in Europe is still using Roman numerals at this moment in history, he cannot
14+
represent this number. If he only knew about the invention of zero, it might make this sort of thing easier.
1215

13-
He decides to go back to bed. He consoles himself, *The Lord would never challenge me thus; this must have been some deceit by the devil. A pretty horrendous nightmare, to be sure.*
16+
He decides to go back to bed. He consoles himself, *The Lord would never challenge me thus; this must have been some deceit by the devil. A
17+
pretty horrendous nightmare, to be sure.*
1418

1519
Pope Innocent III's armies would go on to conquer Constantinople (now Istanbul), but they would never reclaim the holy land as he desired.
1620

@@ -26,6 +30,8 @@ Write an algorithm that can handle `n` up to `2000000`.
2630

2731
Your algorithm must output the exact integer answer, to full precision. Also, it must correctly handle negative numbers as input.
2832

29-
**HINT I**: Can you rearrange the equation `fib(n + 2) = fib(n + 1) + fib(n)` to find `fib(n)` if you already know `fib(n + 1)` and `fib(n + 2)`? Use this to reason what value `fib` has to have for negative values.
33+
**HINT I**: Can you rearrange the equation `fib(n + 2) = fib(n + 1) + fib(n)` to find `fib(n)` if you already know `fib(n + 1)` and
34+
`fib(n + 2)`? Use this to reason what value `fib` has to have for negative values.
3035

31-
**HINT II**: See https://web.archive.org/web/20220614001843/https://mitpress.mit.edu/sites/default/files/sicp/full-text/book/book-Z-H-11.html#%_sec_1.2.4
36+
**HINT II**:
37+
See https://web.archive.org/web/20220614001843/https://mitpress.mit.edu/sites/default/files/sicp/full-text/book/book-Z-H-11.html#%_sec_1.2.4
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import static java.math.BigInteger.*;
2+
3+
import java.math.BigInteger;
4+
5+
interface Fibonacci {
6+
static BigInteger fib(BigInteger n) {
7+
BigInteger result = fib(ONE, ZERO, ZERO, ONE, n.abs());
8+
return n.signum() < 0 && !n.testBit(0) ? result.negate() : result;
9+
}
10+
11+
private static BigInteger fib(BigInteger a, BigInteger b, BigInteger p, BigInteger q, BigInteger n) {
12+
return n.equals(ZERO) ? b :
13+
!n.testBit(0) ? fib(a, b, p.pow(2).add(q.pow(2)), p.multiply(q.multiply(TWO)).add(q.pow(2)), n.divide(TWO)) :
14+
fib(b.multiply(q).add(a.multiply(q)).add(a.multiply(p)), b.multiply(p).add(a.multiply(q)), p, q, n.subtract(ONE));
15+
}
16+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import static org.junit.jupiter.api.Assertions.assertEquals;
2+
3+
import java.math.BigInteger;
4+
import org.junit.jupiter.params.ParameterizedTest;
5+
import org.junit.jupiter.params.provider.CsvSource;
6+
7+
class FibonacciTest {
8+
@ParameterizedTest
9+
@CsvSource(textBlock = """
10+
0, 0
11+
1, 1
12+
2, 1
13+
3, 2
14+
4, 3
15+
5, 5
16+
""")
17+
void sample(BigInteger input, BigInteger expected) {
18+
assertEquals(expected, Fibonacci.fib(input));
19+
}
20+
}

0 commit comments

Comments
 (0)