JS22. 判断质数
描述
请补全JavaScript代码,要求在Number对象的原型对象上添加"_isPrime"函数,该函数判断调用的对象是否为一个质数,是则返回true,否则返回false。HTML/CSS/JavaScript 解法, 执行用时: 1684ms, 内存消耗: 77812KB, 提交时间: 2022-02-08
{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n <head>\n <meta charset=utf-8>\n </head>\n <body>\n <script type=\"text/javascript\">\n \t// 补全代码\n \tNumber.prototype._isPrime = function() {\n const v = this.valueOf();\n if (v <= 1 || !Number.isInteger(v)) return false;\n for(let i = 2, ceil=Math.floor(Math.sqrt(v)); i <= ceil; i += 1) {\n if (v % i === 0) return false;\n }\n return true;\n }\n </script>\n </body>\n</html>","libs":[]}
HTML/CSS/JavaScript 解法, 执行用时: 1749ms, 内存消耗: 77900KB, 提交时间: 2022-01-22
{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n <head>\n <meta charset=utf-8>\n </head>\n <body>\n <script type=\"text/javascript\">\n \t// 补全代码\n \t Number.prototype._isPrime = function(){\n let number = this.valueOf()\n //let num=Number(this);\n if(number < 2){\n return true\n }\n for(let i = 2;i<=Math.sqrt(number);i++){\n if(number % i == 0){\n return false\n }\n }\n return true\n }\n </script>\n </body>\n</html>","libs":[]}
HTML/CSS/JavaScript 解法, 执行用时: 1749ms, 内存消耗: 77900KB, 提交时间: 2021-12-08
{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n <head>\n <meta charset=utf-8>\n </head>\n <body>\n <script type=\"text/javascript\">\n \t// 补全代码\n \tNumber.prototype._isPrime=function(){\n let number = this.valueOf()\n if(number<2) return false\n for(let i=2;i<number;i++){\n //if(i===number) return true\n if(number%i ===0) return false\n }\n return true\n }\n </script>\n </body>\n</html>","libs":[]}
HTML/CSS/JavaScript 解法, 执行用时: 1751ms, 内存消耗: 77924KB, 提交时间: 2021-12-14
{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n <head>\n <meta charset=utf-8>\n </head>\n <body>\n <script type=\"text/javascript\">\n \t// 补全代码\n Number.prototype._isPrime=function(){\n let num = Number(this)\n if(parseInt(num)!==num || num<2) return false\n else if([2,3,5,7].includes(num)) return true\n for(let i=3;i*i<=num;i+=2){\n if(num%i===0) return false\n }\n return true\n }\n \t\n </script>\n </body>\n</html>","libs":[]}
HTML/CSS/JavaScript 解法, 执行用时: 1752ms, 内存消耗: 77836KB, 提交时间: 2021-12-18
{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n <head>\n <meta charset=utf-8>\n </head>\n <body>\n <script type=\"text/javascript\">\n \t// 补全代码\n \tNumber.prototype._isPrime=function(){\n var num=Number(this);\n if(parseInt(num)!=num||num<2)\n return false;\n for(var i=2;i<=Math.sqrt(num);i++){\n if(num%i==0)\n return false;\n }\n return true;\n }\n </script>\n </body>\n</html>","libs":[]}