-
Notifications
You must be signed in to change notification settings - Fork 0
/
primeNumber.js
64 lines (59 loc) · 1.79 KB
/
primeNumber.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
function isPrimeNumber(num) {
if (num <= 1) {
return false;
}
for (let i = 2; i <= ~~Math.sqrt(num); i++) {
if (num % i === 0) {
console.log(num + " is not a prime number");
return false;
}
}
console.log(num + " is a prime number");
return true;
}
// console.log(isPrimeNumber(-1));
// console.log(isPrimeNumber(1));
// console.log(isPrimeNumber(2));
// console.log(isPrimeNumber(3));
// console.log(isPrimeNumber(4));
// console.log(isPrimeNumber(5));
// console.log(isPrimeNumber(6));
// console.log(isPrimeNumber(7));
// console.log(isPrimeNumber(8));
// console.log(isPrimeNumber(9));
// console.log(isPrimeNumber(10));
// console.log(isPrimeNumber(49));
// console.log(isPrimeNumber(113));
function isPrimeNumberOtherMethod(/** @type {Number} */ num) {
let isPrime;
if (num <= 1) {
return num + " is not a prime number";
}
if (num === 2) {
return num + " is a prime number";
}
for (let i = 2; i < num; i++) {
if (num % i === 0) {
isPrime = false;
break;
}
isPrime = true;
}
return isPrime ? num + " is a prime number" : num + " is not a prime number";
}
console.time("isPrimeNumberOtherMethod");
console.log(isPrimeNumberOtherMethod(-1));
console.log(isPrimeNumberOtherMethod(1));
console.log(isPrimeNumberOtherMethod(2));
console.log(isPrimeNumberOtherMethod(3));
console.log(isPrimeNumberOtherMethod(4));
console.log(isPrimeNumberOtherMethod(5));
console.log(isPrimeNumberOtherMethod(6));
console.log(isPrimeNumberOtherMethod(7));
console.log(isPrimeNumberOtherMethod(8));
console.log(isPrimeNumberOtherMethod(9));
console.log(isPrimeNumberOtherMethod(10));
console.log(isPrimeNumberOtherMethod(49));
console.log(isPrimeNumberOtherMethod(113));
console.log(isPrimeNumberOtherMethod(117));
console.timeEnd("isPrimeNumberOtherMethod");