猿问

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

目前,我正在实施一种算法,该算法依赖于找到给定数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]。


一只萌萌小番薯
浏览 139回答 2
2回答

波斯汪

也许递归处理它是一个好主意这是我在Python中尝试递归解决方案的尝试:def even_factorization(n):&nbsp; &nbsp; solutions = []&nbsp; &nbsp; def even_divisors(n):&nbsp; # 136 generates [2, 4, 8, 34, 68, 136]&nbsp; &nbsp; &nbsp; &nbsp; return (d for d in range(2, n + 1, 2) if n % d == 0)&nbsp; &nbsp; def remove_similarities(array):&nbsp; # [[2, 2, 34], [2, 34, 2], [34, 2, 2]] -> [[2, 2, 34]]&nbsp; &nbsp; &nbsp; &nbsp; return list(map(list, set(map(lambda a: tuple(sorted(a)), array))))&nbsp; &nbsp; for divisor in even_divisors(n):&nbsp; &nbsp; &nbsp; &nbsp; if divisor == n:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; solutions.append([divisor])&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for solution in even_factorization(n // divisor):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; solutions.append([divisor] + solution)&nbsp; &nbsp; return remove_similarities(solutions)&nbsp; # 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]]

UYOU

在cdlane正确指出我的解决方案中的缺陷后,我收回了原来的解决方案,并将cdlane的优雅python解决方案移植到了javascript中。function even_factorization(n) {&nbsp; let solutions = [];&nbsp; function even_divisors(n) {&nbsp; &nbsp; &nbsp; var divisors = [];&nbsp; &nbsp; &nbsp; for (let i = 2; i <= n; i += 2) {&nbsp; &nbsp; &nbsp; &nbsp; if (n % i === 0) divisors.push(i);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; return divisors;&nbsp; }&nbsp; function remove_similarities(combos) {&nbsp; &nbsp; for (let i = 0; i < combos.length; i++) {&nbsp; &nbsp; &nbsp; for (let j = i + 1; j < combos.length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; if (combos[i].sort((a,b) => a - b).join(" ") === combos[j].sort((a,b) => a - b).join(" ")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; combos.splice(j--,1);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return combos;&nbsp; }&nbsp; even_divisors(n).forEach(divisor => {&nbsp; &nbsp; if (divisor === n)&nbsp; &nbsp; &nbsp; solutions.push([divisor]);&nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; even_factorization(n / divisor).forEach(solution => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; solutions.push([divisor, ...solution]);&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; });&nbsp; 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]]
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答