int i,j;
for(i=5;i<=10000;i++){
j=i*i;
if(i<10) if(i==j%10)System.out.println(i);
if(i>9&&i<100) if(i==j%100)System.out.println(i);
if(i>99&&i<1000)if(i==j%1000)System.out.println(i);
if(i>999)if(i==j%10000)System.out.println(i);
}
10000以内所有的自守数:
1 5 6 25 76 376 625 9376
程序如下:
public class ZishouNumber {
public static void main(String[] args) {
for(int i = 1; i < 10000; i++){
String strI = String.valueOf(i);
String multiStr = String.valueOf(i*i);
String last = multiStr.substring(multiStr.length() - strI.length());
if(last.equals(strI)){
System.out.println(i + "*" + i + "=" + multiStr + "--> " + i + " is Zishoushu");
}
}
}
}
----------------------
1*1=1--> 1 is Zishoushu
5*5=25--> 5 is Zishoushu
6*6=36--> 6 is Zishoushu
25*25=625--> 25 is Zishoushu
76*76=5776--> 76 is Zishoushu
376*376=141376--> 376 is Zishoushu
625*625=390625--> 625 is Zishoushu
9376*9376=87909376--> 9376 is Zishoushu
如果某个数的平方的末尾极为数等于这个数,那么就称这个数为自守数。
显然,5和6是一位自守数(5x5=25 6x6=36)
25x25=625 76x76=5776,所以25和76是两位自守数。
自守数有一个特性,以他为后几位的两个数相乘,乘积的后几位仍是这个自守数。因为5时自守数,所以以5为个位数的两个数相乘,乘积的个位仍然是5;76是自守数,所以以76为后两位数的两个数相乘,其结果的后两位仍是76,如176x576=101376。
虽然0和1的平方的个位数仍然是0和1,但是他们太“平凡”了,研究他们没有意义,所以不算自守数。
三位自守数是625和376,四位自守数是0625和9376,五位自守数是90625和09376......
我们可以看到,(n+1)位的自守数出自n位的自守数。由此得出,如果知道n位的自守数a,那么(n+1)位的自守数应当由a前面加上一个数构成。
实际上,简化一下,还能发现如下规律:
5+6=11
25+76=101
625+376=1001
......
所以,两个n位自守数,他们的和等于10^n+1
JAVA实现:
public class ZishouNumber {
public static void main(String[] args) {
for(int i = 1; i < 10000; i++){
String strI = String.valueOf(i);
String multiStr = String.valueOf(i*i);
String last = multiStr.substring(multiStr.length() - strI.length());
if(last.equals(strI)){
System.out.println(i + "*" + i + "=" + multiStr + "--> " + i + " is Zishoushu");
}
}
}
}
--------------------------
1*1=1--> 1 is Zishoushu
5*5=25--> 5 is Zishoushu
6*6=36--> 6 is Zishoushu
25*25=625--> 25 is Zishoushu
76*76=5776--> 76 is Zishoushu
376*376=141376--> 376 is Zishoushu
625*625=390625--> 625 is Zishoushu
9376*9376=87909376--> 9376 is Zishoushu