当前位置:网站首页>JS to determine whether the number entered by the user is a prime number (multiple methods)
JS to determine whether the number entered by the user is a prime number (multiple methods)
2022-06-27 07:35:00 【I am the sun?】
subject :
The user enters a number , Let's judge whether it is a prime number .
Prime number : Can only be 1 And the number divisible by itself ,1 It's neither prime nor sum . Prime number must be greater than 1 The natural number of ( Natural number : 0,1,2,3…)
Method 1 :
Code :
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<script type="text/javascript">
var num = +prompt(" Please enter a number :");
for(var i = 2;i <= num;i++){
if(num == i && num != 1)
{
document.write(num + " Prime number ");
break;
}
else if(num == 1 || num % i == 0 )
{
document.write(num + " Not prime ");
break;
}
}
if(num == 1)
document.write(num + " Not prime ");
</script>
</head>
<body>
</body>
</html>
Running results :





Method 2 :
Make good use of Boolean functions , because fo There is more than one judgment in the loop , therefore , As long as there is one that can 0, Then change to false. Not at last 0, To continue true.
The code is as follows :
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<script type="text/javascript">
var num = prompt(" Please enter a number :");
var flag = true;
if(num < 1){
document.write(" Wrong number entered ;");
}else if(num==1){
document.write("1 Not prime ");
}else{
for(var i = 2;i < num; i++){
if(num % i == 0 )
{
flag = false;
break;
}
}
if(flag){
document.write(num + " Prime number ");
}else{
document.write(num+" Not prime .");
}
}
</script>
</head>
<body>
</body>
</html>
The running results are as follows ..
边栏推荐
- 一個人管理1000臺服務器?這款自動化運維工具一定要掌握
- 正斜杠反斜杠的由来
- R language analyzing wine data
- js成绩奖惩例题
- 【Kevin三连弹之三】Rust真的比C慢吗?进一步分析queen微测评
- log4j:WARN No such property [zipPermission] in org.apache.log4j.RollingFileAppender.
- Origin of forward slash and backslash
- The song of cactus -- throwing stones to ask the way (1)
- 请问如何将数据从oracle导入fastDFS?
- Rust中的Pin详解
猜你喜欢

cookie加密7 fidder分析阶段

(resolved) NPM suddenly reports an error cannot find module 'd:\program files\nodejs\node_ modules\npm\bin\npm-cli. js‘

js输出形状

VNC Viewer方式的远程连接树莓派

js中判断奇偶的函数,求圆面积的函数

One person manages 1000 servers? This automatic operation and maintenance tool must be mastered

js用switch输出成绩是否合格

Basic knowledge | JS Foundation

Rust Async: smol源码分析-Executor篇

Process termination (have you really learned recursion? Test your recursion Foundation)
随机推荐
Memory barrier store buffer, invalid queue
Custom palette for ggplot2
mysql关于自增和不能为空
What is a flotation machine?
pytorch Default process group is not initialized
js求所有水仙花数
Speech signal processing - concept (II): amplitude spectrum (STFT spectrum), Mel spectrum [the deep learning of speech mainly uses amplitude spectrum and Mel spectrum] [extracted with librosa or torch
Get the query parameter in the address URL specify the parameter method
MySQL
[Kevin's third play in a row] is rust really slower than C? Further analyze queen micro assessment
SQL考勤查询间隔一小时
Idea method template
Multi table associated query -- 07 -- hash join
Nature、science、cell旗下刊物
Delay queue `delayqueue`
移动安全工具-jad
js用while循环计算假如投资多年的利率为5%,试求从1000块增长到5000块,需要花费多少年
File and multipartfile overview
Win10 remote connection to ECS
Interviewer: please introduce cache penetration, cache null value, cache avalanche and cache breakdown, which are easy to understand