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

# cpp go java php ruby rust javascript
1
2
3  
4    
5    
6      
7          
8          
9          
10          
11          
12          
# cpp ruby
13
14
15
16
17
18
19
20  
21  
22  
23  
24