From 2ea39679fc47a44e2e1f7d56eb18bbe65e66d45e Mon Sep 17 00:00:00 2001 From: Sahil Afrid Farookhi Date: Fri, 7 May 2021 20:14:06 +0530 Subject: [PATCH 1/2] armstrong number feature implementation --- DIRECTORY.md | 1 + maths/armstrong_number.rb | 57 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 maths/armstrong_number.rb diff --git a/DIRECTORY.md b/DIRECTORY.md index bf86d5d..4c2cb0a 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -72,6 +72,7 @@ * [Add Digits](https://github.com/TheAlgorithms/Ruby/blob/master/maths/add_digits.rb) * [Aliquot Sum](https://github.com/TheAlgorithms/Ruby/blob/master/maths/aliquot_sum.rb) * [Aliquot Sum Test](https://github.com/TheAlgorithms/Ruby/blob/master/maths/aliquot_sum_test.rb) + * [Armstrong Number](https://github.com/TheAlgorithms/Ruby/blob/master/maths/armstrong_number.rb) * [Average Mean](https://github.com/TheAlgorithms/Ruby/blob/master/maths/average_mean.rb) * [Average Median](https://github.com/TheAlgorithms/Ruby/blob/master/maths/average_median.rb) * [Binary To Decimal](https://github.com/TheAlgorithms/Ruby/blob/master/maths/binary_to_decimal.rb) diff --git a/maths/armstrong_number.rb b/maths/armstrong_number.rb new file mode 100644 index 0000000..36aed4e --- /dev/null +++ b/maths/armstrong_number.rb @@ -0,0 +1,57 @@ +# A ruby program to find a given number is armstrong number or not +# Wiki url: https://en.wikipedia.org/wiki/Narcissistic_number +# other resources: https://pages.mtu.edu/~shene/COURSES/cs201/NOTES/chap04/arms.html + +# +# Examples: -> 153 = (1 * 1 * 1) + (5 * 5 * 5) + (3 * 3 * 3) [Armstrong number] and -> 125 != (1 * 1 * 1) + (2 * 2 * 2) + (5 * 5 * 5) +# -> 1634 = (1 * 1 * 1 * 1) + (6 * 6 * 6 * 6) + (3 * 3 * 3 * 3) + (4 * 4 * 4 * 4) +# + +def armstrong_number(number) + num = number + sum = 0 + len = number.digits.count + while(number != 0) + remainder = number % 10 + sum += remainder ** len + number /= 10 + end + + if num == sum + "The number #{num} is an Armstrong number." + else + "The number #{num} is not an Armstrong number." + end + rescue + "Error: Please provide number only!" +end + +# +# Valid inputs +# +puts armstrong_number(153) +# The number 153 is an Armstrong number. + +puts armstrong_number(0) +# The number 0 is an Armstrong number. + +puts armstrong_number(370) +# The number 370 is an Armstrong number. + +puts armstrong_number(10) +# The number 10 is not an Armstrong number. + +puts armstrong_number(1634) +# The number 1634 is an Armstrong number. + +puts armstrong_number(123) +# The number 123 is not an Armstrong number. + +# +# Invalid inputs +# +puts armstrong_number("153") +# Error: Please provide number only! + +puts armstrong_number("a") +# Error: Please provide number only! From a5e3a0f2b442c9d653766e72aadfe2bf0c85f374 Mon Sep 17 00:00:00 2001 From: Sahil Afrid Farookhi Date: Sat, 15 May 2021 11:10:22 +0530 Subject: [PATCH 2/2] Update maths/armstrong_number.rb Co-authored-by: Jessica Kwok <60627484+jsca-kwok@users.noreply.github.com> --- maths/armstrong_number.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/armstrong_number.rb b/maths/armstrong_number.rb index 36aed4e..b312891 100644 --- a/maths/armstrong_number.rb +++ b/maths/armstrong_number.rb @@ -1,4 +1,4 @@ -# A ruby program to find a given number is armstrong number or not +# A ruby program to determine whether a given number is an Armstrong number # Wiki url: https://en.wikipedia.org/wiki/Narcissistic_number # other resources: https://pages.mtu.edu/~shene/COURSES/cs201/NOTES/chap04/arms.html