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 """
315
316 Find all of the factors of a number and return it as a list
317
318 number:
319 The number to find the factors for
320
321 """
322
323 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 """
39
40 def factor(n, i, combi, res):
41 """[summary]
42 helper function
43
44 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 """
53
54 while i * i <= n:
55 if n % i == 0:
56 res += combi + [i, int(n/i)],
57 factor(n/i, i, combi+[i], res)
58 i += 1
59 return res
60 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. By
39 default it'll be rounded up, but you can specify `round_up=False` to have
40 it be rounded down.
41
42 Examples:
43
44 >>> inverse_factorial(100)
45 5
46 >>> inverse_factorial(100, round_up=False)
47 4
48
49 '''
50 assert number >= 0
51 if number == 0:
52 return 0
53 elif number < 1:
54 return int(round_up) # Heh.
55 elif number == 1:
56 return 1
57 else:
58 current_number = 1
59 for multiplier in itertools.count(2):
60 current_number *= multiplier
61 if current_number == number:
62 return multiplier
63 elif current_number > number:
64 return multiplier if round_up else (multiplier - 1)
241def closest_square_factors(integer, larger_first=True):
242
243 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)
248
249 return (1, integer)
17def factor_modulus(n, d, e):
18 """
19 Efficiently recover non-trivial factors of n
20 See: Handbook of Applied Cryptography
21 8.2.2 Security of RSA -> (i) Relation to factoring (p.287)
22 http://www.cacr.math.uwaterloo.ca/hac/
23 """
24 t = (e * d - 1)
25 s = 0
26
27 while True:
28 quotient, remainder = divmod(t, 2)
29
30 if remainder != 0:
31 break
32
33 s += 1
34 t = quotient
35
36 found = False
37
38 while not found:
39 i = 1
40 a = random.randint(1,n-1)
41
42 while i <= 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)
45
46 found = c1 != 1 and c1 != (-1 % n) and c2 == 1
47
48 i += 1
49
50 p = fractions.gcd(c1-1, n)
51 q = n // p
52
53 return p, q
746def prime_number_factorisation(n):
747 if n < 2:
748 return [n]
749 i = 2
750 factors = []
751 while i * i <= n:
752 if n % i:
753 i += 1
754 else:
755 n //= i
756 factors.append(i)
757 if n > 1:
758 factors.append(n)
759 return factors

Related snippets