MCPcopy Create free account
hub / github.com/subbarayudu-j/TheAlgorithms-Python / primeFactorization

Function primeFactorization

other/primelib.py:140–182  ·  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

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

Callers 3

greatestPrimeFactorFunction · 0.85
smallestPrimeFactorFunction · 0.85
kgVFunction · 0.85

Calls 1

isPrimeFunction · 0.70

Tested by

no test coverage detected