Skip to content

Commit f538625

Browse files
committed
素数
1 parent 8fc463b commit f538625

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

素数.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//素数:因子除了1和它本身的数
2+
3+
//判断是否是素数
4+
function isPrimeNumber(num) {
5+
let sqrt = Math.ceil(Math.sqrt(+num))
6+
7+
for(let i = 2; i <= sqrt; i++){
8+
if(num % i == 0) {
9+
return false
10+
}
11+
}
12+
return true
13+
}
14+
15+
16+
//获取某一范围内所有的素数
17+
function getPrimeNumer(num) {
18+
let arr = []
19+
for(let i = 1; i <= num; i++){
20+
if(isPrimeNumber(i)){
21+
arr.push(i)
22+
}
23+
}
24+
25+
return arr
26+
}

0 commit comments

Comments
 (0)