-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMedium - 17 - Prime Checker.js
60 lines (47 loc) · 1.23 KB
/
Medium - 17 - Prime Checker.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
function PrimeChecker(num) {
function permute(num) {
num = num.toString().split("");
var answerArr = [];
var outerLength = num.length;
var innerLength = 1;
var startItem;
var moveItem;
for (i = 1; i < num.length; i++) {
innerLength *= i;
}
for (i = 0; i < outerLength; i++) {
startItem = num[i];
num.splice(i, 1);
for (j = 0; j < innerLength+1; j++) {
moveItem = num.shift();
num.splice(j, 0, moveItem);
if (j<innerLength) {
answerArr.push(parseInt(startItem + num.join("")));
}
}
num.splice(i, 0, startItem);
}
return answerArr;
}
function isPrime(num) {
for (i = 2; i < num; i++) {
if (num % i === 0) {
return false;
}
}
if (num == 1) {
return false;
}
else {
return true;
}
}
var possibleCombos = permute(num);
var foundPerms = 0;
for (k = 0; k < possibleCombos.length; k++) {
if (isPrime(possibleCombos[k])) {
foundPerms = 1;
}
}
return foundPerms;
}