Skip to content

Commit

Permalink
Solve p34 in C#, JS
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Aug 1, 2024
1 parent 0c30fa4 commit e421f36
Show file tree
Hide file tree
Showing 26 changed files with 155 additions and 21 deletions.
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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| |
Expand Down
1 change: 1 addition & 0 deletions csharp/Euler.Test/test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static IEnumerable<object[]> 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" };
}
Expand Down
42 changes: 42 additions & 0 deletions csharp/Euler/p0034.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
1 change: 0 additions & 1 deletion docs/c.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ Library Code
------------

.. toctree::
:glob:
:numbered:
:maxdepth: 1

Expand Down
1 change: 0 additions & 1 deletion docs/cplusplus.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ Library Code
------------

.. toctree::
:glob:
:numbered:
:maxdepth: 1

Expand Down
14 changes: 12 additions & 2 deletions docs/csharp.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Test Infrastructure
.. csharp:class:: Euler.IEuler
.. csharp:method:: Task<object> Answer()
.. csharp:method:: object Answer()
.. csharp:namespace:: EulerTest
Expand All @@ -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
---------------

Expand All @@ -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]
16 changes: 16 additions & 0 deletions docs/csharp/math.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
math.cs
=======

View source code `here on GitHub! <https://github.com/LivInTheLookingGlass/Euler/blob/master/csharp/include/math.cs>`_

.. 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:
2 changes: 1 addition & 1 deletion docs/csharp/p0001.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ View source code `here on GitHub! <https://github.com/LivInTheLookingGlass/Euler
.. csharp:inherits:: Euler.IEuler
.. csharp:method:: Task<object> Answer()
.. csharp:method:: object Answer()
.. literalinclude:: ../../csharp/Euler/p0001.cs
:language: csharp
Expand Down
2 changes: 1 addition & 1 deletion docs/csharp/p0002.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ View source code `here on GitHub! <https://github.com/LivInTheLookingGlass/Euler
.. csharp:inherits:: Euler.IEuler
.. csharp:method:: Task<object> Answer()
.. csharp:method:: object Answer()
.. literalinclude:: ../../csharp/Euler/p0002.cs
:language: csharp
Expand Down
2 changes: 1 addition & 1 deletion docs/csharp/p0004.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ View source code `here on GitHub! <https://github.com/LivInTheLookingGlass/Euler
.. csharp:inherits:: Euler.IEuler
.. csharp:method:: Task<object> Answer()
.. csharp:method:: object Answer()
.. literalinclude:: ../../csharp/Euler/p0004.cs
:language: csharp
Expand Down
2 changes: 1 addition & 1 deletion docs/csharp/p0006.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ View source code `here on GitHub! <https://github.com/LivInTheLookingGlass/Euler
.. csharp:inherits:: Euler.IEuler
.. csharp:method:: Task<object> Answer()
.. csharp:method:: object Answer()
.. literalinclude:: ../../csharp/Euler/p0006.cs
:language: csharp
Expand Down
2 changes: 1 addition & 1 deletion docs/csharp/p0008.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ View source code `here on GitHub! <https://github.com/LivInTheLookingGlass/Euler
.. csharp:inherits:: Euler.IEuler
.. csharp:method:: Task<object> Answer()
.. csharp:method:: object Answer()
.. literalinclude:: ../../csharp/Euler/p0008.cs
:language: csharp
Expand Down
2 changes: 1 addition & 1 deletion docs/csharp/p0009.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ View source code `here on GitHub! <https://github.com/LivInTheLookingGlass/Euler
.. csharp:inherits:: Euler.IEuler
.. csharp:method:: Task<object> Answer()
.. csharp:method:: object Answer()
.. literalinclude:: ../../csharp/Euler/p0009.cs
:language: csharp
Expand Down
2 changes: 1 addition & 1 deletion docs/csharp/p0011.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ View source code `here on GitHub! <https://github.com/LivInTheLookingGlass/Euler
.. csharp:inherits:: Euler.IEuler
.. csharp:method:: Task<object> Answer()
.. csharp:method:: object Answer()
.. literalinclude:: ../../csharp/Euler/p0011.cs
:language: csharp
Expand Down
2 changes: 1 addition & 1 deletion docs/csharp/p0014.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ View source code `here on GitHub! <https://github.com/LivInTheLookingGlass/Euler
.. csharp:inherits:: Euler.IEuler
.. csharp:method:: Task<object> Answer()
.. csharp:method:: object Answer()
.. literalinclude:: ../../csharp/Euler/p0014.cs
:language: csharp
Expand Down
10 changes: 9 additions & 1 deletion docs/csharp/p0015.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@ C# Implementation of Problem 15

View source code `here on GitHub! <https://github.com/LivInTheLookingGlass/Euler/blob/master/csharp/Euler/p0015.cs>`_

Includes
--------

- `math <./math.html>`_

Problem Solution
----------------

.. csharp:namespace:: Euler
.. csharp:class:: p0015
.. csharp:inherits:: Euler.IEuler
.. csharp:method:: Task<object> Answer()
.. csharp:method:: object Answer()
.. literalinclude:: ../../csharp/Euler/p0015.cs
:language: csharp
Expand Down
2 changes: 1 addition & 1 deletion docs/csharp/p0017.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ View source code `here on GitHub! <https://github.com/LivInTheLookingGlass/Euler
.. csharp:inherits:: Euler.IEuler
.. csharp:method:: Task<object> Answer()
.. csharp:method:: object Answer()
.. literalinclude:: ../../csharp/Euler/p0017.cs
:language: csharp
Expand Down
2 changes: 1 addition & 1 deletion docs/csharp/p0076.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ View source code `here on GitHub! <https://github.com/LivInTheLookingGlass/Euler
.. csharp:inherits:: Euler.IEuler
.. csharp:method:: Task<object> Answer()
.. csharp:method:: object Answer()
.. literalinclude:: ../../csharp/Euler/p0076.cs
:language: csharp
Expand Down
2 changes: 1 addition & 1 deletion docs/csharp/p0836.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ View source code `here on GitHub! <https://github.com/LivInTheLookingGlass/Euler
.. csharp:inherits:: Euler.IEuler
.. csharp:method:: Task<object> Answer()
.. csharp:method:: object Answer()
.. literalinclude:: ../../csharp/Euler/p0836.cs
:language: csharp
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`| |
+-----------+------------+------------+------------+------------+------------+------------+
Expand Down
1 change: 0 additions & 1 deletion docs/javascript.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ Library Code
------------

.. toctree::
:glob:
:numbered:
:maxdepth: 1

Expand Down
8 changes: 8 additions & 0 deletions docs/javascript/p0015.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ JavaScript Implementation of Problem 15

View source code `here on GitHub! <https://github.com/LivInTheLookingGlass/Euler/blob/master/javascript/src/p0015.js>`_

Includes
--------

- `math <./math.html>`_

Problem Solution
----------------

.. js:autofunction:: p0015

.. literalinclude:: ../../javascript/src/p0015.js
Expand Down
18 changes: 18 additions & 0 deletions docs/javascript/p0034.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
JavaScript Implementation of Problem 34
=======================================

View source code `here on GitHub! <https://github.com/LivInTheLookingGlass/Euler/blob/master/javascript/src/p0034.js>`_

Includes
--------

- `math <./math.html>`_

Problem Solution
----------------

.. js:autofunction:: p0034

.. literalinclude:: ../../javascript/src/p0034.js
:language: javascript
:linenos:
1 change: 0 additions & 1 deletion docs/python.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ Library Code
------------

.. toctree::
:glob:
:numbered:
:maxdepth: 1

Expand Down
1 change: 1 addition & 0 deletions javascript/euler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
};
Expand Down
34 changes: 34 additions & 0 deletions javascript/src/p0034.js
Original file line number Diff line number Diff line change
@@ -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');

0 comments on commit e421f36

Please sign in to comment.