Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

obama

var stdin = process.openStdin();
var data = [];
var letters = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
var BCTABNTb_TEKCT = `2
103 31
217 1891 4819 2291 2987 3811 1739 2491 4717 445 65 1079 8383 5353 901 187 649 1003 697 3239 7663 291 123 779 1007 3551 1943 2117 1679 989 3053
10000 25
3292937 175597 18779 50429 375469 1651121 2102 3722 2376497 611683 489059 2328901 3150061 829981 421301 76409 38477 291931 730241 959821 1664197 3057407 4267589 4729181 5335543`;
data = BCTABNTb_TEKCT.toString().split(/\r\n|\r|\n/);
var tests = data[0];
for(var i = 0; i<tests; i++) {
    var s1 = data[i*2+1].split(' ');
    var s2 = data[i*2+2].split(' ').reverse();
    var lastPrime = parseInt(s1[0]);
    var primesCount = parseInt(s1[1]);
    
    var givenPrimes = s2;
    
    var sourceList = [];
    
    var tempList = [];
    
    for(var j = 0; j<primesCount; j++) {
        var n = s2[j];
        var nums = findPrimes(n);
        if(nums.length == 1) {
            nums.push(n/nums[0]);
        }
        tempList.push(nums);
    }
    for(var k = 0; k<primesCount; k++) {
        if(k>0) {
            var arr1 = tempList[k];
            var arr2 = tempList[k-1];
            
            var sameArr = arr1.filter(element => arr2.includes(element));
            var same = sameArr[0];
            if(arr2[0] == same) sourceList.push(arr2[1]);
            else sourceList.push(arr2[1]);
            sourceList.push(same);
            if(k==primesCount-1) {
                if(arr1[0] == same) sourceList.push(arr1[1]);
                else sourceList.push(arr1[0]);
            }
        }
    }
    for(var m = 0; m<sourceList.length; m++) {
        if(sourceList[m] == sourceList[m+1]) {
            sourceList.splice(m+1, 1);
            m--;
        }
    }
    
    sourceList.reverse();
    
    var sorted = sourceList.filter((v,i) => sourceList.indexOf(v) == i);
    sorted.sort((a, b) => a - b);
    
    var msg = [];
    
    for(var h = 0; h<sourceList.length; h++) {
        for(var p = 0; p<sorted.length; p++) {
            if(sorted[p] == sourceList[h]) msg.push(letters[p]);
        }
    }
    console.log('Case #'+(i+1)+': '+msg.join(''));
}

function findPrimes(num) {
    var integer = num,
    primeArray = [],
    isPrime;

    for(i = 2; i <= integer; i++){
      if (integer % i==0) {
        for(var j = 2; j <= i/2; j++) {
          if(i % j == 0) {
            isPrime = false;
          } else {
            isPrime = true;
          }
        }
    
        if (isPrime == true) {
          integer /= i
          primeArray.push(i);
        }
      }   
    }
    
    var result = [];
    for (var k = 0; k < primeArray.length; k++) {
      result.push(primeArray[k]);
    }
    
    return result;
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.