Skip to content

Instantly share code, notes, and snippets.

@dbhalling
Last active December 12, 2017 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbhalling/88b6c6602eb03b06e37656213d10b971 to your computer and use it in GitHub Desktop.
Save dbhalling/88b6c6602eb03b06e37656213d10b971 to your computer and use it in GitHub Desktop.
#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