-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c7c3c22
commit e429852
Showing
14 changed files
with
201 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 Error loading related location Loading 1000 Error loading related location Loading |
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,10 +29,7 @@ | |
8, | ||
9, | ||
11, | ||
14, | ||
13, | ||
15, | ||
17, | ||
*range(13, 18), | ||
20, | ||
22, | ||
34, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / csharp (3, ubuntu-latest)
|
||
{ | ||
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; | ||
} | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |