Skip to content

Commit 8c847e3

Browse files
MatheusMurielMatheus Muriel
andauthored
solution: Project Euler Problem 13 (TheAlgorithms#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 <[email protected]>
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+
exportfunctionlargeSum(bignum){
6+
constnums=[]
7+
for(leti=0;i<bignum.length;i+=50){
8+
nums.push(bignum.slice(i,i+50))
9+
}
10+
11+
letpos=nums[0].length
12+
letret=''
13+
letnum=0
14+
15+
while(pos--){
16+
for(leti=nums.length;i--;){
17+
num+=+nums[i].charAt(pos)
18+
}
19+
ret=num%10+ret
20+
num=num/10|0
21+
}
1422

15-
exportconstlargeSum=(nums)=>{
16-
constarr=nums.split('\n')// convert to array of strings
17-
letsum=0n
18-
for(constitemofarr){
19-
sum+=BigInt(item)
20-
}// calculate the sum
21-
returnparseInt(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+
returnret.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)