[fix/docs]: fit armstrong_number.cpp to guidelines (#2457)

* fix: fit armstrong_number.cpp to guidelines

* chore: delete unnecessary whitespace

* updating DIRECTORY.md

* fit: aliquot_sum to contribution guidelines

added a math formula

* chore: add print statement mentioning tests have passed

* Update math/armstrong_number.cpp

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update math/armstrong_number.cpp

Co-authored-by: David Leal <halfpacho@gmail.com>

---------

Co-authored-by: github-actions[bot] <github-actions@users.noreply.github.com>
Co-authored-by: David Leal <halfpacho@gmail.com>
This commit is contained in:
realstealthninja 2023-06-21 03:30:41 +05:30 committed by GitHub
parent 5dc34677ed
commit 2fd530cba1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 30 deletions

View File

@ -3,10 +3,18 @@
* @brief Program to return the [Aliquot * @brief Program to return the [Aliquot
* Sum](https://en.wikipedia.org/wiki/Aliquot_sum) of a number * Sum](https://en.wikipedia.org/wiki/Aliquot_sum) of a number
* *
* \details * @details
* The Aliquot sum s(n) of a non-negative integer n is the sum of all * 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. * 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) * @author [SpiderMath](https://github.com/SpiderMath)
*/ */
@ -19,8 +27,9 @@
* @namespace math * @namespace math
*/ */
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 * @param num The input number
*/ */
uint64_t aliquot_sum(const uint64_t num) { uint64_t aliquot_sum(const uint64_t num) {
@ -63,6 +72,5 @@ static void test() {
*/ */
int main() { int main() {
test(); // run the self-test implementations test(); // run the self-test implementations
return 0; return 0;
} }

View File

@ -1,20 +1,27 @@
/** /**
* @file * @file
* \brief Program to check if a number is an [Armstrong/Narcissistic * @brief Program to check if a number is an [Armstrong/Narcissistic
* number](https://en.wikipedia.org/wiki/Narcissistic_number) in decimal system. * number](https://en.wikipedia.org/wiki/Narcissistic_number) in decimal system.
* *
* \details * @details
* Armstrong number or [Narcissistic * Armstrong number or [Narcissistic
* number](https://en.wikipedia.org/wiki/Narcissistic_number) is a number that * 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. * is the sum of its own digits raised to the power of the number of digits.
* @author iamnambiar *
* 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> #include <cassert> /// for assert
#include <cmath> #include <cmath> /// for std::pow
#include <iostream> #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 * @param num Number
* @return Total number of digits. * @return Total number of digits.
*/ */
@ -28,16 +35,17 @@ int number_of_digits(int num) {
} }
/** /**
* Function to check whether the number is armstrong number or not. * @brief Function to check whether the number is armstrong number or not.
* @param num Number * @param number to be checked
* @return `true` if the number is armstrong. * @return `true` if the number is armstrong.
* @return `false` if the number is not armstrong. * @return `false` if the number is not armstrong.
*/ */
bool is_armstrong(int number) { 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) { if (number < 0) {
return false; return false;
} }
int sum = 0; int sum = 0;
int temp = number; int temp = number;
// Finding the total number of digits in the number // Finding the total number of digits in the number
@ -46,17 +54,17 @@ bool is_armstrong(int number) {
int rem = temp % 10; int rem = temp % 10;
// Finding each digit raised to the power total digit and add it to the // Finding each digit raised to the power total digit and add it to the
// total sum // total sum
sum = sum + std::pow(rem, total_digits); sum += static_cast<int>(std::pow(rem, total_digits));
temp = temp / 10; temp = temp / 10;
} }
return number == sum; return number == sum;
} }
/** /**
* Function for testing the is_armstrong() function * @brief Self-test implementations
* with all the test cases. * @returns void
*/ */
void test() { static void test() {
// is_armstrong(370) returns true. // is_armstrong(370) returns true.
assert(is_armstrong(370) == true); assert(is_armstrong(370) == true);
// is_armstrong(225) returns false. // is_armstrong(225) returns false.
@ -69,12 +77,15 @@ void test() {
assert(is_armstrong(0) == true); assert(is_armstrong(0) == true);
// is_armstrong(12) returns false. // is_armstrong(12) returns false.
assert(is_armstrong(12) == false); assert(is_armstrong(12) == false);
std::cout << "All tests have successfully passed!\n";
} }
/** /**
* Main Function * @brief Main Function
* @returns 0 on exit
*/ */
int main() { int main() {
test(); test(); // run self-test implementations
return 0; return 0;
} }