| Given a positive integer N, raise each of its digits to the K-th power and sum those values to get SK(N). For example, S2(65) = 62 + 52 = 61. Now, consider a sequence N, SK(N), SK(SK(N)) and so on. The happiness of N with respect to K is the smallest number in this sequence.
You will be given three ints A, B and K. Calculate the happiness of each integer between A and B, inclusive, with respect to K and return their sum.
|
| | Returns: 14 | The sequences for numbers 1 to 5 are:
1: 1, 1, 1...
2: 2, 4, 16, 37, 58, 89, 145, 42, 20, 4...
3: 3, 9, 81, 65, 61, 37, 58, 89, 145, 42, 20, 4, 16, 37...
4: 4, 16, 37, 58, 89, 145, 42, 20, 4...
5: 5, 25, 29, 85, 89, 145, 42, 20, 4, 16, 37, 58, 89...
|
|
|