Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fit armstrong_number.cpp to guidelines #2457

Merged
merged 12 commits into from
Jun 20, 2023
18 changes: 13 additions & 5 deletions math/aliquot_sum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@
* @brief Program to return the [Aliquot
* Sum](https://en.wikipedia.org/wiki/Aliquot_sum) of a number
*
* \details
* The Aliquot sum s(n) of a non-negative integer n is the sum of all
* @details
* The Aliquot sum \f$s(n)\f$ of a non-negative integer n is the sum of all
* proper divisors of n, that is, all the divisors of n, other than itself.
* For example, the Aliquot sum of 18 = 1 + 2 + 3 + 6 + 9 = 21
*
* Formula:
*
* \f[
* s(n) = \sum_{d|n, d\neq n}d.
* \f]
*
* For example;
* \f$s(18) = 1 + 2 + 3 + 6 + 9 = 21 \f$
*
* @author [SpiderMath](https://github.com/SpiderMath)
*/
Expand All @@ -19,8 +27,9 @@
* @namespace math
*/
namespace math {

/**
* Function to return the aliquot sum of a number
* @brief to return the aliquot sum of a number
* @param num The input number
*/
uint64_t aliquot_sum(const uint64_t num) {
Expand Down Expand Up @@ -63,6 +72,5 @@ static void test() {
*/
int main() {
test(); // run the self-test implementations

return 0;
}
67 changes: 39 additions & 28 deletions math/armstrong_number.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
/**
* @file
* \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) is a number that
* is the sum of its own digits raised to the power of the number of digits.
* @author iamnambiar
*/
#include <cassert>
#include <cmath>
#include <iostream>
* @file
* @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) is a number that
* is the sum of its own digits raised to the power of the number of digits.
*
* let n be the narcissistic number,
* \f[F_b(n) = \sum_{i=0}^{k-1}d_{i}^{k}\f] for
* \f$ b > 1 F_b : \N \to \N \f$ where
* \f$ k = \lfloor log_b n\rfloor is the number of digits in the number in base \f$b\f$, and
* \f$ d_i = \frac{n mod b^{i+1} - n mod b^{i}}{b^{i}} \f$
*
* @author [Neeraj Cherkara](https://github.com/iamnambiar)
*/
#include <cassert> /// for assert
#include <cmath> /// for std::pow
#include <iostream> /// for IO operations

/**
* Function to calculate the total number of digits in the number.
* @brief Function to calculate the total number of digits in the number.
* @param num Number
* @return Total number of digits.
*/
Expand All @@ -28,16 +35,17 @@ int number_of_digits(int num) {
}

/**
* 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.
*/
* @brief Function to check whether the number is armstrong number or not.
* @param number to be checked
* @return `true` if the number is armstrong.
* @return `false` if the number is not armstrong.
*/
realstealthninja marked this conversation as resolved.
Show resolved Hide resolved
bool is_armstrong(int number) {
// If the number is less than 0, then it is not a armstrong number.
// If the number is less than 0, then it is not an armstrong number.
if (number < 0) {
return false;
}

int sum = 0;
int temp = number;
// Finding the total number of digits in the number
Expand All @@ -46,17 +54,17 @@ bool is_armstrong(int number) {
int rem = temp % 10;
// Finding each digit raised to the power total digit and add it to the
// total sum
sum = sum + std::pow(rem, total_digits);
sum += static_cast<int>(std::pow(rem, total_digits));
temp = temp / 10;
}
return number == sum;
}

/**
* Function for testing the is_armstrong() function
* with all the test cases.
*/
void test() {
* @brief Self-test implementations
* @returns void
*/
static void test() {
// is_armstrong(370) returns true.
assert(is_armstrong(370) == true);
// is_armstrong(225) returns false.
realstealthninja marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -69,12 +77,15 @@ void test() {
assert(is_armstrong(0) == true);
// is_armstrong(12) returns false.
assert(is_armstrong(12) == false);

std::cout << "all tests have passed\n";
realstealthninja marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Main Function
*/
* @brief Main Function
* @returns 0 on exit
*/
int main() {
test();
test(); // run self-test implementations
return 0;
}