Issue
I am new to Python and want to create an algorithm that would give an output of a list of prime factors of an input, for example:
Input: factorise(684)
Output: 2 x 2 x 3 x 3 x 19
Or something along those lines
I would need to define a prime number to begin with so the algorithm knows when it has found a prime factor, so was following a function along these lines:
num = 13
if num > 1:
for i in range(2, num//2):
if (num % i) == 0:
print(num, "is not a prime number")
break
else:
print(num, "is a prime number")
else:
print(num, "is not a prime number")
I have based this code on another question, applying it to my code the first issue I have come across is indentation (I'm thinking this is a quick fix) as python reports an error but changing one level has a knock on effect and it's proving tedious to align everything where necessary
Secondly this code is written to produce a written output as opposed to a definition - I was hoping someone could help me adapt the lines beginning 'print' as I was unsure what command to use.
Finally I need to work on bringing this together to create a final algorithm - if there any ideas on how to do this that would be appreciated but if I can form this definition I should have a decent starting point to work with
Solution
This will fix your checking if a number is prime
num = 13
isPrime = True
for i in range(2, num//2):
if (num % i) == 0:
isPrime = False
break
if isPrime:
print(num, "is a prime number")
else:
print(num, "is not a prime number")
Answered By - Sembei Norimaki
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.