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>
This commit is contained in:
Matheus Muriel
2022-10-15 06:31:02 -03:00
committed by GitHub
parent 4df1e9e90e
commit 8c847e3aea
2 changed files with 29 additions and 122 deletions

View File

@ -1,22 +1,27 @@
/** /**
* Problem 13 - Large Sum * Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.
*
* @see {@link https://projecteuler.net/problem=13}
*
* Work out the first ten digits of the sum of the given one-hundred 50-digit numbers.
*/ */
/** export function largeSum (bignum) {
* calculates the sum and returns first 10 digits. const nums = []
* @param {String} nums for (let i = 0; i < bignum.length; i += 50) {
* @returns {Number} nums.push(bignum.slice(i, i + 50))
*/ }
export const largeSum = (nums) => { let pos = nums[0].length
const arr = nums.split('\n') // convert to array of strings let ret = ''
let sum = 0n let num = 0
for (const item of arr) {
sum += BigInt(item) while (pos--) {
} // calculate the sum for (let i = nums.length; i--;) {
return parseInt(sum.toString().substring(0, 10)) // convert the sum to a string and get the first 10 digits num += +nums[i].charAt(pos)
}
ret = num % 10 + ret
num = num / 10 | 0
}
if (num > 0) {
ret = num + ret
}
return ret.slice(0, 10)
} }

File diff suppressed because one or more lines are too long