Skip to content

Commit f55f34e

Browse files
authored
feat(6-kyu): kata/pell-numbers (#382)
1 parent 77263ed commit f55f34e

File tree

5 files changed

+53
-2
lines changed

5 files changed

+53
-2
lines changed

docs/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Codewars Handbook ☕️🚀
22

33
[![Views statistics +1 👀](https://img.shields.io/badge/dynamic/xml?color=success&label=views&query=//*[name()=%27text%27][3]&url=https://hits.seeyoufarm.com/api/count/incr/badge.svg?url=https%3A%2F%2Fgithub.com%2FParanoidUser%2Fcodewars-handbook)](https://hits.seeyoufarm.com/api/count/graph/dailyhits.svg?url=https://github.com/ParanoidUser/codewars-handbook)
4-
[![Solved kata 👌](https://img.shields.io/badge/solved%20kata-1344-red.svg)](https://www.codewars.com/kata/search/java)
4+
[![Solved kata 👌](https://img.shields.io/badge/solved%20kata-1345-red.svg)](https://www.codewars.com/kata/search/java)
55
[![CI pipeline 🛠](https://img.shields.io/github/actions/workflow/status/ParanoidUser/codewars-handbook/build.yml?branch=main)](https://github.com/ParanoidUser/codewars-handbook/actions/workflows/build.yml)
66
[![Quality gate 🔎](https://img.shields.io/sonar/alert_status/codewars-handbook?server=https%3A%2F%2Fsonarcloud.io)](https://sonarcloud.io/dashboard?id=codewars-handbook)
77
[![Let's have a chat! 📞](https://img.shields.io/gitter/room/ParanoidUser/codewars-handbook?color=49c39e)](https://gitter.im/ParanoidUser/codewars-handbook)
@@ -25,7 +25,7 @@ slug.
2525

2626
| [1 kyu](/kata/1-kyu/index.md) | [2 kyu](/kata/2-kyu/index.md) | [3 kyu](/kata/3-kyu/index.md) | [4 kyu](/kata/4-kyu/index.md) | [5 kyu](/kata/5-kyu/index.md) | [6 kyu](/kata/6-kyu/index.md) | [7 kyu](/kata/7-kyu/index.md) | [8 kyu](/kata/8-kyu/index.md) | [beta](/kata/beta/index.md) | [retired](/kata/retired/index.md) |
2727
|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:---------------------------:|:---------------------------------:|
28-
| - | 1 | 2 | 18 | 36 | 409 | 545 | 201 | 59 | 73 |
28+
| - | 1 | 2 | 18 | 36 | 410 | 545 | 201 | 59 | 73 |
2929

3030
**Note:** The source code is written in Java 17 and may use language features that are incompatible
3131
with Java 8, 11.

kata/6-kyu/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@
252252
- [Password Maker](password-maker-1)
253253
- [Paths in the Grid](paths-in-the-grid)
254254
- [Peel the onion](peel-the-onion)
255+
- [Pell Numbers](pell-numbers)
255256
- [Pentabonacci](pentabonacci)
256257
- [Persistent Bugger](persistent-bugger)
257258
- [PhoneWords](phonewords-1)

kata/6-kyu/pell-numbers/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# [Pell Numbers](https://www.codewars.com/kata/pell-numbers "https://www.codewars.com/kata/5818d00a559ff57a2f000082")
2+
3+
The [Pell sequence](https://en.wikipedia.org/wiki/Pell_number) is the sequence of integers defined by the initial values
4+
5+
```
6+
P(0) = 0, P(1) = 1
7+
```
8+
9+
and the recurrence relation
10+
11+
```
12+
P(n) = 2 * P(n-1) + P(n-2)
13+
```
14+
15+
The first few values of `P(n)` are
16+
17+
```
18+
0, 1, 2, 5, 12, 29, 70, 169, 408, 985, 2378, 5741, 13860, 33461, 80782, 195025, 470832, ...
19+
```
20+
21+
## Task
22+
23+
Your task is to write a method that returns `n`th Pell number
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import java.math.BigInteger;
2+
3+
import static java.math.BigInteger.ONE;
4+
import static java.math.BigInteger.TWO;
5+
import static java.util.stream.Stream.iterate;
6+
7+
interface Solution {
8+
static BigInteger pell(int n) {
9+
return iterate(new BigInteger[]{ONE, TWO}, i -> new BigInteger[]{i[1], i[1].add(i[1]).add(i[0])})
10+
.skip(n - 1L).findFirst().orElseThrow()[0];
11+
}
12+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import java.math.BigInteger;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
7+
class SolutionTest {
8+
@Test
9+
void sample() {
10+
assertEquals(BigInteger.ONE, Solution.pell(1));
11+
assertEquals(BigInteger.TWO, Solution.pell(2));
12+
assertEquals(BigInteger.valueOf(5), Solution.pell(3));
13+
assertEquals(BigInteger.valueOf(12), Solution.pell(4));
14+
}
15+
}

0 commit comments

Comments
 (0)