jaimex8
3/23/2016 - 8:02 PM

Least Common Prime Divisor

Least Common Prime Divisor

function lucasTest(number) {
    var tester=[1,3];
    for (var i = 2; i < number; i++) {
        tester.push(tester[tester.length-1]+tester[tester.length-2]);
    }
    if ((tester[tester.length-1]-1)%number === 0) return true;
    return false;
}  

function leastCommonPrimeDivisor(a, b) {
    var primeList = [];
    for (var i = 2; i < Math.min(a,b); i++) {
        if (lucasTest(i)){
            primeList.push(i);
        }
    }

    if (a < 1 || b < 1){
    return -1;
    }

    if (a % 2 == 0 && b % 2 == 0){
        return 2;
    }
    
    for (var i = 0; i < primeList.length; i++) {
         if (a % primeList[i] == 0 && b % primeList[i] == 0){
            return primeList[i];
         }
     } 

     return -1;
}