Skip to content

Commit 086f118

Browse files
committed
P106: Added Java, Python, Mathematica, Haskell solutions.
1 parent 480330e commit 086f118

File tree

7 files changed

+87
-0
lines changed

7 files changed

+87
-0
lines changed

Answers.txt

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ Problem 102: 228
110110
Problem 103: 20313839404245
111111
Problem 104: 329468
112112
Problem 105: 73702
113+
Problem 106: 21384
113114
Problem 107: 259679
114115
Problem 108: 180180
115116
Problem 109: 38182

haskell/p106.hs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{-
2+
- Solution to Project Euler problem 106
3+
- Copyright (c) Project Nayuki. All rights reserved.
4+
-
5+
- https://www.nayuki.io/page/project-euler-solutions
6+
- https://github.com/nayuki/Project-Euler-solutions
7+
-}
8+
9+
import qualified EulerLib
10+
11+
12+
setSize = 12
13+
main = putStrLn (show ans)
14+
catalan n = div (EulerLib.binomial (n * 2) n) (n + 1)
15+
ans = sum [(EulerLib.binomial setSize (i * 2)) * ((div (EulerLib.binomial (i * 2) i) 2) - (catalan i))
16+
| i <- [2 .. (div setSize 2)]]

java/EulerTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public final class EulerTest {
117117
@Test public void testP103() { assertEquals("20313839404245" , new p103().run()); }
118118
@Test public void testP104() { assertEquals("329468" , new p104().run()); }
119119
@Test public void testP105() { assertEquals("73702" , new p105().run()); }
120+
@Test public void testP106() { assertEquals("21384" , new p106().run()); }
120121
@Test public void testP107() { assertEquals("259679" , new p107().run()); }
121122
@Test public void testP108() { assertEquals("180180" , new p108().run()); }
122123
@Test public void testP109() { assertEquals("38182" , new p109().run()); }

java/p106.java

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Solution to Project Euler problem 106
3+
* Copyright (c) Project Nayuki. All rights reserved.
4+
*
5+
* https://www.nayuki.io/page/project-euler-solutions
6+
* https://github.com/nayuki/Project-Euler-solutions
7+
*/
8+
9+
import java.math.BigInteger;
10+
11+
12+
public final class p106 implements EulerSolution {
13+
14+
public static void main(String[] args) {
15+
System.out.println(new p106().run());
16+
}
17+
18+
19+
private static final int SET_SIZE = 12;
20+
21+
public String run() {
22+
BigInteger ans = BigInteger.ZERO;
23+
for (int i = 2; i * 2 <= SET_SIZE; i++) {
24+
BigInteger x = Library.binomial(SET_SIZE, i * 2);
25+
BigInteger y = Library.binomial(i * 2, i).shiftRight(1);
26+
BigInteger z = Library.binomial(i * 2, i).divide(BigInteger.valueOf(i + 1)); // Catalan number
27+
ans = ans.add(x.multiply(y.subtract(z)));
28+
}
29+
return ans.toString();
30+
}
31+
32+
}

mathematica/p106.mathematica

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
(*
2+
* Solution to Project Euler problem 106
3+
* Copyright (c) Project Nayuki. All rights reserved.
4+
*
5+
* https://www.nayuki.io/page/project-euler-solutions
6+
* https://github.com/nayuki/Project-Euler-solutions
7+
*)
8+
9+
setSize = 12;
10+
CatalanNum[n_] := Binomial[n * 2, n] / (n + 1)
11+
Sum[Binomial[setSize, i * 2] * (Binomial[i * 2, i] / 2 - CatalanNum[i]),
12+
{i, 2, Floor[setSize / 2]}]

python/eulertest.py

+1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ def main():
129129
103: "20313839404245",
130130
104: "329468",
131131
105: "73702",
132+
106: "21384",
132133
107: "259679",
133134
108: "180180",
134135
109: "38182",

python/p106.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#
2+
# Solution to Project Euler problem 106
3+
# Copyright (c) Project Nayuki. All rights reserved.
4+
#
5+
# https://www.nayuki.io/page/project-euler-solutions
6+
# https://github.com/nayuki/Project-Euler-solutions
7+
#
8+
9+
import eulerlib
10+
11+
12+
def compute():
13+
SET_SIZE = 12
14+
15+
def catalan(n):
16+
return eulerlib.binomial(n * 2, n) // (n + 1)
17+
18+
ans = sum(eulerlib.binomial(SET_SIZE, i * 2) * (eulerlib.binomial(i * 2, i) // 2 - catalan(i))
19+
for i in range(2, SET_SIZE // 2 + 1))
20+
return str(ans)
21+
22+
23+
if __name__ == "__main__":
24+
print(compute())

0 commit comments

Comments
 (0)