查找给定数字n的所有偶数分解-javascript python

时间:2019-05-11 19:53:44

标签: javascript python python-3.x number-theory factorization

当前,我正在实施一种算法,该算法依赖于找到给定数n(包括n)的所有偶数分解。

我已经尝试了一些方法,但总的来说,我无法很好地解决问题。递归处理它可能是一个好主意,但是我对javascript的了解还不够,尤其是在语言的更高层次方面可能会派上用场。

function evens(n) {
evens = []
for (var i = 2; i < n/2 - 1; i++){
    if (i % 2 != 0){
        continue;
    }
    else {
        if ((n/i) % 2 == 0) {
            evens.push([n/i, i])
        }
    }
}
return evens
}

这是某种形式的代码,但是考虑到所有正确的基本情况,我仍无法递归实现它。 我还认为可以使用像树一样的结构来完成,甚至路径也是因素,但是我的CS知识却很糟糕。

也欢迎使用Python提出建议,但是javascript最好。

只是为了让一切更清晰:例如136的所有偶数分解都是[[68,2],[34,2,2],[34,4],[136]]。

非常感谢您的帮助:)

2 个答案:

答案 0 :(得分:1)

在cdlane正确指出我的解决方案中的缺陷之后,我收回了原来的解决方案,并将cdlane的优雅python解决方案移植到了javascript中。

function even_factorization(n) {
  let solutions = [];

  function even_divisors(n) {
      var divisors = [];
      for (let i = 2; i <= n; i += 2) {
        if (n % i === 0) divisors.push(i);
      }
      return divisors;
  }

  function remove_similarities(combos) {
    for (let i = 0; i < combos.length; i++) {
      for (let j = i + 1; j < combos.length; j++) {
        if (combos[i].sort((a,b) => a - b).join(" ") === combos[j].sort((a,b) => a - b).join(" ")) {
          combos.splice(j--,1);
        }
      }
    }
    return combos;
  }

  even_divisors(n).forEach(divisor => {
    if (divisor === n)
      solutions.push([divisor]);
    else {
        even_factorization(n / divisor).forEach(solution => {
          solutions.push([divisor, ...solution]);
        });
    }
  });

  return remove_similarities(solutions);
}

运行218960返回...

  • [[2,2,2,27370],[2,2,10,5474],[2,2,14,3910],[2,2,34,1610],[2,2,46 ,1190],[2,2,70,782],[2,2,170,322],[2,2,230,238],[2,2,54740],[2,4,27370],[2,10,14,782],[2 ,10,34,322],[2,10,46,238],[2,10,10948],[2,14,34,230],[2,14,46,170],[2,14,7820],[2,20 ,5474],[2,28,3910],[2,34,46,70],[2,34,3220],[2,46,2380],[2,68,1610],[2,70 ,1564],[2,92,1190],[2,140,​​782],[2,170,644],[2,230,476],[2,238,460],[2,322,340],[2,109480],[4,10,5474],[4,14 ,3910],[4,34,1610],[4,46,1190],[4,70,782],[4,170,322],[4,230,238],[4,54740],[8,27370],[10,14 ,34,46],[10,14,1564],[10,28,782],[10,34,644],[10,46,476],[10,68,322],[10,92,238],[10,21896], [14,20,782],[14,34,460],[14,46,340],[14,68,230],[14,92,170],[14,15640],[20,34,322],[20,46,238],[20 ,10948],[28,34,230],[28,46,170],[28,7820],[34,46,140],[34,70,92],[34,6440],[40,5474],[46 ,68,70],[46,4760],[56,3910],[68,3220],[70,3128],[92,2380],[136,1610],[140,1564],[170 ,1288],[184,1190],[230,952],[238,920],[280,782],[322,680],[340,644],[460,476],[218960]]

...并以136运行返回...

  • [[2,2,34],[2,68],[4,34],[136]]

答案 1 :(得分:1)

  

也许递归处理它是一个好主意

这是我尝试使用Python进行递归的解决方案:

def even_factorization(n):
    solutions = []

    def even_divisors(n):  # 136 generates [2, 4, 8, 34, 68, 136]
        return (d for d in range(2, n + 1, 2) if n % d == 0)

    def remove_similarities(array):  # [[2, 2, 34], [2, 34, 2], [34, 2, 2]] -> [[2, 2, 34]]
        return list(map(list, set(map(lambda a: tuple(sorted(a)), array))))

    for divisor in even_divisors(n):
        if divisor == n:
            solutions.append([divisor])
        else:
            for solution in even_factorization(n // divisor):
                solutions.append([divisor] + solution)

    return remove_similarities(solutions)  # return 'solutions' to see raw data

对于136个返回:

[[2, 2, 34], [4, 34], [2, 68], [136]]

对于218960返回:

[[184, 1190], [8, 27370], [4, 54740], [2, 70, 1564], [56, 3910], [2, 2, 170, 322],
[280, 782], [70, 3128], [4, 46, 1190], [2, 2, 34, 1610], [2, 14, 34, 230],
[2, 14, 7820], [20, 34, 322], [10, 14, 34, 46], [14, 92, 170], [20, 46, 238],
[218960], [2, 322, 340], [10, 68, 322], [34, 46, 140], [10, 14, 1564],
[2, 10, 10948], [10, 92, 238], [4, 170, 322], [92, 2380], [14, 20, 782],
[10, 21896], [238, 920], [28, 34, 230], [10, 28, 782], [2, 2, 46, 1190],
[2, 28, 3910], [10, 34, 644], [34, 6440], [2, 92, 1190], [46, 4760], [2, 170, 644],
[2, 68, 1610], [4, 70, 782], [340, 644], [2, 34, 46, 70], [2, 20, 5474],
[14, 68, 230], [2, 34, 3220], [4, 34, 1610], [4, 10, 5474], [28, 7820],
[14, 34, 460], [322, 680], [10, 46, 476], [2, 2, 54740], [4, 230, 238],
[2, 2, 2, 27370], [34, 70, 92], [2, 140, 782], [14, 15640], [2, 10, 46, 238],
[2, 10, 14, 782], [2, 14, 46, 170], [2, 238, 460], [136, 1610], [2, 2, 10, 5474],
[20, 10948], [4, 14, 3910], [40, 5474], [2, 2, 70, 782], [2, 2, 230, 238],
[230, 952], [68, 3220], [2, 46, 2380], [2, 230, 476], [2, 10, 34, 322],
[140, 1564], [460, 476], [170, 1288], [2, 4, 27370], [46, 68, 70], [14, 46, 340],
[2, 109480], [28, 46, 170], [2, 2, 14, 3910]]