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

Project Euler - Problem 16 #745

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
raklaptudirm merged 4 commits into TheAlgorithms:master from lvlte:ProjectEuler/016
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@
* [Problem013](https://github.com/TheAlgorithms/Javascript/blob/master/Project-Euler/Problem013.js)
* [Problem014](https://github.com/TheAlgorithms/Javascript/blob/master/Project-Euler/Problem014.js)
* [Problem015](https://github.com/TheAlgorithms/Javascript/blob/master/Project-Euler/Problem015.js)
* [Problem016](https://github.com/TheAlgorithms/Javascript/blob/master/Project-Euler/Problem016.js)
* [Problem020](https://github.com/TheAlgorithms/Javascript/blob/master/Project-Euler/Problem020.js)
* [Problem1](https://github.com/TheAlgorithms/Javascript/blob/master/Project-Euler/Problem1.js)
* [Problem10](https://github.com/TheAlgorithms/Javascript/blob/master/Project-Euler/Problem10.js)
Expand Down
45 changes: 45 additions & 0 deletions Project-Euler/Problem016.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Problem 16 - Power digit sum
*
* @see {@link https://projecteuler.net/problem=16}
*
* 215 = 32768 and the sum of its digits is 3 +たす 2 +たす 7 +たす 6 +たす 8 = 26.
*
* What is the sum of the digits of the number 21000 ?
*/

/**
* Returns the power digit sum of n^pow.
*
* @param {number} [n=2]
* @param {number} [pow=1000]
* @returns {number}
*/
const powerDigitSum = function (n = 2, pow = 1000) {
// The idea is to consider each digit (d*10^exp) separately, right-to-left.
// digits = [units, tens, ...]

const digits = [n]
let p = 1

while (++p <= pow) {
let carry = 0
for (let exp = 0; exp < digits.length; exp++) {
const prod = digits[exp] * n + carry
carry = Math.floor(prod / 10)
digits[exp] = prod % 10
}
while (carry > 0) {
digits.push(carry % 10)
carry = Math.floor(carry / 10)
}
}

// (digits are reversed but we only want the sum so it doesn't matter)

return digits.reduce((prev, current) => prev + current, 0)
}

console.log('Power digit sum of 2^1000 :', powerDigitSum())

module.exports = powerDigitSum
16 changes: 16 additions & 0 deletions Project-Euler/test/Problem016.test.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const powerDigitSum = require('../Problem016')

describe('Check Problem 16 - Power digit sum', () => {
it('Power digit sum of 2^15', () => {
expect(powerDigitSum(2, 15)).toBe(26)
})

it('Power digit sum of 2^1000', () => {
expect(powerDigitSum()).toBe(1366)
expect(powerDigitSum(2, 1000)).toBe(1366)
})

it('Power digit sum of 3^5000', () => {
expect(powerDigitSum(3, 5000)).toBe(11097)
})
})

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