Project Euler in Ruby: Problem 16

  • by

For Project 16 of Project Euler, I’ll jump over to Ruby for an elegant solution.

Problem 16:

Power Digit Sum

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?

To come to this solution, we generate the result of 2^1000, convert it into a string and split each digit into its own entry on an array, then sum them up all in one easy line.

answer = (2**1000).to_s.split('').map(&:to_i).reduce(:+)

puts answer

--output: 1366

Questions or comments? Feel free to leave them below or reach out to me on Twitter!

Leave a Reply

Your email address will not be published. Required fields are marked *