Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8c847e3

Browse files
MatheusMurielMatheus Muriel
and
Matheus Muriel
authored
solution: Project Euler Problem 13 (#1173)
* Included Project Euler 13 solution * Fix review comments * Fix style * fix for code review * fix the review * Fix Style Co-authored-by: Matheus Muriel <Matheus_MurielFerreira@swissre.com>
1 parent 4df1e9e commit 8c847e3

File tree

2 files changed

+29
-122
lines changed

2 files changed

+29
-122
lines changed

‎Project-Euler/Problem013.js‎

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
/**
2-
* Problem 13 - Large Sum
3-
*
4-
* @see {@link https://projecteuler.net/problem=13}
5-
*
6-
* Work out the first ten digits of the sum of the given one-hundred 50-digit numbers.
7-
*/
2+
* Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.
3+
*/
84

9-
/**
10-
* calculates the sum and returns first 10 digits.
11-
* @param {String} nums
12-
* @returns {Number}
13-
*/
5+
export function largeSum (bignum) {
6+
const nums = []
7+
for (let i = 0; i < bignum.length; i += 50) {
8+
nums.push(bignum.slice(i, i + 50))
9+
}
10+
11+
let pos = nums[0].length
12+
let ret = ''
13+
let num = 0
14+
15+
while (pos--) {
16+
for (let i = nums.length; i--;) {
17+
num += +nums[i].charAt(pos)
18+
}
19+
ret = num % 10 + ret
20+
num = num / 10 | 0
21+
}
1422

15-
export const largeSum = (nums) => {
16-
const arr = nums.split('\n') // convert to array of strings
17-
let sum = 0n
18-
for (const item of arr) {
19-
sum += BigInt(item)
20-
} // calculate the sum
21-
return parseInt(sum.toString().substring(0, 10)) // convert the sum to a string and get the first 10 digits
23+
if (num > 0) {
24+
ret = num + ret
25+
}
26+
return ret.slice(0, 10)
2227
}

‎Project-Euler/test/Problem013.test.js‎

Lines changed: 6 additions & 104 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /