June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -0,0 +1,34 @@
|
|||
using System;
|
||||
public static class IteratedDigitsSquaring
|
||||
{
|
||||
public static void Main() {
|
||||
Console.WriteLine(Count89s(1_000_000));
|
||||
Console.WriteLine(Count89s(100_000_000));
|
||||
}
|
||||
|
||||
public static int Count89s(int limit) {
|
||||
if (limit < 1) return 0;
|
||||
int[] end = new int[Math.Min(limit, 9 * 9 * 9 + 2)];
|
||||
int result = 0;
|
||||
|
||||
for (int i = 1; i < end.Length; i++) {
|
||||
for (end[i] = i; end[i] != 1 && end[i] != 89; end[i] = SquareDigitSum(end[i])) { }
|
||||
if (end[i] == 89) result++;
|
||||
}
|
||||
for (int i = end.Length; i < limit; i++) {
|
||||
if (end[SquareDigitSum(i)] == 89) result++;
|
||||
}
|
||||
return result;
|
||||
|
||||
int SquareDigitSum(int n) {
|
||||
int sum = 0;
|
||||
while (n > 0) {
|
||||
int digit = n % 10;
|
||||
sum += digit * digit;
|
||||
n /= 10;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue