diff --git a/README.rst b/README.rst index 3ec0bc7d..342d90b8 100644 --- a/README.rst +++ b/README.rst @@ -54,11 +54,11 @@ Olivia's Project Euler Solutions | | |gcc| |br| | | |Cp-Cov| |br| | | | C++14+ in: |msvc| [1]_ | | |CodeQL| | +------------+--------------------------+--------+-------------------+ -| C# | .NET 2+ | 12 | |C#i| |br| | +| C# | .NET 2+ | 13 | |C#i| |br| | | | | | |Cs-Cov| |br| | | | | | |CodeQL| | +------------+--------------------------+--------+-------------------+ -| JavaScript | Node 12+ |br| | 12 | |JavaScript| |br| | +| JavaScript | Node 12+ |br| | 13 | |JavaScript| |br| | | | Bun 1.0+ |br| | | |Js-Cov| |br| | | | Firefox [2]_ |br| | | |CodeQL| |br| | | | Chrome [2]_ | | |ESLint| | diff --git a/csharp/Euler.Test/test.cs b/csharp/Euler.Test/test.cs index 90bc00f0..c42536d4 100644 --- a/csharp/Euler.Test/test.cs +++ b/csharp/Euler.Test/test.cs @@ -18,6 +18,7 @@ public static IEnumerable Data() yield return new object[] { typeof(p0014), false, 837799 }; yield return new object[] { typeof(p0015), false, 137846528820 }; yield return new object[] { typeof(p0017), false, 21124 }; + yield return new object[] { typeof(p0034), false, 40730 }; yield return new object[] { typeof(p0076), true, 190569291 }; yield return new object[] { typeof(p0836), false, "aprilfoolsjoke" }; } diff --git a/csharp/Euler/p0034.cs b/csharp/Euler/p0034.cs new file mode 100644 index 00000000..d7938019 --- /dev/null +++ b/csharp/Euler/p0034.cs @@ -0,0 +1,42 @@ +/* +Project Euler Problem 34 + +This ended up being a filtering problem. The problem with my solution is that I +am not satisfied with my filter at all. I feel like there is a more efficient +way to go about it. + +Problem: + +145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. + +Find the sum of all numbers which are equal to the sum of the factorial of +their digits. + +Note: as 1! = 1 and 2! = 2 are not sums they are not included. +*/ +using System; + +namespace Euler +{ + public class p0034 : IEuler + { + public object Answer() + { + int answer = 0; + for (int x = 10; x < 100000; x += 1) + { + string xs = x.ToString(); + int sum = 0; + for (byte i = 0; i < xs.Length; i += 1) + { + sum += (int)Mathematics.Factorial((ulong)(xs[i] - '0')); + } + if (sum == x) + { + answer += x; + } + } + return answer; + } + } +} diff --git a/docs/c.rst b/docs/c.rst index b8baa66a..ff7f2a22 100644 --- a/docs/c.rst +++ b/docs/c.rst @@ -9,7 +9,6 @@ Library Code ------------ .. toctree:: - :glob: :numbered: :maxdepth: 1 diff --git a/docs/cplusplus.rst b/docs/cplusplus.rst index 1306afed..ab37e8ee 100644 --- a/docs/cplusplus.rst +++ b/docs/cplusplus.rst @@ -9,7 +9,6 @@ Library Code ------------ .. toctree:: - :glob: :numbered: :maxdepth: 1 diff --git a/docs/csharp.rst b/docs/csharp.rst index 5b2099b7..638f7977 100644 --- a/docs/csharp.rst +++ b/docs/csharp.rst @@ -53,7 +53,7 @@ Test Infrastructure .. csharp:class:: Euler.IEuler - .. csharp:method:: Task Answer() + .. csharp:method:: object Answer() .. csharp:namespace:: EulerTest @@ -63,6 +63,16 @@ Test Infrastructure An Xunit theory that, for each registered solution, will test against the known answer and a one minute time limit. +Library Code +------------ + +.. toctree:: + :numbered: + :maxdepth: 1 + + csharp/math + csharp/primes + Problems Solved --------------- @@ -71,4 +81,4 @@ Problems Solved :numbered: :maxdepth: 1 - csharp/p[0-9][0-9][0-9][0-9] + csharp/p[0-9][0-9][0-9][0-9] \ No newline at end of file diff --git a/docs/csharp/math.rst b/docs/csharp/math.rst new file mode 100644 index 00000000..cd16d947 --- /dev/null +++ b/docs/csharp/math.rst @@ -0,0 +1,16 @@ +math.cs +======= + +View source code `here on GitHub! `_ + +.. csharp:namespace:: Euler + +.. csharp:class:: Mathematics + + .. csharp:method:: static ulong Factorial(ulong n) + + .. csharp:method:: static ulong NChooseR(ulong n, ulong r) + +.. literalinclude:: ../../csharp/Euler/include/math.cs + :language: csharp + :linenos: diff --git a/docs/csharp/p0001.rst b/docs/csharp/p0001.rst index cf90ee7b..20d40eed 100644 --- a/docs/csharp/p0001.rst +++ b/docs/csharp/p0001.rst @@ -9,7 +9,7 @@ View source code `here on GitHub! Answer() + .. csharp:method:: object Answer() .. literalinclude:: ../../csharp/Euler/p0001.cs :language: csharp diff --git a/docs/csharp/p0002.rst b/docs/csharp/p0002.rst index 73a1faf2..c36044a5 100644 --- a/docs/csharp/p0002.rst +++ b/docs/csharp/p0002.rst @@ -9,7 +9,7 @@ View source code `here on GitHub! Answer() + .. csharp:method:: object Answer() .. literalinclude:: ../../csharp/Euler/p0002.cs :language: csharp diff --git a/docs/csharp/p0004.rst b/docs/csharp/p0004.rst index 670c51e7..16c365b0 100644 --- a/docs/csharp/p0004.rst +++ b/docs/csharp/p0004.rst @@ -9,7 +9,7 @@ View source code `here on GitHub! Answer() + .. csharp:method:: object Answer() .. literalinclude:: ../../csharp/Euler/p0004.cs :language: csharp diff --git a/docs/csharp/p0006.rst b/docs/csharp/p0006.rst index 276248ac..7e98b8b2 100644 --- a/docs/csharp/p0006.rst +++ b/docs/csharp/p0006.rst @@ -9,7 +9,7 @@ View source code `here on GitHub! Answer() + .. csharp:method:: object Answer() .. literalinclude:: ../../csharp/Euler/p0006.cs :language: csharp diff --git a/docs/csharp/p0008.rst b/docs/csharp/p0008.rst index b9c411c9..36a4f7f0 100644 --- a/docs/csharp/p0008.rst +++ b/docs/csharp/p0008.rst @@ -9,7 +9,7 @@ View source code `here on GitHub! Answer() + .. csharp:method:: object Answer() .. literalinclude:: ../../csharp/Euler/p0008.cs :language: csharp diff --git a/docs/csharp/p0009.rst b/docs/csharp/p0009.rst index 261a0dc3..f72d7d83 100644 --- a/docs/csharp/p0009.rst +++ b/docs/csharp/p0009.rst @@ -9,7 +9,7 @@ View source code `here on GitHub! Answer() + .. csharp:method:: object Answer() .. literalinclude:: ../../csharp/Euler/p0009.cs :language: csharp diff --git a/docs/csharp/p0011.rst b/docs/csharp/p0011.rst index f58a6f4c..17f26e67 100644 --- a/docs/csharp/p0011.rst +++ b/docs/csharp/p0011.rst @@ -9,7 +9,7 @@ View source code `here on GitHub! Answer() + .. csharp:method:: object Answer() .. literalinclude:: ../../csharp/Euler/p0011.cs :language: csharp diff --git a/docs/csharp/p0014.rst b/docs/csharp/p0014.rst index 7900aa13..91bb181f 100644 --- a/docs/csharp/p0014.rst +++ b/docs/csharp/p0014.rst @@ -9,7 +9,7 @@ View source code `here on GitHub! Answer() + .. csharp:method:: object Answer() .. literalinclude:: ../../csharp/Euler/p0014.cs :language: csharp diff --git a/docs/csharp/p0015.rst b/docs/csharp/p0015.rst index e4cece2e..d347e920 100644 --- a/docs/csharp/p0015.rst +++ b/docs/csharp/p0015.rst @@ -3,13 +3,21 @@ C# Implementation of Problem 15 View source code `here on GitHub! `_ +Includes +-------- + +- `math <./math.html>`_ + +Problem Solution +---------------- + .. csharp:namespace:: Euler .. csharp:class:: p0015 .. csharp:inherits:: Euler.IEuler - .. csharp:method:: Task Answer() + .. csharp:method:: object Answer() .. literalinclude:: ../../csharp/Euler/p0015.cs :language: csharp diff --git a/docs/csharp/p0017.rst b/docs/csharp/p0017.rst index 88bad731..6420f5c8 100644 --- a/docs/csharp/p0017.rst +++ b/docs/csharp/p0017.rst @@ -9,7 +9,7 @@ View source code `here on GitHub! Answer() + .. csharp:method:: object Answer() .. literalinclude:: ../../csharp/Euler/p0017.cs :language: csharp diff --git a/docs/csharp/p0076.rst b/docs/csharp/p0076.rst index d5e00968..16a6b1db 100644 --- a/docs/csharp/p0076.rst +++ b/docs/csharp/p0076.rst @@ -9,7 +9,7 @@ View source code `here on GitHub! Answer() + .. csharp:method:: object Answer() .. literalinclude:: ../../csharp/Euler/p0076.cs :language: csharp diff --git a/docs/csharp/p0836.rst b/docs/csharp/p0836.rst index f9db61c9..d3fc73ef 100644 --- a/docs/csharp/p0836.rst +++ b/docs/csharp/p0836.rst @@ -9,7 +9,7 @@ View source code `here on GitHub! Answer() + .. csharp:method:: object Answer() .. literalinclude:: ../../csharp/Euler/p0836.cs :language: csharp diff --git a/docs/index.rst b/docs/index.rst index a2a6a554..a020e909 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -191,7 +191,7 @@ Problems Solved +-----------+------------+------------+------------+------------+------------+------------+ |:prob:`33` | | | | |:py-d:`0033`| | +-----------+------------+------------+------------+------------+------------+------------+ -|:prob:`34` |:c-d:`0034` |:cp-d:`0034`| | |:py-d:`0034`|:rs-d:`0034`| +|:prob:`34` |:c-d:`0034` |:cp-d:`0034`|:cs-d:`0034`|:js-d:`0034`|:py-d:`0034`|:rs-d:`0034`| +-----------+------------+------------+------------+------------+------------+------------+ |:prob:`35` | | | | |:py-d:`0035`| | +-----------+------------+------------+------------+------------+------------+------------+ diff --git a/docs/javascript.rst b/docs/javascript.rst index fb76e986..b56e1b97 100644 --- a/docs/javascript.rst +++ b/docs/javascript.rst @@ -14,7 +14,6 @@ Library Code ------------ .. toctree:: - :glob: :numbered: :maxdepth: 1 diff --git a/docs/javascript/p0015.rst b/docs/javascript/p0015.rst index 2dc2abf0..f7061b21 100644 --- a/docs/javascript/p0015.rst +++ b/docs/javascript/p0015.rst @@ -3,6 +3,14 @@ JavaScript Implementation of Problem 15 View source code `here on GitHub! `_ +Includes +-------- + +- `math <./math.html>`_ + +Problem Solution +---------------- + .. js:autofunction:: p0015 .. literalinclude:: ../../javascript/src/p0015.js diff --git a/docs/javascript/p0034.rst b/docs/javascript/p0034.rst new file mode 100644 index 00000000..c1e0b11b --- /dev/null +++ b/docs/javascript/p0034.rst @@ -0,0 +1,18 @@ +JavaScript Implementation of Problem 34 +======================================= + +View source code `here on GitHub! `_ + +Includes +-------- + +- `math <./math.html>`_ + +Problem Solution +---------------- + +.. js:autofunction:: p0034 + +.. literalinclude:: ../../javascript/src/p0034.js + :language: javascript + :linenos: diff --git a/docs/python.rst b/docs/python.rst index 58b73d90..206903c9 100644 --- a/docs/python.rst +++ b/docs/python.rst @@ -9,7 +9,6 @@ Library Code ------------ .. toctree:: - :glob: :numbered: :maxdepth: 1 diff --git a/javascript/euler.test.js b/javascript/euler.test.js index fed7e98f..20711564 100644 --- a/javascript/euler.test.js +++ b/javascript/euler.test.js @@ -12,6 +12,7 @@ const answers = { 14: [require('./src/p0014.js'), false, 837799], 15: [require('./src/p0015.js'), false, 137846528820], 17: [require('./src/p0017.js'), false, 21124], + 34: [require('./src/p0034.js'), false, 40730], 76: [require('./src/p0076.js'), true, 190569291], 836: [require('./src/p0836.js'), false, 'aprilfoolsjoke'], }; diff --git a/javascript/src/p0034.js b/javascript/src/p0034.js new file mode 100644 index 00000000..4e44d85f --- /dev/null +++ b/javascript/src/p0034.js @@ -0,0 +1,34 @@ +/** + * Project Euler Problem 34 + * + * This ended up being a filtering problem. The problem with my solution is that I + * am not satisfied with my filter at all. I feel like there is a more efficient + * way to go about it. + * + * Problem: + * + * 145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. + * + * Find the sum of all numbers which are equal to the sum of the factorial of + * their digits. + * + * Note: as 1! = 1 and 2! = 2 are not sums they are not included. + * + * @return {number} + */ +exports.p0034 = function() { + let answer = 0; + for (let x = 10; x < 100000; x += 1) { + let xs = x.toString(); + let sum = 0; + for (let i = 0; i < xs.length; i += 1) { + sum += Mathematics.factorial(parseInt(xs[i])); + } + if (sum == x) { + answer += x; + } + } + return answer; +}; + +const Mathematics = require('./lib/math.js');