From e4298527c3e2ff8df58d5fc62eb49662fe63285f Mon Sep 17 00:00:00 2001 From: Olivia Appleton Date: Thu, 8 Aug 2024 23:32:11 -0500 Subject: [PATCH] Solve p16 in C++, C#, Js --- cplusplus/README.rst | 2 ++ cplusplus/src/p0016.cpp | 48 ++++++++++++++++++++++++++++++++++++ cplusplus/test.cpp | 4 +-- cplusplus/test_euler.py | 5 +--- csharp/Euler.Test/test.cs | 1 + csharp/Euler/p0016.cs | 52 +++++++++++++++++++++++++++++++++++++++ csharp/README.rst | 5 ++++ docs/cplusplus/p0016.rst | 19 ++++++++++++++ docs/csharp/p0016.rst | 19 ++++++++++++++ docs/index.rst | 4 +-- docs/javascript/p0016.rst | 10 ++++++++ javascript/README.rst | 6 +++++ javascript/euler.test.js | 1 + javascript/src/p0016.js | 33 +++++++++++++++++++++++++ 14 files changed, 201 insertions(+), 8 deletions(-) create mode 100644 cplusplus/src/p0016.cpp create mode 100644 csharp/Euler/p0016.cs create mode 100644 docs/cplusplus/p0016.rst create mode 100644 docs/csharp/p0016.rst create mode 100644 docs/javascript/p0016.rst create mode 100644 javascript/src/p0016.js diff --git a/cplusplus/README.rst b/cplusplus/README.rst index 576f788b..6a2013c5 100644 --- a/cplusplus/README.rst +++ b/cplusplus/README.rst @@ -169,11 +169,13 @@ Problems Solved - ☒ `2 <./src/p0002.cpp>`__ - ☒ `4 <./src/p0004.cpp>`__ - ☒ `6 <./src/p0006.cpp>`__ +- ☒ `8 <./src/p0008.cpp>`__ - ☒ `9 <./src/p0009.cpp>`__ - ☒ `11 <./src/p0011.cpp>`__ - ☒ `13 <./src/p0013.cpp>`__ - ☒ `14 <./src/p0014.cpp>`__ - ☒ `15 <./src/p0015.cpp>`__ +- ☒ `16 <./src/p0016.cpp>`__ - ☒ `17 <./src/p0017.cpp>`__ - ☒ `20 <./src/p0020.cpp>`__ - ☒ `22 <./src/p0022.cpp>`__ diff --git a/cplusplus/src/p0016.cpp b/cplusplus/src/p0016.cpp new file mode 100644 index 00000000..496b116c --- /dev/null +++ b/cplusplus/src/p0016.cpp @@ -0,0 +1,48 @@ +/* +Project Euler Problem 16 + +Problem: + +2**15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. + +What is the sum of the digits of the number 2**1000? +*/ +#ifndef EULER_P0016 +#define EULER_P0016 +#include +#include + + +unsigned long long p0016() { + std::vector numbers(16, 0); + const unsigned long long ten17 = 100000000000000000; + numbers[0] = 1; + for (unsigned char i = 0; i < 1000; i++) { + for (size_t j = 0; j < numbers.size(); j++) { + numbers[j] *= 2; + } + for (size_t j = 0; j < numbers.size() - 1; j++) { + if (numbers[j] > ten17) { + numbers[j + 1] += numbers[j] / ten17; + numbers[j] %= ten17; + } + } + } + unsigned long long answer = 0; + unsigned long long power = 1; + for (unsigned char i = 0; i < 18; i++) { + for (size_t j = 0; j < numbers.size(); j++) { + answer += (numbers[j] / power) % 10; + } + power *= 10; + } + return answer; +} + +#ifndef UNITY_END +int main(int argc, char const *argv[]) { + std::cout << p0020() << std::endl; + return 0; +} +#endif +#endif diff --git a/cplusplus/test.cpp b/cplusplus/test.cpp index dd313f63..164e15d9 100644 --- a/cplusplus/test.cpp +++ b/cplusplus/test.cpp @@ -15,7 +15,7 @@ #include "src/p0013.cpp" #include "src/p0014.cpp" #include "src/p0015.cpp" -// #include "src/p0016.cpp" +#include "src/p0016.cpp" #include "src/p0017.cpp" #include "src/p0020.cpp" #include "src/p0022.cpp" @@ -45,7 +45,7 @@ static const Answer answers[] = { {13, 5537376230, p0013}, {14, 837799, p0014}, {15, 137846528820, p0015}, - // {16, 1366, p0016}, + {16, 1366, p0016}, {17, 21124, p0017}, {20, 648, p0020}, {22, 871198282, p0022}, diff --git a/cplusplus/test_euler.py b/cplusplus/test_euler.py index d8110f4c..2f2bccfc 100644 --- a/cplusplus/test_euler.py +++ b/cplusplus/test_euler.py @@ -29,10 +29,7 @@ 8, 9, 11, - 14, - 13, - 15, - 17, + *range(13, 18), 20, 22, 34, diff --git a/csharp/Euler.Test/test.cs b/csharp/Euler.Test/test.cs index 68fada0e..53d0cfce 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(p0013), false, Utilities.GetAnswer(13) }; yield return new object[] { typeof(p0014), false, Utilities.GetAnswer(14) }; yield return new object[] { typeof(p0015), false, Utilities.GetAnswer(15) }; + yield return new object[] { typeof(p0016), false, Utilities.GetAnswer(16) }; yield return new object[] { typeof(p0017), false, Utilities.GetAnswer(17) }; yield return new object[] { typeof(p0020), false, Utilities.GetAnswer(20) }; yield return new object[] { typeof(p0022), false, Utilities.GetAnswer(22) }; diff --git a/csharp/Euler/p0016.cs b/csharp/Euler/p0016.cs new file mode 100644 index 00000000..ff6f7a48 --- /dev/null +++ b/csharp/Euler/p0016.cs @@ -0,0 +1,52 @@ +/* +Project Euler Problem 16 + +Problem: + +2**15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. + +What is the sum of the digits of the number 2**1000? +*/ +using System; + +namespace Euler +{ + public class p0016 : IEuler + { + public object Answer() + { + ulong[] numbers = new ulong[16]; + const ulong ten17 = 100000000000000000; + numbers[0] = 1; + for (byte i = 0; i < 1000; i++) + { + for (byte j = 0; j < 16; j++) + { + numbers[j] *= 2; + } + for (byte j = 0; j < 15; j++) + { + if (numbers[j] > ten17) + { + numbers[j + 1] += numbers[j] / ten17; + numbers[j] %= ten17; + } + } + } + ulong answer = 0; + ulong power = 1; + for (byte i = 0; i < 19; i++) + { + for (byte j = 0; j < 16; j++) + { + ulong value = numbers[j] / power; + answer += value % 10; + } + power *= 10; + } + return (ushort)answer; + } + } +} + + diff --git a/csharp/README.rst b/csharp/README.rst index eaca3d97..60f12cc9 100644 --- a/csharp/README.rst +++ b/csharp/README.rst @@ -84,8 +84,13 @@ Problems Solved - ☒ `9 <./Euler/p0009.cs>`__ - ☒ `11 <./Euler/p0011.cs>`__ - ☒ `13 <./Euler/p0013.cs>`__ +- ☒ `14 <./Euler/p0014.cs>`__ +- ☒ `15 <./Euler/p0015.cs>`__ +- ☒ `16 <./Euler/p0016.cs>`__ - ☒ `17 <./Euler/p0017.cs>`__ - ☒ `20 <./Euler/p0020.cs>`__ - ☒ `22 <./Euler/p0022.cs>`__ +- ☒ `34 <./Euler/p0034.cs>`__ +- ☒ `76 <./Euler/p0076.cs>`__ - ☒ `836 <./Euler/p0836.cs>`__ diff --git a/docs/cplusplus/p0016.rst b/docs/cplusplus/p0016.rst new file mode 100644 index 00000000..67d3ec64 --- /dev/null +++ b/docs/cplusplus/p0016.rst @@ -0,0 +1,19 @@ +C++ Implementation of Problem 16 +================================ + +View source code :source:`cplusplus/src/p0016.cpp` + +Solution +-------- + +.. cpp:function:: unsigned long long p0016() + +.. cpp:function:: int main(int argc, char const *argv[]) + + .. note:: + + This function is only present in the Python test runner, or when compiling as a standalone program. + +.. literalinclude:: ../../cplusplus/src/p0016.cpp + :language: C++ + :linenos: diff --git a/docs/csharp/p0016.rst b/docs/csharp/p0016.rst new file mode 100644 index 00000000..256f10dc --- /dev/null +++ b/docs/csharp/p0016.rst @@ -0,0 +1,19 @@ +C# Implementation of Problem 16 +=============================== + +View source code :source:`csharp/Euler/p0016.cs` + +Problem Solution +---------------- + +.. csharp:namespace:: Euler + +.. csharp:class:: p0016 + + .. csharp:inherits:: Euler.IEuler + + .. csharp:method:: object Answer() + +.. literalinclude:: ../../csharp/Euler/p0016.cs + :language: csharp + :linenos: diff --git a/docs/index.rst b/docs/index.rst index 6143f914..e09dc2c9 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -86,7 +86,7 @@ Problems Solved +-----------+------------+------------+------------+------------+------------+------------+ |:prob:`9` |:c-d:`0009` |:cp-d:`0009`|:cs-d:`0009`|:js-d:`0009`|:py-d:`0009`|:rs-d:`0009`| +-----------+------------+------------+------------+------------+------------+------------+ -|:prob:`10` |:c-d:`0010` | | | |:py-d:`0010`|:rs-d:`0010`| +|:prob:`10` |:c-d:`0010` | | |:js-d:`0010`|:py-d:`0010`|:rs-d:`0010`| +-----------+------------+------------+------------+------------+------------+------------+ |:prob:`11` |:c-d:`0011` |:cp-d:`0011`|:cs-d:`0011`|:js-d:`0011`|:py-d:`0011`|:rs-d:`0011`| +-----------+------------+------------+------------+------------+------------+------------+ @@ -98,7 +98,7 @@ Problems Solved +-----------+------------+------------+------------+------------+------------+------------+ |:prob:`15` |:c-d:`0015` |:cp-d:`0015`|:cs-d:`0015`|:js-d:`0015`|:py-d:`0015`|:rs-d:`0015`| +-----------+------------+------------+------------+------------+------------+------------+ -|:prob:`16` |:c-d:`0016` | | | |:py-d:`0016`|:rs-d:`0016`| +|:prob:`16` |:c-d:`0016` |:cp-d:`0016`|:cs-d:`0016`|:js-d:`0016`|:py-d:`0016`|:rs-d:`0016`| +-----------+------------+------------+------------+------------+------------+------------+ |:prob:`17` |:c-d:`0017` |:cp-d:`0017`|:cs-d:`0017`|:js-d:`0017`|:py-d:`0017`|:rs-d:`0017`| +-----------+------------+------------+------------+------------+------------+------------+ diff --git a/docs/javascript/p0016.rst b/docs/javascript/p0016.rst new file mode 100644 index 00000000..38329ed8 --- /dev/null +++ b/docs/javascript/p0016.rst @@ -0,0 +1,10 @@ +JavaScript Implementation of Problem 16 +======================================= + +View source code :source:`javascript/src/p0016.js` + +.. js:autofunction:: p0016 + +.. literalinclude:: ../../javascript/src/p0016.js + :language: javascript + :linenos: diff --git a/javascript/README.rst b/javascript/README.rst index 737227d6..e31eee4f 100644 --- a/javascript/README.rst +++ b/javascript/README.rst @@ -101,6 +101,7 @@ Problems Solved - ☒ `1 <./src/p0001.js>`__ - ☒ `2 <./src/p0002.js>`__ +- ☒ `3 <./src/p0003.js>`__ - ☒ `4 <./src/p0004.js>`__ - ☒ `6 <./src/p0006.js>`__ - ☒ `7 <./src/p0007.js>`__ @@ -109,7 +110,12 @@ Problems Solved - ☒ `10 <./src/p0010.js>`__ - ☒ `11 <./src/p0011.js>`__ - ☒ `13 <./src/p0013.js>`__ +- ☒ `14 <./src/p0014.js>`__ +- ☒ `15 <./src/p0015.js>`__ +- ☒ `16 <./src/p0016.js>`__ - ☒ `17 <./src/p0017.js>`__ - ☒ `20 <./src/p0020.js>`__ - ☒ `22 <./src/p0022.js>`__ +- ☒ `34 <./src/p0034.js>`__ +- ☒ `76 <./src/p0076.js>`__ - ☒ `836 <./src/p0836.js>`__ diff --git a/javascript/euler.test.js b/javascript/euler.test.js index 11f5ce6c..d1a12bde 100644 --- a/javascript/euler.test.js +++ b/javascript/euler.test.js @@ -32,6 +32,7 @@ const answers = { 13: [require('./src/p0013.js'), false], 14: [require('./src/p0014.js'), false], 15: [require('./src/p0015.js'), false], + 16: [require('./src/p0016.js'), false], 17: [require('./src/p0017.js'), false], 20: [require('./src/p0020.js'), false], 22: [require('./src/p0022.js'), false], diff --git a/javascript/src/p0016.js b/javascript/src/p0016.js new file mode 100644 index 00000000..081fd062 --- /dev/null +++ b/javascript/src/p0016.js @@ -0,0 +1,33 @@ +/** + * Project Euler Problem 16 + * + * Problem: + * 2**15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. + * + * What is the sum of the digits of the number 2**1000? + * @return {number} + */ +exports.p0016 = function() { + const ten13 = 10000000000000; + const numbers = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; + for (let i = 0; i < 1000; i++) { + for (let j = 0; j < numbers.length; j++) { + numbers[j] *= 2; + } + for (let j = 0; j < numbers.length - 1; j++) { + if (numbers[j] > ten13) { + numbers[j + 1] += 0 | (numbers[j] / ten13); + numbers[j] %= ten13; + } + } + } + let answer = 0; + let power = 1; + while (power < ten13) { + for (let j = 0; j < numbers.length; j++) { + answer += 0 | ((numbers[j] / power) % 10); + } + power *= 10; + } + return answer; +};