Commit 086f118c authored by Nayuki's avatar Nayuki

P106: Added Java, Python, Mathematica, Haskell solutions.

parent 480330e3
......@@ -110,6 +110,7 @@ Problem 102: 228
Problem 103: 20313839404245
Problem 104: 329468
Problem 105: 73702
Problem 106: 21384
Problem 107: 259679
Problem 108: 180180
Problem 109: 38182
......
{-
- Solution to Project Euler problem 106
- Copyright (c) Project Nayuki. All rights reserved.
-
- https://www.nayuki.io/page/project-euler-solutions
- https://github.com/nayuki/Project-Euler-solutions
-}
import qualified EulerLib
setSize = 12
main = putStrLn (show ans)
catalan n = div (EulerLib.binomial (n * 2) n) (n + 1)
ans = sum [(EulerLib.binomial setSize (i * 2)) * ((div (EulerLib.binomial (i * 2) i) 2) - (catalan i))
| i <- [2 .. (div setSize 2)]]
......@@ -117,6 +117,7 @@ public final class EulerTest {
@Test public void testP103() { assertEquals("20313839404245" , new p103().run()); }
@Test public void testP104() { assertEquals("329468" , new p104().run()); }
@Test public void testP105() { assertEquals("73702" , new p105().run()); }
@Test public void testP106() { assertEquals("21384" , new p106().run()); }
@Test public void testP107() { assertEquals("259679" , new p107().run()); }
@Test public void testP108() { assertEquals("180180" , new p108().run()); }
@Test public void testP109() { assertEquals("38182" , new p109().run()); }
......
/*
* Solution to Project Euler problem 106
* Copyright (c) Project Nayuki. All rights reserved.
*
* https://www.nayuki.io/page/project-euler-solutions
* https://github.com/nayuki/Project-Euler-solutions
*/
import java.math.BigInteger;
public final class p106 implements EulerSolution {
public static void main(String[] args) {
System.out.println(new p106().run());
}
private static final int SET_SIZE = 12;
public String run() {
BigInteger ans = BigInteger.ZERO;
for (int i = 2; i * 2 <= SET_SIZE; i++) {
BigInteger x = Library.binomial(SET_SIZE, i * 2);
BigInteger y = Library.binomial(i * 2, i).shiftRight(1);
BigInteger z = Library.binomial(i * 2, i).divide(BigInteger.valueOf(i + 1)); // Catalan number
ans = ans.add(x.multiply(y.subtract(z)));
}
return ans.toString();
}
}
(*
* Solution to Project Euler problem 106
* Copyright (c) Project Nayuki. All rights reserved.
*
* https://www.nayuki.io/page/project-euler-solutions
* https://github.com/nayuki/Project-Euler-solutions
*)
setSize = 12;
CatalanNum[n_] := Binomial[n * 2, n] / (n + 1)
Sum[Binomial[setSize, i * 2] * (Binomial[i * 2, i] / 2 - CatalanNum[i]),
{i, 2, Floor[setSize / 2]}]
......@@ -129,6 +129,7 @@ ANSWERS = {
103: "20313839404245",
104: "329468",
105: "73702",
106: "21384",
107: "259679",
108: "180180",
109: "38182",
......
#
# Solution to Project Euler problem 106
# Copyright (c) Project Nayuki. All rights reserved.
#
# https://www.nayuki.io/page/project-euler-solutions
# https://github.com/nayuki/Project-Euler-solutions
#
import eulerlib
def compute():
SET_SIZE = 12
def catalan(n):
return eulerlib.binomial(n * 2, n) // (n + 1)
ans = sum(eulerlib.binomial(SET_SIZE, i * 2) * (eulerlib.binomial(i * 2, i) // 2 - catalan(i))
for i in range(2, SET_SIZE // 2 + 1))
return str(ans)
if __name__ == "__main__":
print(compute())
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment