Last active
December 12, 2017 21:27
-
-
Save dbhalling/88b6c6602eb03b06e37656213d10b971 to your computer and use it in GitHub Desktop.
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
#ThePowerSum | |
x = 100 | |
n = 3.0 | |
def solution(x, n) | |
answer = 0 | |
power = 1/n | |
sq = x**(1/n) | |
sqi = sq.to_i | |
aofsquares = [] | |
for i in 1..sqi | |
aofsquares = aofsquares.push(i**n) | |
end | |
count = aofsquares.count | |
sumsa = [] | |
for i in 1..count | |
sumsa = [] | |
comba = aofsquares.combination(i).to_a | |
sumsa = sumsa.push(comba.map {|s| s.sum}) | |
sumsa.flatten! | |
sumsa.delete_if {|s| s != x} | |
answer = answer + sumsa.count | |
end | |
return answer | |
end | |
puts "The number of ways that #{x} can be expressed as sum of the #{n}th power is #{solution(x, n)}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment