forked from TheAlgorithms/C-Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeithNumberChecker.cs
56 lines (45 loc) · 1.5 KB
/
KeithNumberChecker.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
using System;
namespace Algorithms.Numeric;
/// <summary>
/// In number theory, a Keith number or repfigit number is a natural number n in a given number base b with k digits such that
/// when a sequence is created such that the first k terms are the k digits of n and each subsequent term is the sum of the
/// previous k terms, n is part of the sequence.
/// </summary>
public static class KeithNumberChecker
{
/// <summary>
/// Checks if a number is a Keith number or not.
/// </summary>
/// <param name="number">Number to check.</param>
/// <returns>True if it is a Keith number; False otherwise.</returns>
public static bool IsKeithNumber(int number)
{
if (number < 0)
{
throw new ArgumentException($"{nameof(number)} cannot be negative");
}
var tempNumber = number;
var stringNumber = number.ToString();
var digitsInNumber = stringNumber.Length;
/* storing the terms of the series */
var termsArray = new int[number];
for (var i = digitsInNumber - 1; i >= 0; i--)
{
termsArray[i] = tempNumber % 10;
tempNumber /= 10;
}
var sum = 0;
var k = digitsInNumber;
while (sum < number)
{
sum = 0;
for (var j = 1; j <= digitsInNumber; j++)
{
sum += termsArray[k - j];
}
termsArray[k] = sum;
k++;
}
return sum == number;
}
}