TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
C语言趣味程序百例精解之java实现(27)自守数
自守数是指一个数的平方的尾数等于该数自身的自然数,例如:
25^2=625 76^2=5776 9376^2=87909376
请求出200000以内的自守数。
程序:
- public class Test27{
- public static void main(String args[]){
- ZiShouShu27();
- }
-
- /**
- * 27.自守数()
- */
- public static void ZiShouShu27() {
- for (long i = 1; i < 200000; i++) {
- long n = getBitCount(i);
- long s = (long)Math.pow(10, n);
- if ((i * i - i) % s == 0)
- System.out.println("i*i=" + i * i + " i=" + i);
- }
- }
- /**
- * 获取一个数的位数
- */
- public static long getBitCount(long n) {
- int i = 1;
- while (n / 10 > 0) {
- i++;
- n /= 10;
- }
- return i;
- }
- }
- 运行:
- C:at>java Test27
- i*i=1 i=1
- i*i=25 i=5
- i*i=36 i=6
- i*i=625 i=25
- i*i=5776 i=76
- i*i=141376 i=376
- i*i=390625 i=625
- i*i=87909376 i=9376
- i*i=8212890625 i=90625
- i*i=11963109376 i=109376
复制代码
源码下载:http://file.javaxxz.com/2014/11/14/000835843.zip |
|