0
0
RubyProgramBeginner · 2 min read

Ruby Program to Check Armstrong Number

A Ruby program to check an Armstrong number calculates the sum of each digit raised to the power of the number of digits and compares it to the original number using code like sum == number after computing sum = digits.map { |d| d**digits.size }.sum.
📋

Examples

Input153
Output153 is an Armstrong number
Input9474
Output9474 is an Armstrong number
Input123
Output123 is not an Armstrong number
🧠

How to Think About It

To check if a number is an Armstrong number, first split the number into its digits. Then raise each digit to the power of the total number of digits and add these values together. If the sum equals the original number, it is an Armstrong number.
📐

Algorithm

1
Get the input number.
2
Convert the number to a list of its digits.
3
Calculate the number of digits.
4
Raise each digit to the power of the number of digits and sum the results.
5
Compare the sum with the original number.
6
Return whether the number is an Armstrong number or not.
💻

Code

ruby
puts 'Enter a number:'
number = gets.to_i

digits = number.to_s.chars.map(&:to_i)
power = digits.size
sum = digits.map { |d| d**power }.sum

if sum == number
  puts "#{number} is an Armstrong number"
else
  puts "#{number} is not an Armstrong number"
end
Output
Enter a number: 153 153 is an Armstrong number
🔍

Dry Run

Let's trace the number 153 through the code

1

Input number

number = 153

2

Convert to digits

digits = [1, 5, 3]

3

Count digits

power = 3

4

Calculate sum of powers

sum = 1**3 + 5**3 + 3**3 = 1 + 125 + 27 = 153

5

Compare sum and number

sum == number → 153 == 153 → true

6

Output result

"153 is an Armstrong number"

DigitDigit^Power
11
5125
327
💡

Why This Works

Step 1: Extract digits

We convert the number to a string and then to an array of digits to handle each digit separately.

Step 2: Calculate power

The power is the count of digits because Armstrong numbers use the number of digits as the exponent.

Step 3: Sum of powered digits

Each digit is raised to the power and summed to check if it matches the original number.

Step 4: Comparison

If the sum equals the original number, it confirms the number is an Armstrong number.

🔄

Alternative Approaches

Using a loop instead of map
ruby
puts 'Enter a number:'
number = gets.to_i

digits = number.to_s.chars.map(&:to_i)
power = digits.size
sum = 0
digits.each { |d| sum += d**power }

if sum == number
  puts "#{number} is an Armstrong number"
else
  puts "#{number} is not an Armstrong number"
end
This uses a loop to sum powers instead of map and sum, which is more explicit but slightly longer.
Using a function to check Armstrong
ruby
def armstrong?(number)
  digits = number.to_s.chars.map(&:to_i)
  power = digits.size
  digits.map { |d| d**power }.sum == number
end

puts 'Enter a number:'
num = gets.to_i
puts armstrong?(num) ? "#{num} is an Armstrong number" : "#{num} is not an Armstrong number"
Encapsulates logic in a function for reuse and cleaner main code.

Complexity: O(n) time, O(n) space

Time Complexity

The program processes each digit once to compute the power and sum, so time grows linearly with the number of digits.

Space Complexity

Extra space is used to store the digits array, which grows linearly with the number of digits.

Which Approach is Fastest?

Using map and sum is concise and efficient; using a loop is slightly more verbose but similar in performance.

ApproachTimeSpaceBest For
map and sumO(n)O(n)Concise and readable code
loop with accumulatorO(n)O(n)Explicit control over summing
function encapsulationO(n)O(n)Reusable and clean structure
💡
Convert the number to digits as strings first, then to integers for easy processing.
⚠️
Forgetting to raise each digit to the power of the total number of digits instead of a fixed power like 3.