gpt4 book ai didi

java - 出数位数的平方到89

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:36:28 25 4
gpt4 key购买 nike

<分区>

好吧,我有以下问题:

数字链是通过连续添加一个数字中数字的平方来形成一个新数字,直到它以前出现过为止。例如,

25 → 29 → 85 → 89 → 145 → 42 → 20 → 4 → 16 → 37 → 58 → 89

32 → 13 → 10 → 1 → 1

因此,任何到达 1 或 89 的链都会陷入无限循环。最令人惊奇的是,所有数字都将始终到达 1 或 89。有多少个 n 以下的起始数字会到达 89?

例 1。 f(500) = 423 时间。

例子2。 f(1000) = 857 时间。

例子2。 f(1221) = 1043 时间。

我目前的解决方案是:

public static int Solving(int n) {
int numOf89 = 0;

int oldN = n;
int count = 0;
while (count <= oldN) {
count++;
n = is89(n);
if (n == 89) {

numOf89++;
}
}

return numOf89;
}



public static int is89(int n) {
int result = 0;
int val = 0;
while (n >= 1) {
val = n % 10;
result += val * val;
n = n / 10;
}
return result;
}

如何得到n以下的起始数会到达89?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com