不知道正确的答案是多少?我的程序的结果是: 最后的数是7 进行了3次相加 程序如下: public class Text{ static int cishu; public static void main(String args[]){ long i = (long)(Math.pow(2009,2009)); System.out.println("最后的数是" + xunHuan(i)); System.out.println("进行了" + cishu + "次相加"); }
public static long xunHuan(long i){ while(i>=10){ i = sum(i); } return i; }
public static int sum(long i){ int total = 0; String str1 = "" + i; int[] a = new int[str1.length()];
for(int j=0;j<str1.length();j++){ char c = str1.charAt(j); a[j] = Character.getNumericValue(c); total = total + a[j]; } cishu++; return total; } }
关注
假设两个两位数ab和cd相乘
(10a+b)*(10c+d) = 100ac+10ad+10bc+bd
根据上面的原理100ac、10ad、10bc的各个位相加是和ac、ad、bc相等的。
也就是和
(a+b)*(c+d) = ac+ad+bc+bd
的各个位相加是相等的。
所以,可以得出结论:两个两位数相乘的结果的各位数字相加是和每个数的各位数相加后再相乘的结果是相等的。
同样可推出任意位数的两个数相乘的结果的各位数字相加结果可以先相加再相乘。
那么2009^2009的各位数相加
和(2+0+0+9)^2009相等(11^2009)
和(1+1)^2009==>2^2009相等。
实际上2^2009也不用乘2009次,比如可以简化成(2^1004)^2 * 2只需要乘1006次,同理可以继续优化下去。
就说到这里吧,结果不用写程序,用笔和纸就可以算出来了。
你的这个论证结果显然是错的,随便举个例子吧
23*24 = 552,5+5+2=12
(2+3)*(2+4)=30
12 !=30
所以(两个两位数相乘的结果的各位数字相加是和每个数的各位数相加后再相乘的结果是相等的),这个结论是错误的,而后面的证明就没有什么意义了!a=b+c
x=y+z
(b的各位数相加的结果和c的各位数相加的结果的和)
与(y的各位数相加的结果和z的各位数相加的结果的和)的结果相等,并不能证明出(a的各位数相加结果和x的各位数相加的结果相等)!
而你错误的意味意味这是相等的,所以你的论证出现了错误
最后的数是7
进行了3次相加
程序如下:
public class Text{
static int cishu;
public static void main(String args[]){
long i = (long)(Math.pow(2009,2009));
System.out.println("最后的数是" + xunHuan(i));
System.out.println("进行了" + cishu + "次相加");
}
public static long xunHuan(long i){
while(i>=10){
i = sum(i);
}
return i;
}
public static int sum(long i){
int total = 0;
String str1 = "" + i;
int[] a = new int[str1.length()];
for(int j=0;j<str1.length();j++){
char c = str1.charAt(j);
a[j] = Character.getNumericValue(c);
total = total + a[j];
}
cishu++;
return total;
}
}
2009^2009 = (2010 - 1)^2009,用二项式定理展开:原式 = 9n + 2009 * 2010^1 * (-1)^2008 + (-1)^2009 = 9(n-223) - 4 = 9m - 4,根据 9 的倍数各个数位的和的特点,原数各个数位的和为 9 - 4 = 5。
我是楼主,上面是我的博客,有一篇文章贴了这个问题的代码,自己在问了这个问题之后写的!
现在看到这个帖子还有人讨论,就把自己的代码拿出来了,见笑!
大家可以参考,运行效率不高,当绝对可以计算出来,只要你耐心等待!