added a snippet for prime factors in python
This commit is contained in:
31
snippets/python/s/prime-factors.md
Normal file
31
snippets/python/s/prime-factors.md
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
---
|
||||||
|
title: Prime factors of number
|
||||||
|
type: snippet
|
||||||
|
language: python
|
||||||
|
tags: [math,algorithm]
|
||||||
|
cover: dark-leaves-3
|
||||||
|
dateModified: 2023-05-24T00:00:00.000Z
|
||||||
|
---
|
||||||
|
|
||||||
|
Find and return the list containing prime factors of a number.
|
||||||
|
|
||||||
|
- Use a `while` loop to iterate over all possible prime factors, starting with `2`.
|
||||||
|
- If the current `factor`, exactly divides `num`, add `factor` to the `factors` list and divide `num` by `factor`. Otherwise, increment `factor` by one.
|
||||||
|
|
||||||
|
```py
|
||||||
|
def prime_factors(num):
|
||||||
|
factors = []
|
||||||
|
factor = 2
|
||||||
|
while (num >= 2):
|
||||||
|
if (num % factor == 0):
|
||||||
|
factors.append(factor)
|
||||||
|
num = num / factor
|
||||||
|
else:
|
||||||
|
factor += 1
|
||||||
|
return factors
|
||||||
|
```
|
||||||
|
|
||||||
|
```py
|
||||||
|
prime_factors(12) # [2,2,3]
|
||||||
|
prime_factors(42) # [2,3,7]
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user