|
| 1 | +# Prime Factors |
| 2 | + |
| 3 | +## The problem |
| 4 | +Ask the user to enter a number. Then find all the primes factors for the number. |
| 5 | + |
| 6 | +## Hints |
| 7 | +A prime factor is a prime number that could divide a number. |
| 8 | + |
| 9 | +For example, if you think about the number 35. It has two prime factors: 5 and 7. |
| 10 | + |
| 11 | +Both 5 and 7 are prime numbers. And they can divide the number 35. |
| 12 | + |
| 13 | +Similarly, for the number 10, you will have two factors 2 and 5. |
| 14 | + |
| 15 | +To find the factor, you will run a while loop. |
| 16 | + |
| 17 | +Before the loop, add an empty list to store the prime number. And a variable named divisor whose starting value is 2. |
| 18 | + |
| 19 | +In the loop, you will start dividing with the number by the divisor. If the number gets divided, add it in the factors list. |
| 20 | + |
| 21 | +If it is not divided, increase the divisor by 1. |
| 22 | + |
| 23 | +## The Solution |
| 24 | +```python |
| 25 | +def get_prime_factors(n): |
| 26 | + factors = [] |
| 27 | + divisor = 2 |
| 28 | + while n > 2: |
| 29 | + if(n % divisor == 0): |
| 30 | + factors.append(divisor) |
| 31 | + n = n / divisor |
| 32 | + else: |
| 33 | + divisor = divisor + 1 |
| 34 | + return factors |
| 35 | + |
| 36 | +num = int (input('Enter your number: ')) |
| 37 | +factors = get_prime_factors(num) |
| 38 | +print(factors) |
| 39 | +``` |
| 40 | +**[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** |
| 41 | + |
| 42 | +## take Away |
| 43 | +Prime factor is a prime number that divides the provided number. |
| 44 | + |
0 commit comments