A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
|
|
@ -0,0 +1,2 @@
|
|||
>>> def factors(n):
|
||||
return [i for i in range(1, n + 1) if not n%i]
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
>>> def factors(n):
|
||||
return [i for i in range(1, n//2 + 1) if not n%i] + [n]
|
||||
|
||||
>>> factors(45)
|
||||
[1, 3, 5, 9, 15, 45]
|
||||
14
Task/Factors-of-an-integer/Python/factors-of-an-integer-3.py
Normal file
14
Task/Factors-of-an-integer/Python/factors-of-an-integer-3.py
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
>>> from math import sqrt
|
||||
>>> def factor(n):
|
||||
factors = set()
|
||||
for x in range(1, int(sqrt(n)) + 1):
|
||||
if n % x == 0:
|
||||
factors.add(x)
|
||||
factors.add(n//x)
|
||||
return sorted(factors)
|
||||
|
||||
>>> for i in (45, 53, 64): print( "%i: factors: %s" % (i, factor(i)) )
|
||||
|
||||
45: factors: [1, 3, 5, 9, 15, 45]
|
||||
53: factors: [1, 53]
|
||||
64: factors: [1, 2, 4, 8, 16, 32, 64]
|
||||
13
Task/Factors-of-an-integer/Python/factors-of-an-integer-4.py
Normal file
13
Task/Factors-of-an-integer/Python/factors-of-an-integer-4.py
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
def factor(n):
|
||||
a, r = 1, [1]
|
||||
while a * a < n:
|
||||
a += 1
|
||||
if n % a: continue
|
||||
b, f = 1, []
|
||||
while n % a == 0:
|
||||
n //= a
|
||||
b *= a
|
||||
f += [i * b for i in r]
|
||||
r += f
|
||||
if n > 1: r += [i * n for i in r]
|
||||
return r
|
||||
Loading…
Add table
Add a link
Reference in a new issue