# Exercism: Armstrong Numbers (Ruby)

A presentation outlining my solution to the Exercism Armstrong Numbers problem in Ruby (https://exercism.io/tracks/ruby/exercises/armstrong-numbers).

Presented at the Sydney Ruby on Rails meetup on 10 September 2019.

Presentation slide deck markdown and speaker notes (useable in Deckset 2):
https://github.com/paulfioravanti/presentations/tree/master/exercism_ruby_armstrong_numbers

#### Paul Fioravanti

September 10, 2019

## Transcript

1. 1.

2. 2.
3. 3.
4. 4.

5. 5.

6. 6.

7. 7.

8. 8.

9. 9.

10. 10.

11. 11.

12. 12.

### Armstrong Numbers ✅ 9 = 9^1 = 9 ❌ 10

!= 1^2 + 0^2 = 1
13. 13.

= 153
14. 14.

### Armstrong Numbers ✅ 153 = 1^3 + 5^3 + 3^3

= 153 ❌ 154 != 1^3 + 5^3 + 4^3 = 190
15. 15.

16. 16.

17. 17.

### Armstrong Numbers ! Get the number of digits ! Calculate

the power of each digit
18. 18.

### Armstrong Numbers ! Get the number of digits ! Calculate

the power of each digit ! Sum all the powers
19. 19.

20. 20.

21. 21.

end
22. 22.

23. 23.

end
24. 24.

end end
25. 25.

### Get number of digits module ArmstrongNumbers module_function def include?(number) number

.digits end end
26. 26.

end end
27. 27.

### Sum Powers module ArmstrongNumbers module_function def include?(number) sum = number

.digits .then(&method(:sum_powers)) end end
28. 28.

### Sum Powers module ArmstrongNumbers module_function def include?(number) sum = number

.digits .then(&method(:sum_powers)) sum == number end end
29. 29.

### Sum Powers module ArmstrongNumbers module_function # ... def sum_powers(digits) end

private_class_method :sum_powers end
30. 30.

### Sum Powers module ArmstrongNumbers module_function # ... def sum_powers(digits) digits

end private_class_method :sum_powers end
31. 31.

### Sum Powers module ArmstrongNumbers module_function # ... def sum_powers(digits) digits

.each end private_class_method :sum_powers end
32. 32.

### Sum Powers module ArmstrongNumbers module_function # ... def sum_powers(digits) digits

.each .with_object(digits.length) end private_class_method :sum_powers end
33. 33.

### Sum Powers module ArmstrongNumbers module_function # ... def sum_powers(digits) digits

.each .with_object(digits.length) .sum(&method(:power)) end private_class_method :sum_powers end
34. 34.

### Calculate Power of Each Digit module ArmstrongNumbers module_function # ...

def power((digit, length)) digit**length end private_class_method :power end
35. 35.

### module ArmstrongNumbers module_function def include?(number) sum = number .digits .then(&method(:sum_powers))

sum == number end def sum_powers(digits) digits .each .with_object(digits.length) .sum(&method(:power)) end private_class_method :sum_powers def power((digit, length)) digit**length end private_class_method :power end
36. 36.
37. 37.
38. 38.

39. 39.

40. 40.

41. 41.

42. 42.

43. 43.

44. 44.

45. 45.

### module ArmstrongNumbers module_function def include?(number) digits = number.digits length =

digits.length end end
46. 46.

### module ArmstrongNumbers module_function def include?(number) digits = number.digits length =

digits.length number == digits.sum { |digit| digit**length } end end
47. 47.
48. 48.

49. 49.

50. 50.

51. 51.

52. 52.

53. 53.

54. 54.

55. 55.

56. 56.

### BONUS FACTOIDS! ! Number of Armstrong numbers is ﬁnite !

Only 88 Armstrong numbers in Base 10
57. 57.

58. 58.
59. 59.