def digisum(x) ret = 0 (1..(x.to_s.length)).each {|i| ret += (x % (10 ** i)) / (10 ** (i - 1)) } return ret end ans = 0 (1..99).each {|a| (1..99).each {|b| dsum = digisum(a ** b) ans = dsum > ans ? dsum : ans } } printf("%d\n",ans)
Saturday, October 19, 2013
Problem 56 - Ruby
Solving problem 30 in K opened up Ruby for use again. Though Ruby is not a language I have terribly much experience with (indeed, I have only used it as part of this project), it is still pretty nice, and it makes solving Problem 56 relatively easy, as it supports (arbitrary? at least very large) precision integer arithmetic: enough to store 99^99 in a variable with no problem. Here is my solution to this problem in Ruby. I am sure there is room for improvement: this solution takes 2 seconds to run, but nonetheless, it only took a minute or two to write, so its fine.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment