Search code examples
javascriptpythonpython-3.xnumber-theoryfactorization

Finding all even factorizations of a given number n - javascript python


Currently i am implementing an algorithm that relies on finding all even factorizations of a given number n, including n.

I've tried some things, but overall i am not able to handle the problem well. Maybe its a good idea to handle it recursively, but i am not that good with javascript yet, especially with the higher level aspects of the language which might come in handy.

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
}

This is some code that goes some of the way, but i am not yet able to recursively implement it considering all the right base cases. I also thought that it could be done with a tree like structure in which paths are even factors, but my cs knowledge is pretty bad.

Suggestions in Python are also welcome, but javascript would be best.

Just to make everything more clear: all even factorizations of 136 for example are [[68, 2], [34, 2, 2], [34, 4], [136]].

Thankfull for any help :)


Solution

  • Maybe its a good idea to handle it recursively

    Here's my attempt at a recursive solution in 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
    

    For 136 returns:

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

    for 218960 returns:

    [[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]]