Avatar ·

algorithm for finding all divisors of a number

📁 функция, список, python, пример

Hello! While solving a problem, I had to find online an algorithm for finding all divisors of a number. That is, for eight it should output [1,2,4,8], not [2,2,2] - a list of divisors. I rewrote this algorithm from scratch, and I ask the "senior comrades" to suggest how to improve it. If you have time ))

def divisorss(n):    from collections import Counter    ls = get_ls(n)                  # for n=1568 -> ls = [2, 2, 2, 2, 2, 7, 7]    pairs = dict(Counter(ls))       #  {2: 5, 7: 2}    from itertools import product, starmap    from operator 

                    
                
Avatar ·

For numbers, & denotes bitwise "AND" (). In binary, 2 ** n is a 1 followed by n zeros, and (2 ** n - 1) is n ones. For example, (dec) 32 = (bin) 100000, and (dec) 31 = (bin) 11111. Among all positive numbers, only powers of two have no common bits with the previous number. The code contains an error: it considers 0 to be a power of two.

Log in to leave an answer

Интересные статьи