MCPcopy Index your code
hub / github.com/geekcomputers/Python / primeFactorization

Function primeFactorization

primelib/primelib.py:153–188  ·  view source on GitHub ↗

input: positive integer 'number' returns a list of the prime number factors of 'number'

(number)

Source from the content-addressed store, hash-verified

151
152
153def primeFactorization(number):
154 """
155 input: positive integer 'number'
156 returns a list of the prime number factors of 'number'
157 """
158
159 # precondition
160 assert isinstance(number, int) and number >= 0, "'number' must been an int and >= 0"
161
162 ans = [] # this list will be returns of the function.
163
164 # potential prime number factors.
165
166 factor = 2
167
168 quotient = number
169
170 if number == 0 or number == 1:
171 ans.append(number)
172
173 # if 'number' not prime then builds the prime factorization of 'number'
174 elif not isPrime(number):
175 while quotient != 1:
176 if isPrime(factor) and (quotient % factor == 0):
177 ans.append(factor)
178 quotient /= factor
179 else:
180 factor += 1
181
182 else:
183 ans.append(number)
184
185 # precondition
186 assert isinstance(ans, list), "'ans' must been from type list"
187
188 return ans
189
190
191# -----------------------------------------

Callers 3

greatestPrimeFactorFunction · 0.85
smallestPrimeFactorFunction · 0.85
kgVFunction · 0.85

Calls 2

isPrimeFunction · 0.85
appendMethod · 0.45

Tested by

no test coverage detected