-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
AutomorphicNumber.cs
73 lines (63 loc) · 2.48 KB
/
AutomorphicNumber.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
namespace Algorithms.Numeric;
/// <summary>
/// Calculates Automorphic numbers. A number is said to be an Automorphic number
/// if the square of the number will contain the number itself at the end.
/// </summary>
public static class AutomorphicNumber
{
/// <summary>
/// Generates a list of automorphic numbers that are between <paramref name="lowerBound"/> and <paramref name="upperBound"/>
/// inclusive.
/// </summary>
/// <param name="lowerBound">The lower bound of the list.</param>
/// <param name="upperBound">The upper bound of the list.</param>
/// <returns>A list that contains all of the automorphic numbers between <paramref name="lowerBound"/> and <paramref name="upperBound"/> inclusive.</returns>
/// <exception cref="ArgumentException">If the <paramref name="lowerBound"/>
/// or <paramref name="upperBound"/> is not greater than zero
/// or <paramref name="upperBound"/>is lower than the <paramref name="lowerBound"/>.</exception>
public static IEnumerable<int> GetAutomorphicNumbers(int lowerBound, int upperBound)
{
if (lowerBound < 1)
{
throw new ArgumentException($"Lower bound must be greater than 0.");
}
if (upperBound < 1)
{
throw new ArgumentException($"Upper bound must be greater than 0.");
}
if (lowerBound > upperBound)
{
throw new ArgumentException($"The lower bound must be less than or equal to the upper bound.");
}
return Enumerable.Range(lowerBound, upperBound).Where(IsAutomorphic);
}
/// <summary>
/// Checks if a given natural number is automorphic or not.
/// </summary>
/// <param name="number">The number to check.</param>
/// <returns>True if the number is automorphic, false otherwise.</returns>
/// <exception cref="ArgumentException">If the number is non-positive.</exception>
public static bool IsAutomorphic(int number)
{
if (number < 1)
{
throw new ArgumentException($"An automorphic number must always be positive.");
}
BigInteger square = BigInteger.Pow(number, 2);
// Extract the last digits of both numbers
while (number > 0)
{
if (number % 10 != square % 10)
{
return false;
}
number /= 10;
square /= 10;
}
return true;
}
}