function factorial( n) { if(n==0) { return 1 } else{ return n*factorial(n-1) } }
function prime(n){ if(n == 0 || n == 1) return false; else{ for(let i = 2; i <= Math.sqrt(n); i++){ if(n % i == 0){ return false; } } return true; } }
function f(n){ var a=0; for(;n>=2;n--) { for(i=2;i<=n-1;i++) if(n%i==0)break; if(i>=n) { a++; } }return a; }
function f(n) { var i,j,k,sum=0,flag1,flag2; for(i=3;i<=n;i++) { flag1=1,flag2=1; for(j=2;j<i;j++) { if(i%j==0) { flag1=0; break; } for(k=2;k<i+2;k++) { if((i+2)%k==0) { flag2=0; break; } } } if(flag1==1&&flag2==1){ sum++; } } return sum;}
function superPrime(n){ if(n == 0 || n == 1) return 0; else{ var array = []; var count = 0; for(let j = n; j >= 2; j--){ let flag = 0; for(let i = 2; i <= Math.sqrt(j); i++){ if(j % i == 0){ flag = 1; break; } } if(flag == 0){ array.push(j); } } if(array[0] != n) return false; var len = n.toString().length; for(let k = 0 ;n >= 10; k++){ n = n / 10; n = Math.floor(n); for(let m = 0; m < array.length; m++){ if(n == array[m]){ count++; } } } if(count == len -1){ return true; } else{ return false; } } }
function f(n){ if(n == 0 || n == 1) return 0; else{ var array = []; var count = 0; var count1 = 0; for(let j = n; j >= 2; j--){ let flag = 0; for(let i = 2; i <= Math.sqrt(j); i++){ if(j % i == 0){ flag = 1; break; } } if(flag == 0){ array.push(j); } } for(var q = 0; q < array.length; q++){ var len = array[q].toString().length; let number = array[q]; for(let k = 0 ;number >= 10; k++){ number = number / 10; number = Math.floor(number); for(let m = 0; m < array.length; m++){ if(number == array[m]){ count++; } } } if(count == len -1){ count1++; } count = 0; } return count1; } }
function superPrime(n){ if(n == 0 || n == 1 || n == 2 || n == 3 || n == 5 || n == 7) return false; //还可以 n < 10 else{ var array = []; var count = 0; for(let j = n; j >= 2; j--){ //从最大值n开始算起判断素数,依次减小逐个判断。 let flag = 0; for(let i = 2; i <= Math.sqrt(j); i++){ if(j % i == 0){ flag = 1; break; } } if(flag == 0){ array.push(j); //将n以内的素数放在array数组内。 } } if(n != array[0]) return false; //如果array[0]不是n的话 说明n不是素数直接返回false var len = n.toString().length; //求出n值的位数 let number = n; let count11 = len - 1; let flag1 = 0; //用于记录从高位的每位数 for(let k = 0 ; number >= 10; k++){ flag1 = Math.floor(number/Math.pow(10,count11)); number = number - (flag1 * Math.pow(10,count11)); //依次减去当前最高位的对应数 for(let m = 0; m < array.length; m++){ if(number == array[m]){ //判断剩下的数是否在素数数组array里面,有则count+1 count++; } } count11 = count11 - 1; } if(count == len - 1){ //因为最高位之前是素数的直接去掉最高位计算下一位,所以对应的位数减1 return true; }else{ return false; } } }
function f(n){ if(n < 10) return 0; else{ var array = []; var count = 0; var count1 = 0; var count111 = 0; for(let j = n; j >= 2; j--){ let flag = 0; for(let i = 2; i <= Math.sqrt(j); i++){ if(j % i == 0){ flag = 1; break; } } if(flag == 0){ array.push(j); } } for(var q = 0; q < array.length; q++){ var len = array[q].toString().length; let number = array[q]; let count11 = len - 1; let flag1 = 0; for(let k = 0 ;number >= 10; k++){ flag1 = Math.floor(number/Math.pow(10,count11)); number = number-flag1*Math.pow(10,count11); for(let m = 0; m < array.length; m++){ if(number == array[m]){ count++; } } count11 = count11 - 1; } if(count == len - 1){ count111++; } count = 0; } return count111-4; } }
var num=0,sum,result=1; function move(n,from,buffer,to) { if (n == 1) { num++; if(sum==num) { result=from; return result; } } else { move (n-1, from, to, buffer); num++; if(sum==num) { result=from; return result; } move (n-1, buffer, from, to); } } function f(n,m) { sum=m; move(n,1,2,3); return result; }
function f(a,b) { var num=0,flag=0,i=0; for(var i=0;i<a.length;i++) { if(a[i]==b) { flag=1; num=i; break; } } if(flag==1) return num; else return -1; }
JavaScript练习题1–10
JavaScript练习题21-25