var m,b,c=0,j=0; var arr=[]; function fjzys(k){ var i = 2; while(k>=i) { if (k%i == 0) { arr[j] = i; k/=i; i=2; j++; } else { i++; } } } fjzys(100); //求100的质数因子 for(var b = 0; b<(j-1); b++) { console.log(arr[b]); console.log(‘*‘); } console.log(arr[j-1]);
下面是第二种答案
var num = prompt(‘input your number then i will go on:‘,‘90‘); var regex = new RegExp(/^[0-9]*[1-9][0-9]*$/); var i = 2; var handle = num; var arr = new Array(); while(i<handle){ //i小于num则一直循环下去,直到i=num为止 var result = handle/i; if(regex.test(result)){ //除2结果为整数 arr.push(i); i=2; handle=result; }else{ i++; } if(i == handle-1){ //判断是否要结尾 arr.push(handle) break; } } //打印结果 var str = String(); for(i=0;i<arr.length;i++){ str += arr[i]; if(i != arr.length-1) str += ‘*‘; } alert(‘the result :‘+str);
时间: 2024-10-06 09:58:46