Skip to content

Commit db64ed7

Browse files
simolab99siriak
andauthored
Add Aliquot Sum, Narcissistic Number (Armstrong Number), Perfect Square, and Perfect Number (#201)
Co-authored-by: Andrii Siriak <siryaka@gmail.com>
1 parent cf23991 commit db64ed7

File tree

9 files changed

+272
-0
lines changed

9 files changed

+272
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using Algorithms.Numeric;
3+
using NUnit.Framework;
4+
5+
namespace Algorithms.Tests.Numeric
6+
{
7+
public static class AliquotTest
8+
{
9+
[Test]
10+
[TestCase(1, 0)]
11+
[TestCase(3, 1)]
12+
[TestCase(25, 6)]
13+
[TestCase(99, 57)]
14+
public static void AliquotSumWork(int number, int expectedAS)
15+
{
16+
// Arrange
17+
18+
// Act
19+
var result = Aliquot.AliquotSum(number);
20+
21+
// Assert
22+
Assert.AreEqual(result, expectedAS);
23+
}
24+
25+
[Test]
26+
[TestCase(-2)]
27+
public static void AliquotSumShouldThrowEx(int number)
28+
{
29+
// Arrange
30+
31+
// Assert
32+
Assert.Throws<ArgumentException>(() => Aliquot.AliquotSum(number));
33+
}
34+
}
35+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Algorithms.Numeric;
2+
using NUnit.Framework;
3+
4+
namespace Algorithms.Tests.Numeric
5+
{
6+
public static class NarcissisticNumberTest
7+
{
8+
[Test]
9+
[TestCase(2, ExpectedResult = true)]
10+
[TestCase(3, ExpectedResult = true)]
11+
[TestCase(28, ExpectedResult = false)]
12+
[TestCase(153, ExpectedResult = true)]
13+
[TestCase(170, ExpectedResult = false)]
14+
[TestCase(371, ExpectedResult = true)]
15+
public static bool NarcissisticNumberWork(int number)
16+
{
17+
// Arrange
18+
19+
// Act
20+
var result = NarcissisticNumber.IsNarcissistic(number);
21+
22+
// Assert
23+
return result;
24+
}
25+
}
26+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using Algorithms.Numeric;
3+
using NUnit.Framework;
4+
5+
namespace Algorithms.Tests.Numeric
6+
{
7+
public static class PerfectNumberTests
8+
{
9+
[Test]
10+
[TestCase(6)]
11+
[TestCase(28)]
12+
[TestCase(496)]
13+
[TestCase(8128)]
14+
public static void PerfectNumberWork(int number)
15+
{
16+
// Arrange
17+
18+
// Act
19+
var result = PerfectNumber.IsPerfectNumber(number);
20+
21+
// Assert
22+
Assert.IsTrue(result);
23+
}
24+
25+
[Test]
26+
[TestCase(-2)]
27+
public static void PerfectNumberShouldThrowEx(int number)
28+
{
29+
// Arrange
30+
31+
// Assert
32+
Assert.Throws<ArgumentException>(() => Aliquot.AliquotSum(number));
33+
}
34+
}
35+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Algorithms.Numeric;
2+
using NUnit.Framework;
3+
4+
namespace Algorithms.Tests.Numeric
5+
{
6+
public static class PerfectSquareTests
7+
{
8+
[Test]
9+
[TestCase(-4, ExpectedResult = false)]
10+
[TestCase(4, ExpectedResult = true)]
11+
[TestCase(9, ExpectedResult = true)]
12+
[TestCase(10, ExpectedResult = false)]
13+
[TestCase(16, ExpectedResult = true)]
14+
[TestCase(70, ExpectedResult = false)]
15+
[TestCase(81, ExpectedResult = true)]
16+
public static bool PerfectSquareWork(int number)
17+
{
18+
// Arrange
19+
20+
// Act
21+
var result = PerfectSquare.IsPerfectSquare(number);
22+
23+
// Assert
24+
return result;
25+
}
26+
}
27+
}

Algorithms/Numeric/Aliquot.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
3+
namespace Algorithms.Numeric
4+
{
5+
/// <summary>
6+
/// In number theory, the aliquot sum s(n) of a positive integer n is the sum of all proper divisors
7+
/// of n, that is, all divisors of n other than n itself. For example, the proper divisors of 15
8+
/// (that is, the positive divisors of 15 that are not equal to 15) are 1, 3 and 5, so the aliquot
9+
/// sum of 15 is 9 i.e. (1 + 3 + 5). Wikipedia: https://en.wikipedia.org/wiki/Aliquot_sum.
10+
/// </summary>
11+
public static class Aliquot
12+
{
13+
/// <summary>
14+
/// Finds the aliquot sum of an integer number.
15+
/// </summary>
16+
/// <param name="number">Positive number.</param>
17+
/// <returns>The Aliquot Sum.</returns>
18+
/// <exception cref="ArgumentException">Error number is not on interval (0.0; int.MaxValue).</exception>
19+
public static int AliquotSum(int number)
20+
{
21+
if (number < 0)
22+
{
23+
throw new ArgumentException($"{nameof(number)} cannot be negative");
24+
}
25+
26+
int sum = 0;
27+
for (int i = 1, limit = number / 2; i <= limit; ++i)
28+
{
29+
if (number % i == 0)
30+
{
31+
sum += i;
32+
}
33+
}
34+
35+
return sum;
36+
}
37+
}
38+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
3+
namespace Algorithms.Numeric
4+
{
5+
/// <summary>
6+
/// A Narcissistic number is equal to the sum of the cubes of its digits. For example, 370 is a
7+
/// Narcissistic number because 3*3*3 + 7*7*7 + 0*0*0 = 370.
8+
/// </summary>
9+
public static class NarcissisticNumber
10+
{
11+
/// <summary>
12+
/// Checks if a number is a Narcissistic number or not.
13+
/// </summary>
14+
/// <param name="number">Number to check.</param>
15+
/// <returns>True if is a Narcissistic number; False otherwise.</returns>
16+
public static bool IsNarcissistic(int number)
17+
{
18+
int sum = 0;
19+
int temp = number;
20+
int numberOfDigits = 0;
21+
while (temp != 0)
22+
{
23+
numberOfDigits++;
24+
temp /= 10;
25+
}
26+
27+
temp = number;
28+
while (number > 0)
29+
{
30+
int remainder = number % 10;
31+
int power = (int)Math.Pow(remainder, numberOfDigits);
32+
33+
sum += power;
34+
number /= 10;
35+
}
36+
37+
return sum == temp;
38+
}
39+
}
40+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
3+
namespace Algorithms.Numeric
4+
{
5+
/// <summary>
6+
/// In number theory, a perfect number is a positive integer that is equal to the sum of its positive
7+
/// divisors, excluding the number itself.For instance, 6 has divisors 1, 2 and 3 (excluding
8+
/// itself), and 1 + 2 + 3 = 6, so 6 is a perfect number.
9+
/// </summary>
10+
public static class PerfectNumber
11+
{
12+
/// <summary>
13+
/// Checks if a number is a perfect number or not.
14+
/// </summary>
15+
/// <param name="number">Number to check.</param>
16+
/// <returns>True if is a perfect number; False otherwise.</returns>
17+
/// <exception cref="ArgumentException">Error number is not on interval (0.0; int.MaxValue).</exception>
18+
public static bool IsPerfectNumber(int number)
19+
{
20+
if (number < 0)
21+
{
22+
throw new ArgumentException($"{nameof(number)} cannot be negative");
23+
}
24+
25+
int sum = 0; /* sum of its positive divisors */
26+
for (int i = 1; i < number; ++i)
27+
{
28+
if (number % i == 0)
29+
{
30+
sum += i;
31+
}
32+
}
33+
34+
return sum == number;
35+
}
36+
}
37+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
3+
namespace Algorithms.Numeric
4+
{
5+
/// <summary>
6+
/// A perfect square is an element of algebraic structure that is equal to the square of another element.
7+
/// </summary>
8+
public static class PerfectSquare
9+
{
10+
/// <summary>
11+
/// Checks if a number is a perfect square or not.
12+
/// </summary>
13+
/// <param name="number">Number too check.</param>
14+
/// <returns>True if is a perfect square; False otherwise.</returns>
15+
public static bool IsPerfectSquare(int number)
16+
{
17+
if (number < 0)
18+
{
19+
return false;
20+
}
21+
22+
int sqrt = (int)Math.Sqrt(number);
23+
return sqrt * sqrt == number;
24+
}
25+
}
26+
}

DIRECTORY.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* Eigenvalue
2424
* [Poweriterationtests](https://github.com/TheAlgorithms/C-Sharp/blob/master/Algorithms.Tests/LinearAlgebra/Eigenvalue/PowerIterationTests.cs)
2525
* Numeric
26+
* [Aliquottest](https://github.com/TheAlgorithms/C-Sharp/blob/master/Algorithms.Tests/Numeric/AliquotTest.cs)
2627
* [Binomialcoefficienttests](https://github.com/TheAlgorithms/C-Sharp/blob/master/Algorithms.Tests/Numeric/BinomialCoefficientTests.cs)
2728
* Decomposition
2829
* [Lutests](https://github.com/TheAlgorithms/C-Sharp/blob/master/Algorithms.Tests/Numeric/Decomposition/LUTests.cs)
@@ -35,6 +36,9 @@
3536
* Greatestcommondivisor
3637
* [Binarygreatestcommondivisorfindertests](https://github.com/TheAlgorithms/C-Sharp/blob/master/Algorithms.Tests/Numeric/GreatestCommonDivisor/BinaryGreatestCommonDivisorFinderTests.cs)
3738
* [Euclideangreatestcommondivisorfindertests](https://github.com/TheAlgorithms/C-Sharp/blob/master/Algorithms.Tests/Numeric/GreatestCommonDivisor/EuclideanGreatestCommonDivisorFinderTests.cs)
39+
* [Narcissisticnumbertest](https://github.com/TheAlgorithms/C-Sharp/blob/master/Algorithms.Tests/Numeric/NarcissisticNumberTest.cs)
40+
* [Perfectnumbertest](https://github.com/TheAlgorithms/C-Sharp/blob/master/Algorithms.Tests/Numeric/PerfectNumberTest.cs)
41+
* [Perfectsquaretest](https://github.com/TheAlgorithms/C-Sharp/blob/master/Algorithms.Tests/Numeric/PerfectSquareTest.cs)
3842
* Pseudoinverse
3943
* [Pseudoinversetests](https://github.com/TheAlgorithms/C-Sharp/blob/master/Algorithms.Tests/Numeric/PseudoInverse/PseudoInverseTests.cs)
4044
* Other
@@ -115,6 +119,7 @@
115119
* Eigenvalue
116120
* [Poweriteration](https://github.com/TheAlgorithms/C-Sharp/blob/master/Algorithms/LinearAlgebra/Eigenvalue/PowerIteration.cs)
117121
* Numeric
122+
* [Aliquot](https://github.com/TheAlgorithms/C-Sharp/blob/master/Algorithms/Numeric/Aliquot.cs)
118123
* [Binomialcoefficient](https://github.com/TheAlgorithms/C-Sharp/blob/master/Algorithms/Numeric/BinomialCoefficient.cs)
119124
* Decomposition
120125
* [Lu](https://github.com/TheAlgorithms/C-Sharp/blob/master/Algorithms/Numeric/Decomposition/LU.cs)
@@ -128,6 +133,9 @@
128133
* [Binarygreatestcommondivisorfinder](https://github.com/TheAlgorithms/C-Sharp/blob/master/Algorithms/Numeric/GreatestCommonDivisor/BinaryGreatestCommonDivisorFinder.cs)
129134
* [Euclideangreatestcommondivisorfinder](https://github.com/TheAlgorithms/C-Sharp/blob/master/Algorithms/Numeric/GreatestCommonDivisor/EuclideanGreatestCommonDivisorFinder.cs)
130135
* [Igreatestcommondivisorfinder](https://github.com/TheAlgorithms/C-Sharp/blob/master/Algorithms/Numeric/GreatestCommonDivisor/IGreatestCommonDivisorFinder.cs)
136+
* [Narcissisticnumber](https://github.com/TheAlgorithms/C-Sharp/blob/master/Algorithms/Numeric/NarcissisticNumber.cs)
137+
* [Perfectnumber](https://github.com/TheAlgorithms/C-Sharp/blob/master/Algorithms/Numeric/PerfectNumber.cs)
138+
* [Perfectsquare](https://github.com/TheAlgorithms/C-Sharp/blob/master/Algorithms/Numeric/PerfectSquare.cs)
131139
* Pseudoinverse
132140
* [Pseudoinverse](https://github.com/TheAlgorithms/C-Sharp/blob/master/Algorithms/Numeric/Pseudoinverse/PseudoInverse.cs)
133141
* Series

0 commit comments

Comments
 (0)