Skip to content

Instantly share code, notes, and snippets.

@toddpress
Last active December 27, 2020 19:02
Show Gist options
  • Save toddpress/89a66188782f324536af135f10516257 to your computer and use it in GitHub Desktop.
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!!
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!!
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