Skip to content

Commit bc87fea

Browse files
authored
Merge branch 'master' into check_amicable_pair
2 parents e862c87 + 2fd530c commit bc87fea

File tree

3 files changed

+50
-31
lines changed

3 files changed

+50
-31
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.26.4)
1+
cmake_minimum_required(VERSION 3.9)
22
project(Algorithms_in_C++
33
LANGUAGES CXX
44
VERSION 1.0.0

math/aliquot_sum.cpp

+13-5
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,18 @@
33
* @brief Program to return the [Aliquot
44
* Sum](https://en.wikipedia.org/wiki/Aliquot_sum) of a number
55
*
6-
* \details
7-
* The Aliquot sum s(n) of a non-negative integer n is the sum of all
6+
* @details
7+
* The Aliquot sum \f$s(n)\f$ of a non-negative integer n is the sum of all
88
* proper divisors of n, that is, all the divisors of n, other than itself.
9-
* For example, the Aliquot sum of 18 = 1 + 2 + 3 + 6 + 9 = 21
9+
*
10+
* Formula:
11+
*
12+
* \f[
13+
* s(n) = \sum_{d|n, d\neq n}d.
14+
* \f]
15+
*
16+
* For example;
17+
* \f$s(18) = 1 + 2 + 3 + 6 + 9 = 21 \f$
1018
*
1119
* @author [SpiderMath](https://github.com/SpiderMath)
1220
*/
@@ -19,8 +27,9 @@
1927
* @namespace math
2028
*/
2129
namespace math {
30+
2231
/**
23-
* Function to return the aliquot sum of a number
32+
* @brief to return the aliquot sum of a number
2433
* @param num The input number
2534
*/
2635
uint64_t aliquot_sum(const uint64_t num) {
@@ -63,6 +72,5 @@ static void test() {
6372
*/
6473
int main() {
6574
test(); // run the self-test implementations
66-
6775
return 0;
6876
}

math/armstrong_number.cpp

+36-25
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
/**
2-
* @file
3-
* \brief Program to check if a number is an [Armstrong/Narcissistic
4-
* number](https://en.wikipedia.org/wiki/Narcissistic_number) in decimal system.
5-
*
6-
* \details
7-
* Armstrong number or [Narcissistic
8-
* number](https://en.wikipedia.org/wiki/Narcissistic_number) is a number that
9-
* is the sum of its own digits raised to the power of the number of digits.
10-
* @author iamnambiar
11-
*/
12-
#include <cassert>
13-
#include <cmath>
14-
#include <iostream>
2+
* @file
3+
* @brief Program to check if a number is an [Armstrong/Narcissistic
4+
* number](https://en.wikipedia.org/wiki/Narcissistic_number) in decimal system.
5+
*
6+
* @details
7+
* Armstrong number or [Narcissistic
8+
* number](https://en.wikipedia.org/wiki/Narcissistic_number) is a number that
9+
* is the sum of its own digits raised to the power of the number of digits.
10+
*
11+
* let n be the narcissistic number,
12+
* \f[F_b(n) = \sum_{i=0}^{k-1}d_{i}^{k}\f] for
13+
* \f$ b > 1 F_b : \N \to \N \f$ where
14+
* \f$ k = \lfloor log_b n\rfloor is the number of digits in the number in base \f$b\f$, and
15+
* \f$ d_i = \frac{n mod b^{i+1} - n mod b^{i}}{b^{i}} \f$
16+
*
17+
* @author [Neeraj Cherkara](https://github.com/iamnambiar)
18+
*/
19+
#include <cassert> /// for assert
20+
#include <cmath> /// for std::pow
21+
#include <iostream> /// for IO operations
1522

1623
/**
17-
* Function to calculate the total number of digits in the number.
24+
* @brief Function to calculate the total number of digits in the number.
1825
* @param num Number
1926
* @return Total number of digits.
2027
*/
@@ -28,16 +35,17 @@ int number_of_digits(int num) {
2835
}
2936

3037
/**
31-
* Function to check whether the number is armstrong number or not.
32-
* @param num Number
38+
* @brief Function to check whether the number is armstrong number or not.
39+
* @param number to be checked
3340
* @return `true` if the number is armstrong.
3441
* @return `false` if the number is not armstrong.
3542
*/
3643
bool is_armstrong(int number) {
37-
// If the number is less than 0, then it is not a armstrong number.
44+
// If the number is less than 0, then it is not an armstrong number.
3845
if (number < 0) {
3946
return false;
4047
}
48+
4149
int sum = 0;
4250
int temp = number;
4351
// Finding the total number of digits in the number
@@ -46,17 +54,17 @@ bool is_armstrong(int number) {
4654
int rem = temp % 10;
4755
// Finding each digit raised to the power total digit and add it to the
4856
// total sum
49-
sum = sum + std::pow(rem, total_digits);
57+
sum += static_cast<int>(std::pow(rem, total_digits));
5058
temp = temp / 10;
5159
}
5260
return number == sum;
5361
}
5462

5563
/**
56-
* Function for testing the is_armstrong() function
57-
* with all the test cases.
58-
*/
59-
void test() {
64+
* @brief Self-test implementations
65+
* @returns void
66+
*/
67+
static void test() {
6068
// is_armstrong(370) returns true.
6169
assert(is_armstrong(370) == true);
6270
// is_armstrong(225) returns false.
@@ -69,12 +77,15 @@ void test() {
6977
assert(is_armstrong(0) == true);
7078
// is_armstrong(12) returns false.
7179
assert(is_armstrong(12) == false);
80+
81+
std::cout << "All tests have successfully passed!\n";
7282
}
7383

7484
/**
75-
* Main Function
76-
*/
85+
* @brief Main Function
86+
* @returns 0 on exit
87+
*/
7788
int main() {
78-
test();
89+
test(); // run self-test implementations
7990
return 0;
8091
}

0 commit comments

Comments
 (0)