Posted By

theboylatham on 12/01/09


Tagged


Versions (?)

Project Euler - Problem 56


 / Published in: Ruby
 

  1. # Project Euler - Problem 56
  2. # 7th April 2009
  3. #
  4. # A googol (10^(100)) is a massive number: one followed by one-hundred zeros;
  5. # 100^(100) is almost unimaginably large: one followed by two-hundred zeros.
  6. # Despite their size, the sum of the digits in each number is only 1.
  7. #
  8. # Considering natural numbers of the form, a^(b), where a, b < 100, what is the maximum digital sum?
  9.  
  10. maxsum = 0
  11.  
  12. # For all numbers between 90-100 (guesswork)
  13. for a in 90..100
  14. for b in 90..100
  15. # Set the sum to 0
  16. sum = 0
  17. # Perform the exponent sum
  18. number = a ** b
  19. # Split the number into an array of digits
  20. digit_array = number.to_s.split('')
  21. # Sum the array of digits
  22. digit_array.each do |digit|
  23. sum += digit.to_i
  24. end
  25. # If new sum is largest yet set to maximum
  26. maxsum = [sum, maxsum].max
  27. end
  28. end
  29.  
  30. # Display maximum sum
  31. puts maxsum

Report this snippet  

You need to login to post a comment.