广告:这里的 e 是数学常数,自然对数的底数,无限不循环小数。这道题的意思就是,找出 e 中最先出现的 10 位质数,然后可以得出一个网址。进入这个网址会看到 Google 为你出的第二道数学题,成功解锁这步 Google 会告诉你,我们或许是”志同道合“的人,你可以将简历发到这个邮箱,我们一起做点改变世界的事情。
计算 e 值可以通过泰勒公式推导出来:e^x≈1 + x + x^2/2! + x^3/3! +……+ x^n/n! (1) 推导计算过程还包括埃拉托色尼筛选法(the Sieve of Eratosthenes)、线性筛选法的使用。感兴趣的小伙伴可以用代码实现下。
二、编程练习题
1. 斐波那契数列
@Testpublicvoidtest_Fibonacci(){int month =15;// 15个月long f1 =1L, f2 =1L;long f;for(int i =3; i < month; i++){
f = f2;
f2 = f1 + f2;
f1 = f;
System.out.println("第"+ i +"个月的兔子对数: "+ f2);}}
@Testpublicvoidtest_ZhiYinShu(){f(200);}int k =2;publicvoidf(int n){while(k <= n){if(k == n){
System.out.println(n);break;}elseif(n > k && n % k ==
System.out.print(k +"*")
n = n / k;f(n);break;}elseif(n > k && n % k !=
k++;f(n);break;}}}
@Testpublicvoidtest_Prime(){int a =10, b =24;int m =division(a, b);int n = a * b / m;
System.out.println("最大公约数: "+ m);
System.out.println("最小公倍数: "+ n);}publicintdivision(int x,int y){int t;if(x < y){
t = x;
x = y;
y = t;}while(y !=0){if(x == y)return1;else{int k = x % y;
x = y;
y = k;}}return x;}
@Testpublicvoidtest_PerfectSquare(){for(long l =1L; l <100000; l++){if(Math.sqrt((l +100))%1==0){if(Math.sqrt((l +268))%1==0){
System.out.println(l +"加100是一个完全平方数,再加168又是一个完全平方数");}}}}
@Testpublicvoidtest_solution(){
System.out.println("1到1000的完数有: ");for(int i =1; i <1000; i++){int t =0;for(int j =1; j <= i /2; j++){if(i % j ==0){
t = t + j;}}if(t == i){
System.out.print(i +" ");}}}
@Testpublicvoidtest_asum(){long a =2, b =0;
Scanner s =newScanner(System.in);int n = s.nextInt();int i =0;long sum =0;while(i < n){
b = b + a;
sum = sum + b;
a = a *10;++i;}
System.out.println("input number: "+ n);
System.out.println(sum);}
逻辑:定义一个变量b, 赋初值为0;定义一变量sum, 赋初值为0,进入循环后,将a + b 的值赋给b,将sum + b 的值赋给sum;同时,将a 增加十倍, ++ i; 继续循环;循环结束后,输出sum 的值。
11. 无重复三位数
@Testpublicvoidtest_AC(){int count =0;for(int x =1; x <5; x++){for(int y =1; y <5; y++){for(int z =1; z <5; z++){if(x != y && y != z && x != z){
count++;
System.out.print(x *100+ y *10+ z +" ");if(count %4==0){
System.out.println();}}}}}
System.out.println("共有"+ count +"个三位数");}
难度:⭐⭐⭐⭐
题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
逻辑:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去掉不满足条件的排列。
12. 从小到大输出数列
publicclassSmallToBig{publicstaticvoidmain(String[] args){
SmallToBig fnc =newSmallToBig();int a, b, c;
System.out.println("Input 3 numbers:");
a = fnc.input();
b = fnc.input();
c = fnc.input();if(a > b){int t = a;
a = b;
b = t;}if(a > c){int t = a;
a = c;
c = t;}if(b > c){int t = b;
b = c;
c = t;}
System.out.println(a +" "+ b +" "+ c);}publicintinput(){int value =0;
Scanner s =newScanner(System.in);
value = s.nextInt();return value;}publicvoidcompare(int x,int y){// 此方法没用if(x > y){int t = x;
x = y;
y = t;}}}
publicclassFenShu{publicstaticvoidmain(String[] args){int x =2, y =1, t;double sum =0;
DecimalFormat df =newDecimalFormat("#0.0000");for(int i =1; i <=20; i++){
sum +=(double) x / y;
t = y;
y = x;
x = y + t;
System.out.println("第 "+ i +" 次相加,和是 "+ df.format(sum));}}}
publicclassJieCheng{staticlong sum =0;staticlong fac =0;publicstaticvoidmain(String[] args){long sum =0;long fac =1;for(int i =1; i <=10; i++){
fac = fac * i;
sum += fac;}
System.out.println(sum);}}
publicclassHuiWen{publicstaticvoidmain(String[] args){
Scanner s =newScanner(System.in);
System.out.print("请输入一个正整数:");long a = s.nextLong();
String ss = Long.toString(a);char[] ch = ss.toCharArray();boolean is =true;int j = ch.length;for(int i =0; i < j /2; i++){if(ch[i]!= ch[j - i -1]){
is =false;}}if(is ==true){
System.out.println("这是一个回文数");}else{
System.out.println("这不是一个回文数");}}}
难度:⭐⭐⭐
题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。
18. 按顺序输出数列
publicclassShunXu{publicstaticvoidmain(String[] args){
Scanner s =newScanner(System.in);int a = s.nextInt();int b = s.nextInt();int c = s.nextInt();if(a < b){int t = a;
a = b;
b = t;}if(a < c){int t = a;
a = c;
c = t;}if(b < c){int t = b;
b = c;
c = t;}
System.out.println("从大到小的顺序输出:");
System.out.println(a +" "+ b +" "+ c);}}
难度:⭐⭐⭐
题目:输入3个数a,b,c,按大小顺序输出
19. 位置替换
publicclassTiHuan{staticfinalint N =8;publicstaticvoidmain(String[] args){int[] a =newint[N];
Scanner s =newScanner(System.in);int index1 =0, index2 =0;
System.out.println("please input numbers");for(int i =0; i < N; i++){
a[i]= s.nextInt();
System.out.print(a[i]+" ");}int max = a[0], min = a[0];for(int i =0; i < a.length; i++){if(a[i]> max){
max = a[i];
index1 = i;}if(a[i]< min){
min = a[i];
index2 = i;}}if(index1 !=0){int temp = a[0];
a[0]= a[index1];
a[index1]= temp;}if(index2 != a.length -1){int temp = a[a.length -1];
a[a.length -1]= a[index2];
a[index2]= temp;}
System.out.println("after swop:");for(int i =0; i < a.length; i++){
System.out.print(a[i]+" ");}}}
难度:⭐⭐
题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
20. 1的个数
long startTime = System.currentTimeMillis();int num =10000000, saveNum =1, countNum =0, lastNum =0;int copyNum = num;while(num !=0){
lastNum = num %10;
num /=10;if(lastNum ==0){// 如果是0那么正好是少了一次所以num不加1了
countNum += num * saveNum;}elseif(lastNum ==1){// 如果是1说明当前数内少了一次所以num不加1,而且当前1所在位置// 有1的个数,就是去除当前1最高位,剩下位数,的个数。
countNum += num * saveNum + copyNum % saveNum +1;}else{// 如果非1非0.直接用公式计算// abcd...=(abc+1)*1+(ab+1)*10+(a+1)*100+(1)*1000...
countNum +=(num +1)* saveNum;}
saveNum *=10;}
System.out.println("1的个数:"+ countNum);
System.out.println("计算耗时:"+(System.currentTimeMillis()- startTime)+"毫秒");
Programming is one of the most difficult branches of applied mathematics; the poorer mathematicians had better remain pure mathematicians. https://www.cs./users/EWD/transcriptions/EWD04xx/EWD498.html