Created
April 10, 2016 04:49
-
-
Save makshark/5dc33f7bd298f204e65a593ea656c6b5 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
<script> | |
//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; | |
} | |
} | |
//Check number is simple or not | |
function checkSimple(number) { | |
for (var j = 2; j <= parseInt(Math.sqrt(number)); j++) { | |
if (number % j == 0) { | |
return false; | |
} | |
} | |
return true; | |
} | |
//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 simpeOutput = false; | |
var i = 1 + Math.pow(10, arr.length - 1); | |
var max = Math.pow(10, arr.length) - 1; | |
for (i; i < max; i++) { | |
var isSimple = checkSimple(i); | |
//If number simple - let to compare it values | |
if (isSimple) { | |
var result = compareValues(arr, i); | |
if (result) { | |
return console.log(i); | |
simpeOutput = true; | |
break; | |
} | |
} | |
} | |
if (simpeOutput === false) { | |
console.log(false); | |
} | |
} | |
findPrime([9,0,1,3]); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment