-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path41.js
31 lines (26 loc) · 781 Bytes
/
41.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
var out = require("./timer");
var numbers = require("./numbers");
var optimus = require("./optimus");
var prime = 1;
var pNum = optimus.getPrime(prime);
function testPermutation(digits) {
var num = +digits.join("");
return optimus.isPrime(num) ? num : null;
}
function getLargest(digits) {
var permutation = numbers.firstPermutation(digits.split(""));
var max = numbers.factorial(digits.length);
var largest = null;
for (var i=0;i<max;i++){
largest = testPermutation(permutation) || largest;
permutation = numbers.lexiPermutation(permutation);
}
return largest;
}
var digits = "123456789";
var winner = null;
while (!winner) {
winner = getLargest(digits);
digits = digits.substr(0,digits.length-1);
}
out.print(winner);