Euler 016 ruby Solution

Power Digit Sum

Problem

https://projecteuler.net/problem=16

\(2^{15} = 32768\) and the sum of its digits is \(3 + 2 + 7 + 6 + 8 = 26\).

What is the sum of the digits of the number \(2^{1000}\)?

Answer: 1366

Solution

euler015.rb

#!/usr/bin/env ruby
def power_digit_sum(power)
  digits = (2**power).to_s
  digits.split('').reduce(0) { |sum, digit| sum + digit.to_i }
end

puts power_digit_sum(1000) if __FILE__ == $PROGRAM_NAME

See Also