Number.prototype._isPrime = function(val){ let j = val - 1 for(let i = 2;i<val;i++){ if(i>=j) break if(!(val%i) || !(val%j)){ return false } j-- } return true }
<!DOCTYPE html> <html> <head> <meta charset=utf-8> </head> <body> <script type="text/javascript"> // 补全代码 Number.prototype._isPrime = function(){ let num = Number(this); let count =0; if(num > 1){ for(let i =2;i<num;i++){ if(num % i ==0){ count++; } } if(count != 0){ return false; }else{ return true; } } } </script> </body> </html>
// 补全代码 Number.prototype._isPrime=function(val){ if(val<=1){ return false } if(val<=3){//2,3都是质数 return true } for(let i=2;i<Math.sqrt(val);i++){ if(val%i==0){ return false } } return true }
<!DOCTYPE html> <html> <head> <meta charset=utf-8> </head> <body> <script type="text/javascript"> // 补全代码 Number.prototype._isPrime = function(){ let num = Number(this) for(let i=2;i<num;i++){ if(num%2==0) return false } return true; } </script> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset=utf-8> </head> <body> <script type="text/javascript"> // 补全代码 Number.prototype._isPrime =function(){ let number=this.valueOf() if (number==1||number==2||number==3){ return true } let num=Math.floor(Math.sqrt(number)) for(let i=3;i<=num;i++){ if(number%i===0) return false } return true } </script> </body> </html>