We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
My explanation of the recursive approach. Since there isn't an editorial.
What we have:
num and pow (N and power)
Before we even solve the problem we have to solve another problem: What's the max integer value, such that value^pow<=num
int max_value=(int) num^(1/pow) #max=(num**(1.to_f/$pow)).to_i this is my line in the code
Why do we care about this max_value? because the set (1..max_value) i.e. from 1 to max_value inclusive are the only values s.t. values^pow <= num, any number greater than this range is greater than the num and not worth computing time...
So we have a range: 1 to max_value. This will help with the base case.
Now the recursion, is a little more complex than just one call...it's two calls. lol. It honestly, took me too long to figure this part out.
num=gets().to_i
$pow=gets().to_i
max=(num**(1.to_f/$pow)).to_i
def recurse(n,max)
return 0 if (n < 0 || (n > 0 && max == 0))
return 1 if (n == 0)
return recurse(n-(max**$pow),max-1)+recurse(n,max-1)
end
puts recurse(num,max)
Recursive Call:
Don't worry yet: recursive(num, pow, max) is the first call we'll make.
we'll return the sum of two things:
recursive(num-(max^pow), pow, max-1)
num-max^pow is the new value we'll search. max-1 exists because we want to take max out of circulation and only count values s.t.(1 to max-1)
The other part is
recursive(num,pow,max-1)
Same reasoning as above, but you only want solutions that do NOT include max.
Explaining the recursive Base cases:
if num<0, then we subtracted too much and no value exists.
if num>0 and max==0, then there is a remainder in the subtraction, and no remaining numbers to subtract
if num==0, then we have 1 instance where the sum is possible.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
The Power Sum
You are viewing a single comment's thread. Return to all comments →
My explanation of the recursive approach. Since there isn't an editorial.
What we have: num and pow (N and power)
Before we even solve the problem we have to solve another problem: What's the max integer value, such that value^pow<=num
Why do we care about this max_value? because the set (1..max_value) i.e. from 1 to max_value inclusive are the only values s.t. values^pow <= num, any number greater than this range is greater than the num and not worth computing time...
So we have a range: 1 to max_value. This will help with the base case.
Now the recursion, is a little more complex than just one call...it's two calls. lol. It honestly, took me too long to figure this part out.
Recursive Call:
Don't worry yet: recursive(num, pow, max) is the first call we'll make.
we'll return the sum of two things:
num-max^pow is the new value we'll search. max-1 exists because we want to take max out of circulation and only count values s.t.(1 to max-1)
The other part is
Same reasoning as above, but you only want solutions that do NOT include max.
Explaining the recursive Base cases:
if num<0, then we subtracted too much and no value exists.
if num>0 and max==0, then there is a remainder in the subtraction, and no remaining numbers to subtract
if num==0, then we have 1 instance where the sum is possible.