From 5e3307620c8a092a982610fa8f7ced49c7ce06f5 Mon Sep 17 00:00:00 2001 From: Neeraj C Date: Wed, 24 Jun 2020 20:46:23 +0530 Subject: [PATCH 1/5] feat: create math/armstrong_number.cpp --- math/armstrong_number.cpp | 77 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 math/armstrong_number.cpp diff --git a/math/armstrong_number.cpp b/math/armstrong_number.cpp new file mode 100644 index 000000000..0b5f01cd1 --- /dev/null +++ b/math/armstrong_number.cpp @@ -0,0 +1,77 @@ +/** + * @file + * \brief A C++ program to check whether a number is armstrong number or not. + * + * \details + * Armstrong number or [Narcissistic number](https://en.wikipedia.org/wiki/Narcissistic_number) + * is a number that is the sum of its own digits raised to the power of the number of digits. + * @author iamnambiar +*/ +#include +#include +#include + +/** + * Function to calculate the total number of digits in the number. + * @param num Number + * @return Total number of digits. + */ +int number_of_digits(int num) { + int total_digits = 0; + while (num > 0) { + num = num / 10; + ++total_digits; + } + return total_digits; +} + +/** + * Function to check whether the number is armstrong number or not. + * @param num Number + * @return `true` if the number is armstrong. + * @return `false` if the number is not armstrong. + */ +bool is_armstrong(int number) { + // If the number is less than 0, then it is not a armstrong number. + if (number < 0) { + return false; + } + int sum = 0; + int temp = number; + // Finding the total number of digits in the number + int total_digits = number_of_digits(number); + while (temp > 0) { + int rem = temp % 10; + // Finding each digit raised to the power total digit and add it to the total sum + sum = sum + pow(rem, total_digits); + temp = temp / 10; + } + return number == sum; +} + +/** + * Function for testing the is_amicable() with + * all the test cases. + */ +void test() { + // is_armstrong(370) returns true. + assert(is_armstrong(370) == true); + // is_armstrong(225) returns false. + assert(is_armstrong(225) == false); + // is_armstrong(-23) returns false. + assert(is_armstrong(-23) == false); + // is_armstrong(153) returns true. + assert(is_armstrong(153) == true); + // is_armstrong(0) returns true. + assert(is_armstrong(0) == true); + // is_armstrong(12) returns false. + assert(is_armstrong(12) == false); +} + +/** + * Main Function +*/ +int main() { + test(); + return 0; +} From 5b3e30a937e488b3de38673134db136cb1e19759 Mon Sep 17 00:00:00 2001 From: Neeraj C <35414531+iamnambiar@users.noreply.github.com> Date: Wed, 24 Jun 2020 20:52:32 +0530 Subject: [PATCH 2/5] docs: typo fixed --- math/armstrong_number.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/math/armstrong_number.cpp b/math/armstrong_number.cpp index 0b5f01cd1..166c98c07 100644 --- a/math/armstrong_number.cpp +++ b/math/armstrong_number.cpp @@ -50,8 +50,8 @@ bool is_armstrong(int number) { } /** - * Function for testing the is_amicable() with - * all the test cases. + * Function for testing the is_armstrong() function + * with all the test cases. */ void test() { // is_armstrong(370) returns true. From 9c75856235a9cae35ae9833633ef37d65714bdef Mon Sep 17 00:00:00 2001 From: Neeraj C <35414531+iamnambiar@users.noreply.github.com> Date: Thu, 25 Jun 2020 07:59:39 +0530 Subject: [PATCH 3/5] docs: clean the comment Co-authored-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> --- math/armstrong_number.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/math/armstrong_number.cpp b/math/armstrong_number.cpp index 166c98c07..3b7e67cc5 100644 --- a/math/armstrong_number.cpp +++ b/math/armstrong_number.cpp @@ -1,6 +1,6 @@ /** * @file - * \brief A C++ program to check whether a number is armstrong number or not. + * \brief Program to check if a number is an [Armstrong/Narcissistic number](https://en.wikipedia.org/wiki/Narcissistic_number) in decimal system. * * \details * Armstrong number or [Narcissistic number](https://en.wikipedia.org/wiki/Narcissistic_number) From 06ca2a69532bb1bc17173d14804a2329450fe2e8 Mon Sep 17 00:00:00 2001 From: Neeraj C <35414531+iamnambiar@users.noreply.github.com> Date: Thu, 25 Jun 2020 08:00:37 +0530 Subject: [PATCH 4/5] fix: spaces between include and header file Co-authored-by: David Leal --- math/armstrong_number.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/math/armstrong_number.cpp b/math/armstrong_number.cpp index 3b7e67cc5..f8c7510e8 100644 --- a/math/armstrong_number.cpp +++ b/math/armstrong_number.cpp @@ -7,9 +7,9 @@ * is a number that is the sum of its own digits raised to the power of the number of digits. * @author iamnambiar */ -#include -#include -#include +#include +#include +#include /** * Function to calculate the total number of digits in the number. From 06f425493b1567a7394d533dda525b4d64583be9 Mon Sep 17 00:00:00 2001 From: Neeraj C <35414531+iamnambiar@users.noreply.github.com> Date: Thu, 25 Jun 2020 10:59:44 +0530 Subject: [PATCH 5/5] fix: changed to std::pow Co-authored-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> --- math/armstrong_number.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/math/armstrong_number.cpp b/math/armstrong_number.cpp index f8c7510e8..db8eb0fb7 100644 --- a/math/armstrong_number.cpp +++ b/math/armstrong_number.cpp @@ -43,7 +43,7 @@ bool is_armstrong(int number) { while (temp > 0) { int rem = temp % 10; // Finding each digit raised to the power total digit and add it to the total sum - sum = sum + pow(rem, total_digits); + sum = sum + std::pow(rem, total_digits); temp = temp / 10; } return number == sum;