Skip to content

Commit

Permalink
Solve p16 in C++, C#, Js
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Aug 9, 2024
1 parent c7c3c22 commit e429852
Show file tree
Hide file tree
Showing 14 changed files with 201 additions and 8 deletions.
2 changes: 2 additions & 0 deletions cplusplus/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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>`__
Expand Down
48 changes: 48 additions & 0 deletions cplusplus/src/p0016.cpp
Original file line number Diff line number Diff line change
@@ -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 <iostream>
#include <vector>


unsigned long long p0016() {
std::vector<unsigned long long> numbers(16, 0);
const unsigned long long ten17 = 100000000000000000;
numbers[0] = 1;
for (unsigned char i = 0; i < 1000; i++) {

Check failure

Code scanning / CodeQL

Comparison of narrow type with wide type in loop condition High

Comparison between
i
of type unsigned char and
1000
of wider type int.
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
4 changes: 2 additions & 2 deletions cplusplus/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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},
Expand Down
5 changes: 1 addition & 4 deletions cplusplus/test_euler.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@
8,
9,
11,
14,
13,
15,
17,
*range(13, 18),
20,
22,
34,
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(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) };
Expand Down
52 changes: 52 additions & 0 deletions csharp/Euler/p0016.cs
Original file line number Diff line number Diff line change
@@ -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++)

Check warning on line 21 in csharp/Euler/p0016.cs

View workflow job for this annotation

GitHub Actions / csharp (3, ubuntu-latest)

Comparison to integral constant is useless; the constant is outside the range of type 'byte'

Check warning on line 21 in csharp/Euler/p0016.cs

View workflow job for this annotation

GitHub Actions / csharp (2, ubuntu-latest)

Comparison to integral constant is useless; the constant is outside the range of type 'byte'
{
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;
}
}
}


5 changes: 5 additions & 0 deletions csharp/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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>`__

19 changes: 19 additions & 0 deletions docs/cplusplus/p0016.rst
Original file line number Diff line number Diff line change
@@ -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:
19 changes: 19 additions & 0 deletions docs/csharp/p0016.rst
Original file line number Diff line number Diff line change
@@ -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:
4 changes: 2 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`|
+-----------+------------+------------+------------+------------+------------+------------+
Expand All @@ -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`|
+-----------+------------+------------+------------+------------+------------+------------+
Expand Down
10 changes: 10 additions & 0 deletions docs/javascript/p0016.rst
Original file line number Diff line number Diff line change
@@ -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:
6 changes: 6 additions & 0 deletions javascript/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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>`__
Expand All @@ -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>`__
1 change: 1 addition & 0 deletions javascript/euler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
33 changes: 33 additions & 0 deletions javascript/src/p0016.js
Original file line number Diff line number Diff line change
@@ -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;
};

0 comments on commit e429852

Please sign in to comment.