当前位置:网站首页>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 ..
边栏推荐
- 1-4 进制表示与转换
- PostgreSQL encounters permission denied in Windows system
- File and multipartfile overview
- 用XGBoost迭代读取数据集
- cookie加密7 fidder分析阶段
- Get the query parameter in the address URL specify the parameter method
- The song of cactus -- throwing stones to ask the way (1)
- 基础知识 | js基础
- [compilation principles] review outline of compilation principles of Shandong University
- oracle的similarity方法实现原理
猜你喜欢
![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](/img/b3/6c8d9fc66e2a4dbdc0dd40179266d3.png)
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

Memory barrier store buffer, invalid queue

正斜杠反斜杠的由来

Interviewer: you use Lombok every day. What is its principle? I can't answer

Interviewer: do you have any plan to request a lot of data that does not exist in redis to destroy the database?

Remote connection raspberry pie in VNC Viewer Mode

Interviewer: please introduce cache penetration, cache null value, cache avalanche and cache breakdown, which are easy to understand

Solve the problem of win10 wsl2 IP change

一個人管理1000臺服務器?這款自動化運維工具一定要掌握

Stream常用操作以及原理探索
随机推荐
Bean copy details
Get the query parameter in the address URL specify the parameter method
Interviewer: do you have any plan to request a lot of data that does not exist in redis to destroy the database?
How to write controller layer code gracefully?
[Software Engineering] software engineering review outline of Shandong University
js输出1-100之间所有的质数并求总个数
Bean拷贝详解
IDEA连接数据库报错
pytorch Default process group is not initialized
js中判断奇偶的函数,求圆面积的函数
多表联查--07--- Hash join
Speech signal processing - concept (I): time spectrum (horizontal axis: time; vertical axis: amplitude), spectrum (horizontal axis: frequency; vertical axis: amplitude) -- Fourier transform -- > time
JDBC参数化查询示例
How to implement redis cache of highly paid programmers & interview questions series 116? How do I find a hot key? What are the possible problems with caching?
One person manages 1000 servers? This automatic operation and maintenance tool must be mastered
攻防演习防御体系构建之第一篇之介绍和防守的四个阶段
请问网页按钮怎么绑定sql语句呀
专业四第二周自测
js中输入三个值,并且由小到大输出
Cookie encryption 6