 @virnuls/

Prime Factorisation One of the OCR Coding Challenges

Files
• main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def factorise(number):
# check whether the number is prime
prime = True
factor = 1
# there's no square root in Python; raising a
# number to the power of 1/2 is equivalent
root = int(number**0.5)
# check for factors up to the square root
while prime and factor < root:
factor += 1
# does factor divide exactly into number?
if number % factor == 0:
# if you've found a factor then it's not prime
prime = False
# if it's prime, add it to the list
if prime:
factors.append(number)
# if it's not prime, factorise it and its pair
# (i.e. what you multiply it by to get number)
else:
factorise(factor)
factorise(int(number/factor))

#validate input
value = 0
while value < 1:
value = input("Give me a positive integer:")
try:
value = int(value)
except:
print("That's not a whole number.")
value = 0
# create empty list of factors
factors = []
# call the factorise function
factorise(value)
print("The prime factors of",value,"are",factors)