用给定数量的因子寻找最小数的算法

时间:2012-01-14 11:45:03

标签: algorithm numbers number-theory

什么是最有效的算法,任何人都可以想到这一点,给定一个自然数 n ,返回最小自然数 x n 为正除数(包括1和 x )?例如,给定4算法应该得到6(除数:1,2,3,6);即6是具有4个不同因子的最小数。同样,给定6,算法应该得到12(除数:1,2,3,4,6,12);即12是具有6个不同因子的最小数字

就现实世界的性能而言,我正在寻找一种可扩展的算法,可以在一台可以做10 7的机器上在2秒内给出10 20 的答案。 每秒计算。

2 个答案:

答案 0 :(得分:12)

http://www.primepuzzles.net/problems/prob_019.htm

  

b)Jud McCranie,T.W.A。鲍曼&伊诺赫哈加的发送基本相同   查找给定 d的 N(d)的程序:

     
      
  1. d 分解为其主要除数的乘积: d = p 1 a 1 * p 2 a 2 * p 3 a 3 * ...
  2.   
  3. 将这个因子分解转换为另一个算术上等效的因式分解,由无动力单调减少而不是   necesarilly prime factor ...(uf!...) d = p 1 a 1 * p 2 < / sub> a 2 * p 3 a 3 * ... =   b 1 * b 2 * b 3 ... 使 b 1 ≥b 2 ≥b 3 ...   您必须意识到,对于每个给定的d,有几个   可以做的算术等价的因子分解:例如:
      如果 d = 16 = 2 4 则有5个等效的分解:    d = 2 * 2 * 2 * 2 = 4 * 2 * 2 = 4 * 4 = 8 * 2 = 16
  4.   
  5. N 是计算 2 b 1 -1 * 3 b 2的最小数 -1 * 5 b 3 -1 * ... 表示 d的所有等效分解<。 / em>使用相同的例子:
       N(16)=这些中的最小值{2 * 3 * 5 * 7,2 3 * 3 * 5,2 3 * 3 3 ,2 7 * 3,2 15 } = 2 3 * 3 * 5 = 120
  6.   

更新:数字大约10 20 ,请注意同一页上引用的Christian Bau的笔记。

答案 1 :(得分:2)

//What is the smallest number with X factors?
function smallestNumberWithThisManyFactors(factorCount) {

  Number.prototype.isPrime = function() {
    let primeCandidate = this;
    if(primeCandidate <= 1 || primeCandidate % 1 !== 0) return false
    let i = 2;
    while(i <= Math.floor(Math.sqrt(primeCandidate))){
      if(primeCandidate%i === 0) return false;
      i++;
    }
    return true;
  }

  Number.prototype.nextPrime = function() {
    let currentPrime = this;
    let nextPrimeCandidate = currentPrime + 1
    while(nextPrimeCandidate < Infinity) {
      if(nextPrimeCandidate.isPrime()){
        return nextPrimeCandidate;
      } else {
        nextPrimeCandidate++;
      }
    }
  }

  Number.prototype.primeFactors = function() {
    let factorParent = this;
    let primeFactors = [];
    let primeFactorCandidate = 2;
    while(factorParent !== 1){
      while(factorParent % primeFactorCandidate === 0){
        primeFactors.push(primeFactorCandidate);
        factorParent /= primeFactorCandidate;
      }
      primeFactorCandidate = primeFactorCandidate.nextPrime();
    }
    return primeFactors;
  }

  Number.prototype.factors = function() {
    let parentNumber = this.valueOf();
    let factors = []
    let iterator = parentNumber % 2 === 0 ? 1 : 2

    let factorCandidate = 1;
    for(factorCandidate; factorCandidate <= Math.floor(parentNumber/2); factorCandidate += iterator) {
      if(parentNumber % factorCandidate === 0) {
        factors.push(factorCandidate)
      }
    }
    factors.push(parentNumber)
    return factors
  }

  Array.prototype.valueSort = function() {
    return this.sort(function (a,b){ return a-b })
  }

  function clone3DArray(arrayOfArrays) {
    let cloneArray = arrayOfArrays.map(function(arr) {
      return arr.slice();
    });
    return cloneArray;
  }

  function does3DArrayContainArray(arrayOfArrays, array){
    let aOA = clone3DArray(arrayOfArrays);
    let a = array.slice(0);
    for(let i=0; i<aOA.length; i++){
      if(aOA[i].sort().join(',') === a.sort().join(',')){
        return true;
      }
    }
    return false;
  }

  function removeDuplicateArrays(combinations) {
    let uniqueCombinations = []
    for(let c = 0; c < combinations.length; c++){
      if(!does3DArrayContainArray(uniqueCombinations, combinations[c])){
        uniqueCombinations[uniqueCombinations.length] = combinations[c];
      }
    }
    return uniqueCombinations;
  }

  function generateCombinations(parentArray) {
    let generate = function(n, src, got, combinations) {
      if(n === 0){
        if(got.length > 0){
          combinations[combinations.length] = got;
        }
        return;
      }
      for (let j=0; j<src.length; j++){
        generate(n - 1, src.slice(j + 1), got.concat([src[j]]), combinations);
      }
      return;
    }
    let combinations = [];
    for(let i=1; i<parentArray.length; i++){
      generate(i, parentArray, [], combinations);
    }
    combinations.push(parentArray);
    return combinations;
  }

  function generateCombinedFactorCombinations(primeFactors, primeFactorCombinations) {
    let candidates = [];
    for(let p=0; p<primeFactorCombinations.length; p++){
      let product = 1;
      let primeFactorsCopy = primeFactors.slice(0);
      for(let q=0; q<primeFactorCombinations[p].length; q++){
        product *= primeFactorCombinations[p][q];
        primeFactorsCopy.splice(primeFactorsCopy.indexOf(primeFactorCombinations[p][q]), 1);
      }
      primeFactorsCopy.push(product);
      candidates[candidates.length] = primeFactorsCopy.valueSort().reverse();
    }
    return candidates;
  }

  function determineMinimumCobination (candidates){
    let minimumValue = Infinity;
    let bestFactorCadidate = []
    for(let y=0; y<candidates.length; y++){
      let currentValue = 1;
      let currentPrime = 2;
      for(let z=0; z<combinedFactorCandidates[y].length; z++){
        currentValue *= Math.pow(currentPrime,(combinedFactorCandidates[y][z])-1);
        currentPrime = currentPrime.nextPrime();
      }
      if(currentValue < minimumValue){
        minimumValue = currentValue;
        bestFactorCadidate = combinedFactorCandidates[y];
      }
    }
    return minimumValue;
  }

  let primeFactors = factorCount.primeFactors();
  let primeFactorCombinations = removeDuplicateArrays(generateCombinations(primeFactors));
  let combinedFactorCandidates = generateCombinedFactorCombinations(primeFactors, primeFactorCombinations);
  let smallestNumberWithFactorCount = determineMinimumCobination(combinedFactorCandidates);
  console.log('The smallest number with ' + factorCount + ' factors is: ')
  console.log(smallestNumberWithFactorCount)
  console.log('With these factors being: ')
  console.log(smallestNumberWithFactorCount.factors())

  return smallestNumberWithFactorCount;
}

smallestNumberWithThisManyFactors(10)