Check Armstrong Number in R: Comprehensive Guide with Examples

An Armstrong number (also known as a narcissistic number) is a number that is equal to the sum of its own digits each raised to the power of the number of digits. For example, 153 is an Armstrong number because

1^3 + 5^3 + 3^3 = 153

In this guide, we will explore how to check for Armstrong numbers in R, providing three different solutions with corresponding outputs. Before diving into the examples, let’s review the prerequisites necessary for this article.

Prerequisites

To follow along with this guide, you should have:

  • Basic knowledge of R programming
  • R and RStudio installed on your machine
  • Familiarity with basic control structures in R such as loops and conditional statements

1. Using Basic Loop and Arithmetic Operations

1.1. Example 1: Checking a Single Number

In this example, we will use basic loop and arithmetic operations to check if a number is an Armstrong number.

Code

R
# Function to check if a number is an Armstrong number
is_armstrong <- function(number) {
  # Convert the number to a string to extract digits
  digits <- as.character(number)
  num_digits <- nchar(digits)
  
  # Calculate the sum of digits each raised to the power of num_digits
  sum_digits <- 0
  for (digit in strsplit(digits, NULL)[[1]]) {
    sum_digits <- sum_digits + as.numeric(digit)^num_digits
  }
  
  # Check if the sum equals the original number
  return(sum_digits == number)
}

# Test the function with an example
number <- 153
result <- is_armstrong(number)
cat(number, "is an Armstrong number:", result, "\n")

Explanation

  • Function Definition: We define a function is_armstrong to check if a number is an Armstrong number.
  • Digit Extraction: We convert the number to a string to extract its digits.
  • Sum Calculation: We use a loop to calculate the sum of the digits, each raised to the power of the number of digits.
  • Result Check: We compare the sum with the original number to determine if it is an Armstrong number.
  • Testing: We test the function with the number 153 and print the result.

Output

R
153 is an Armstrong number: TRUE

1.2. Example 2: Checking a Range of Numbers

In this example, we extend the previous solution to check a range of numbers for Armstrong numbers.

Code

R
# Function to check if a number is an Armstrong number
is_armstrong <- function(number) {
  digits <- as.character(number)
  num_digits <- nchar(digits)
  sum_digits <- 0
  for (digit in strsplit(digits, NULL)[[1]]) {
    sum_digits <- sum_digits + as.numeric(digit)^num_digits
  }
  return(sum_digits == number)
}

# Function to find Armstrong numbers in a range
find_armstrong_numbers <- function(start, end) {
  armstrong_numbers <- c()
  for (number in start:end) {
    if (is_armstrong(number)) {
      armstrong_numbers <- c(armstrong_numbers, number)
    }
  }
  return(armstrong_numbers)
}

# Test the function with a range
start <- 100
end <- 500
result <- find_armstrong_numbers(start, end)
cat("Armstrong numbers between", start, "and", end, "are:", result, "\n")

Explanation

  • Function Definition: The is_armstrong function is reused to check individual numbers.
  • Range Function: We define a new function find_armstrong_numbers to find Armstrong numbers in a specified range.
  • Loop and Check: The function loops through the specified range, checks each number, and collects Armstrong numbers.
  • Testing: We test the function with the range 100 to 500 and print the results.

Output

R
Armstrong numbers between 100 and 500 are: 153 370 371 407 

1.3. Example 3: Vectorized Approach Using sapply()

In this example, we use a vectorized approach with the sapply() function to check for Armstrong numbers.

Code

R
# Function to check if a number is an Armstrong number
is_armstrong <- function(number) {
  digits <- as.character(number)
  num_digits <- nchar(digits)
  sum_digits <- sum(as.numeric(unlist(strsplit(digits, NULL)))^num_digits)
  return(sum_digits == number)
}

# Function to find Armstrong numbers in a range using sapply
find_armstrong_numbers_sapply <- function(start, end) {
  numbers <- start:end
  armstrong_numbers <- numbers[sapply(numbers, is_armstrong)]
  return(armstrong_numbers)
}

# Test the function with a range
start <- 100
end <- 500
result <- find_armstrong_numbers_sapply(start, end)
cat("Armstrong numbers between", start, "and", end, "are:", result, "\n")

Explanation

  • Function Definition: The is_armstrong function is reused with slight modification to use sapply().
  • Vectorized Function: We define a new function find_armstrong_numbers_sapply to find Armstrong numbers using sapply().
  • Vectorized Check: The function uses sapply() to apply the is_armstrong check across the range of numbers.
  • Testing: We test the function with the range 100 to 500 and print the results.

Output

R
Armstrong numbers between 100 and 500 are: 153 370 371 407 

Conclusion

In this comprehensive guide, we explored three different methods to check for Armstrong numbers in R. We started with a basic loop and arithmetic operations to check a single number. Then, we extended this approach to check a range of numbers. Finally, we demonstrated a vectorized approach using the sapply() function. Each method provides a unique way to identify Armstrong numbers, catering to different needs in data analysis and manipulation. By mastering these techniques, you can efficiently handle and analyze numeric data in R, enhancing your data processing workflows.