I am trying to implement a function primeFac()
that takes as input a positive integer n
and returns a list containing all the numbers in the prime factorization of n
.
I have gotten this far but I think it would be better to use recursion here, not sure how to create a recursive code here, what would be the base case? to start with.
My code:
def primes(n): primfac = [] d = 2 while (n > 1): if n%d==0: primfac.append(d) # how do I continue from here... ?
What is the list of prime numbers from 1 to 100? Prime numbers from 1 to 100 are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97.
D. 7. Hint: We use the concept of prime factorization and write all prime factors of the given number and collect the powers of each prime factor using the law of exponents. * Any number is said to be a prime number if it has no other factors other than 1 and the number itself.
Factors of 10 are the list of integers that we can split evenly into 10. There are overall 4 factors of 10 i.e. 1, 2, 5 and 10 where 10 is the biggest factor. The Prime Factors of 10 are 1, 2, 5, 10 and its Factors in Pairs are (1, 10) and (2, 5).
A simple trial division:
def primes(n): primfac = [] d = 2 while d*d <= n: while (n % d) == 0: primfac.append(d) # supposing you want multiple factors repeated n //= d d += 1 if n > 1: primfac.append(n) return primfac
with O(sqrt(n))
complexity (worst case). You can easily improve it by special-casing 2 and looping only over odd d
(or special-casing more small primes and looping over fewer possible divisors).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With