1 Star 0 Fork 63

雁秋 / C-Sharp

forked from 编程语言算法集 / C-Sharp 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
NarcissisticNumberChecker.cs 1.13 KB
一键复制 编辑 原始数据 按行查看 历史
Andrii Siriak 提交于 2021-05-08 21:42 . Cleanup minor things (#221)
using System;
namespace Algorithms.Numeric
{
/// <summary>
/// A Narcissistic number is equal to the sum of the cubes of its digits. For example, 370 is a
/// Narcissistic number because 3*3*3 + 7*7*7 + 0*0*0 = 370.
/// </summary>
public static class NarcissisticNumberChecker
{
/// <summary>
/// Checks if a number is a Narcissistic number or not.
/// </summary>
/// <param name="number">Number to check.</param>
/// <returns>True if is a Narcissistic number; False otherwise.</returns>
public static bool IsNarcissistic(int number)
{
var sum = 0;
var temp = number;
var numberOfDigits = 0;
while (temp != 0)
{
numberOfDigits++;
temp /= 10;
}
temp = number;
while (number > 0)
{
var remainder = number % 10;
var power = (int)Math.Pow(remainder, numberOfDigits);
sum += power;
number /= 10;
}
return sum == temp;
}
}
}
C#
1
https://gitee.com/xyesterday/C-Sharp.git
git@gitee.com:xyesterday/C-Sharp.git
xyesterday
C-Sharp
C-Sharp
master

搜索帮助