# 6 examples of 'how to find factors of a number in python' in Python

Every line of 'how to find factors of a number in python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure. ## All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
``313def factors(number):314    """315316    Find all of the factors of a number and return it as a list317318    number:319    The number to find the factors for320321    """322323    factors = []324    for i in range(1, number + 1):325        if number % i == 0:326            factors.append(i)327    return factors``
``30def get_factors(n):31    """[summary]32    33    Arguments:34        n {[int]} -- [to analysed number]35    36    Returns:37        [list of lists] -- [all factors of the number n]38    """3940    def factor(n, i, combi, res):41        """[summary]42        helper function4344        Arguments:45            n {[int]} -- [number]46            i {[int]} -- [to tested divisor]47            combi {[list]} -- [catch divisors]48            res {[list]} -- [all factors of the number n]49        50        Returns:51            [list] -- [res]52        """5354        while i * i &lt;= n:55            if n % i == 0:56                res += combi + [i, int(n/i)],57                factor(n/i, i, combi+[i], res)58            i += 159        return res60    return factor(n, 2, [], [])``
``34def inverse_factorial(number, round_up=True):35    '''36    Get the integer that the factorial of would be `number`.37    38    If `number` isn't a factorial of an integer, the result will be rounded. By39    default it'll be rounded up, but you can specify `round_up=False` to have40    it be rounded down.41    42    Examples:43    44        &gt;&gt;&gt; inverse_factorial(100)45        546        &gt;&gt;&gt; inverse_factorial(100, round_up=False)47        448        49    '''50    assert number &gt;= 051    if number == 0:52        return 053    elif number &lt; 1:54        return int(round_up) # Heh.55    elif number == 1:56        return 157    else:58        current_number = 159        for multiplier in itertools.count(2):60            current_number *= multiplier61            if current_number == number:62                return multiplier63            elif current_number &gt; number:64                return multiplier if round_up else (multiplier - 1)``
``241def closest_square_factors(integer, larger_first=True):242243	factors = []244	root = ceil(sqrt(integer))245	for i in range(root, integer+1):246		if integer % i == 0:247			return (i, int(integer / i)) if  larger_first else (int(integer / i), i)248249	return (1, integer)``
``17def factor_modulus(n, d, e):18    """19    Efficiently recover non-trivial factors of n20    See: Handbook of Applied Cryptography21    8.2.2 Security of RSA -&gt; (i) Relation to factoring (p.287)22    http://www.cacr.math.uwaterloo.ca/hac/23    """24    t = (e * d - 1)25    s = 02627    while True:28        quotient, remainder = divmod(t, 2)2930        if remainder != 0:31            break3233        s += 134        t = quotient3536    found = False3738    while not found:39        i = 140        a = random.randint(1,n-1)4142        while i &lt;= s and not found:43            c1 = pow(a, pow(2, i-1, n) * t, n)44            c2 = pow(a, pow(2, i, n) * t, n)4546            found = c1 != 1 and c1 != (-1 % n) and c2 == 14748            i += 14950    p = fractions.gcd(c1-1, n)51    q = n // p5253    return p, q``
``746def prime_number_factorisation(n):747    if n &lt; 2:748        return [n]749    i = 2750    factors = []751    while i * i &lt;= n:752        if n % i:753            i += 1754        else:755            n //= i756            factors.append(i)757    if n &gt; 1:758        factors.append(n)759    return factors``