diff --git a/challenges/euler/readme.md b/challenges/euler/readme.md index 5729488..ceeadac 100644 --- a/challenges/euler/readme.md +++ b/challenges/euler/readme.md @@ -59,7 +59,7 @@ The source code can be found in the [src](src) directory. My thoughts about some - [ ] 45 - Triangular, pentagonal, and hexagonal - [ ] 46 - Goldbach's other conjecture - [ ] 47 - Distinct primes factors -- [ ] 48 - Self powers +- [x] [48 - Self powers](src/48%20-%20Self%20powers.ts) - [ ] 49 - Prime permutations - [ ] 50 - Consecutive prime sum - [ ] 51 - Prime digit replacements diff --git a/challenges/euler/src/48 - Self powers.ts b/challenges/euler/src/48 - Self powers.ts new file mode 100644 index 0000000..e8230b5 --- /dev/null +++ b/challenges/euler/src/48 - Self powers.ts @@ -0,0 +1,21 @@ +// The series, 1^1 + 2^2 + 3^3 + ... + 10^10 = 10405071317. +// Find the last ten digits of the series, 1^1 + 2^2 + 3^3 + ... + 1000^1000. +export = {}; + +const calculateSeries = (upperLimit: number): bigint => { + let sum = BigInt(0); + + for (let i = 1; i <= upperLimit; i++) { + sum += BigInt(i) ** BigInt(i); + } + + return sum; +}; + +const lastNDigits = (number: number | bigint, n: number) => { + const string = number.toString(); + return parseInt(string.substring(string.length - n)); +}; + +// Output +console.log(lastNDigits(calculateSeries(1000), 10)); diff --git a/challenges/euler/tsconfig.json b/challenges/euler/tsconfig.json index 26e218d..0c42201 100644 --- a/challenges/euler/tsconfig.json +++ b/challenges/euler/tsconfig.json @@ -1,6 +1,6 @@ { "compilerOptions": { - "target": "es5", + "target": "es2020", "module": "commonjs", "outDir": "build", "esModuleInterop": true,