-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrimeChecker.js
69 lines (56 loc) · 1.43 KB
/
PrimeChecker.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
65
66
67
68
69
/*
Have the function PrimeChecker(num) take num and return 1 if any arrangement of num comes out to be a prime number, otherwise return 0. For example: if num is 910, the output should be 1 because 910 can be arranged into 109 or 019, both of which are primes.
Use the Parameter Testing feature in the box below to test your code with different arguments.
*/
function PrimeChecker(num) {
num = num.toString();
function isPrime(x) {
if (x==1) {
return false;
}
if (x==2) {
return true;
}
for (var i=2; i<x; i++) {
if (x%i==0) {
return false;
}
}
return true;
}
function factorial(x) {
var product = 1;
for (var i=1; i<=x; i++) {
product *= i;
}
return product;
}
var array_permutations = [];
function permutate(array, x, y) {
var newstring = '';
for (var i=x; i<(num.length)*(num.length); i=i+y) {
if (i>=num.length) {
i = i%num.length;
}
newstring += num[i];
if (newstring.length == num.length) {
array.push(newstring);
return;
}
}
}
for (var i=0; i<num.length; i++) {
for (var j=1; j<num.length; j++) {
permutate(array_permutations, i, j);
}
}
for(var i=0; i<array_permutations.length; i++) {
if (isPrime(Number(array_permutations[i]))) {
return '1';
}
}
return '0';
}
// keep this function call here
// to see how to enter arguments in JavaScript scroll down
PrimeChecker(readline());