Solved armstrong-numbers in Bash

This commit is contained in:
2023-04-25 19:32:35 +02:00
parent 0a54d06454
commit af54b17140
7 changed files with 899 additions and 0 deletions

View File

@@ -0,0 +1,20 @@
#!/usr/bin/env bash
number="$1"
# Convert number to string to get the number of digits
num_str="${number}"
num_digits="${#num_str}"
sum=0
for (( i=0; i<${num_digits}; i++ )); do
# Extract the i-th digit from the number
digit="${num_str:$i:1}"
sum=$((sum + digit ** num_digits))
done
if (( sum == number )); then
echo "true"
else
echo "false"
fi