Skip to content

Commit 6bab3b3

Browse files
authored
Merge pull request #3 from Sondren1288/Sondren1288-patch-1
Added new functions
2 parents 02231d2 + f944f06 commit 6bab3b3

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

functions.py

+49
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,32 @@ def find_primes(upper_bound):
5151
return primes
5252

5353

54+
def prime_factors(num):
55+
"""
56+
Finds the smallest primes that divide a number.
57+
For unique primes, turn the list into a set.
58+
Example: 120 / 2 = 60
59+
60 / 2 = 30
60+
30 / 2 = 15
61+
15 / 3 = 5
62+
5 is prime.
63+
returns [2, 2, 2, 3, 5]
64+
"""
65+
primes = find_primes(int(math.sqrt(num + 1))) # Finds all primes that might be a factor
66+
factors = []
67+
68+
for prime in primes:
69+
70+
if prime >= num + 1: # If prime is bigger than the number, it is no longer divisible
71+
break # So for long lists, quitting earlier is faster
72+
73+
while num % prime == 0:
74+
num = num // prime # Divides the number by the prime, so that we can see how many times
75+
factors.append(prime) # a number is divisible by a prime
76+
77+
return factors
78+
79+
5480
def factorial(num):
5581
"""
5682
The factorial of a number.
@@ -67,6 +93,29 @@ def factorial(num):
6793
return product
6894

6995

96+
def zeros(dimensions):
97+
"""
98+
Attempted clone of numpy zeros.
99+
Not as quick, but might be useful if long lists are desired
100+
and numpy not available.
101+
"""
102+
x = y = None
103+
try:
104+
y, x = dimensions # If it cannot unpack it as tuple, it will fail
105+
except TypeError:
106+
x = dimensions # If it fails, it will standard to 1 row
107+
y = 1
108+
109+
zeros = []
110+
if y == 1:
111+
zeros += [0]*x
112+
else:
113+
for row in range(y): # Makes rows with columns of 0
114+
zeros.append([0]*x)
115+
116+
return zeros
117+
118+
70119
def louville_distance(precision):
71120
"""
72121
Finds the distance between in zeroes between 2 '1's in the

0 commit comments

Comments
 (0)