jsacademyio / Data-Structures-And-Algorithms-Workshop

This is the repository for the Data Structures & Algorithms workshop
MIT License
2 stars 11 forks source link

Power sum #8

Open acha-bill opened 4 years ago

acha-bill commented 4 years ago

Find the number of ways that a given integer, X , can be expressed as the sum of the Nth powers of unique, natural numbers.

For example, if X = 13 and N = 2, we have to find all combinations of unique squares adding up to 13. The only solution is 2^2 + 3^2.

Write a function that given X and N, calculates the number of ways X and be represented as a power sum of N.

e.g

x = 10 and n = 2
Answer: 1

explanation: There is only 1 way:

  1. 3^2 + 1^2

e.g

x = 100, n = 2
answer: 3

explanation: Ther are 3 ways to get 100 as a sum of squares

  1. 100^2
  2. 6^2 + 8^2
  3. 1^2 + 3^2 + 4^2 + 5^2 + 7^2

e.g

x = 100, n = 3
answer: 1

There is only 1 way 100 can be expressed as a sum of cubes

  1. 1^3 + 2^3 + 3^3 + 4^3