feat(euler): 48 - self powers
This commit is contained in:
parent
0612c6ac59
commit
5041ebec2d
3 changed files with 23 additions and 2 deletions
|
@ -59,7 +59,7 @@ The source code can be found in the [src](src) directory. My thoughts about some
|
||||||
- [ ] 45 - Triangular, pentagonal, and hexagonal
|
- [ ] 45 - Triangular, pentagonal, and hexagonal
|
||||||
- [ ] 46 - Goldbach's other conjecture
|
- [ ] 46 - Goldbach's other conjecture
|
||||||
- [ ] 47 - Distinct primes factors
|
- [ ] 47 - Distinct primes factors
|
||||||
- [ ] 48 - Self powers
|
- [x] [48 - Self powers](src/48%20-%20Self%20powers.ts)
|
||||||
- [ ] 49 - Prime permutations
|
- [ ] 49 - Prime permutations
|
||||||
- [ ] 50 - Consecutive prime sum
|
- [ ] 50 - Consecutive prime sum
|
||||||
- [ ] 51 - Prime digit replacements
|
- [ ] 51 - Prime digit replacements
|
||||||
|
|
21
challenges/euler/src/48 - Self powers.ts
Normal file
21
challenges/euler/src/48 - Self powers.ts
Normal file
|
@ -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));
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"target": "es5",
|
"target": "es2020",
|
||||||
"module": "commonjs",
|
"module": "commonjs",
|
||||||
"outDir": "build",
|
"outDir": "build",
|
||||||
"esModuleInterop": true,
|
"esModuleInterop": true,
|
||||||
|
|
Loading…
Reference in a new issue