首页 > 解决方案 > 在 C 中输出自守数

问题描述

我需要在每行中输出 10 个自同构数 ,5。我写了下面的代码,但它不能正常工作,你能帮我吗?我需要输出的自守数字:“1、5、6、25、76、376、625、9376、90625、109376”。

int n = 10, m = 10, a, b, c;
for ( n = 1; n < 11; n++) {
b = m;
while (n > b) {
    b = b * m;
}
a = (n * n) % b;
if (a == n)
{
    printf("%d ", n);
    if (n % 6 == 0)
        printf("\n");

}
a++;

标签: c

解决方案


有趣的问题。

这就是我想出的。

#include <stdio.h>
#include <math.h>
#include <limits.h>

int main()
{
    int counter = 0;
    int i=1;
    while(counter < 10)
    {
        if (i*i % (int)pow(10,(int)log10(i)+1) == i)
        {
            counter++;
            printf("%d) %d is automorphic because %d * %d == %d\n", counter, i, i, i, i*i);
        }
        ++i;
    }
    return 0;
}

样本输出

Success #stdin #stdout 0s 4516KB
1) 1 is automorphic because 1 * 1 == 1
2) 5 is automorphic because 5 * 5 == 25
3) 6 is automorphic because 6 * 6 == 36
4) 25 is automorphic because 25 * 25 == 625
5) 76 is automorphic because 76 * 76 == 5776
6) 376 is automorphic because 376 * 376 == 141376
7) 625 is automorphic because 625 * 625 == 390625
8) 9376 is automorphic because 9376 * 9376 == 87909376

(由于时间限制,我在找到 8 个值后停止了)


推荐阅读