18 Star 133 Fork 63

编程语言算法集 / C-Sharp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
AutomorphicNumber.cs 2.48 KB
一键复制 编辑 原始数据 按行查看 历史
Gerson Jr 提交于 2023-12-30 10:33 . Switch to file-scoped namespaces (#430)
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;
}
}
C#
1
https://gitee.com/TheAlgorithms/C-Sharp.git
git@gitee.com:TheAlgorithms/C-Sharp.git
TheAlgorithms
C-Sharp
C-Sharp
master

搜索帮助