A Pen by stephen preston on CodePen.
Last active
December 27, 2020 19:02
-
-
Save toddpress/89a66188782f324536af135f10516257 to your computer and use it in GitHub Desktop.
Take a Number And Sum Its Digits Raised To The Consecutive Powers And ....¡Eureka!!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
p The number 89 is the first integer with more than one digit that fulfills the property partially introduced in the title of this kata. What's the use of saying "Eureka"? Because this sum gives the same number. | |
p In effect: | |
code 89 = 8^1 + 9^2 | |
p The next number in having this property is 135. | |
p See this property again: | |
code 135 = 1^1 + 3^2 + 5^3 | |
p We need a function to collect these numbers, that may receive two integers a, b that defines the range [a, b] (inclusive) and outputs a list of the sorted numbers in the range that fulfills the property described above. | |
p Let's see some cases: | |
p: code sumDigPow(1, 10) == [1, 2, 3, 4, 5, 6, 7, 8, 9] | |
p: code sumDigPow(1, 100) == [1, 2, 3, 4, 5, 6, 7, 8, 9, 89] | |
p If there are no numbers of this kind in the range [a, b] the function should output an empty list. | |
p: code sumDigPow(90, 100) == [] | |
p Enjoy it!! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const sumDigPow = (a, b) => | |
[...Array(b + 1).keys()] | |
.slice(a) | |
.filter( | |
(int) => | |
`${int}`.split("") | |
.reduce((acc, str, i) => acc + str ** ++i, 0) === int | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment