Created
March 31, 2016 18:12
-
-
Save makshark/c59fad08d7dfb47d74281c6fa060799f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Compare incomming array with number | |
//Return true if combination is available for simple number | |
function compareValues(arr, number) { | |
var digits = ("" + number).split("").sort(); | |
arr = arr.map(String).sort(); | |
if (arr.length == digits.length) { | |
for (var z = 0; z < digits.length; z++) { | |
if (digits[z] != arr[z]) { | |
//Not equal | |
return false; | |
} | |
} | |
//Equal | |
return true; | |
} else { | |
//Not equal | |
return false; | |
} | |
} | |
//Main findPrime function | |
function findPrime(arr) { | |
if (arr.length == 0) { | |
return console.log(false); | |
} | |
if ((arr.length == 1) && (arr[0] == 2)) { | |
return console.log(2); | |
} | |
var simple = false; | |
var i = 1 + Math.pow(10, arr.length - 1); | |
var max = Math.pow(10, arr.length) - 1; | |
for (i; i < max; i++) { | |
//By default simple is true | |
simple = true; | |
for (var j = 2; j < parseInt(Math.sqrt(i)); j++) { | |
if (i % j == 0) { | |
simple = false; | |
break; | |
} | |
} | |
//If number simple - let to compare it values | |
if (simple) { | |
var result = compareValues(arr, i); | |
if (result) { | |
return console.log(i); | |
break; | |
} | |
} | |
} | |
console.log(false); | |
} | |
findPrime([3]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment