Write a python program to find the prime factorisation of an input number entered by the user.
Don't dare to spam, or I will report your account.
Answers
The given code is written in Python.
n=b=int(input("Enter a number - "))
i=2
a=list()
while n!=1:
if n%i==0:
a.append(i)
n//=i
else:
i+=1
print(b,"= ",end="")
print(*a,sep=" * ")
- Initialise i=2.
- Ask the user for the number (n).
- Loop while n!=1:
- Check if n is divisible by i. If true, append the value of 'i' in the list.
- If false, increment the value of i.
- At last, display the factors separated by ⋆.
Example: Consider a number, say 72.
>> n = 72
n != 1 is true.
>> n%i == 0 is true.
>> If block executes.
a = [2]
>> n = n/i
>> n = 36 [72/2]
>> n = 36
n !=1 is true.
>> n % i == 0 is true.
>> If block executes.
a = [2, 2]
>> n = n/i
>> n = 18 [36/2]
>> n = 18
n != 1 is true.
>> n % i == 0 is true.
>> If block executes.
a = [2, 2, 2]
>> n = n/i
>> n = 9 [18/2]
>> n = 9
>> n != 1 is true.
>> n % i == 0 is false as 9 is not divisible by 2.
>> Else block executes.
>> i = i + 1
>> i = 3
>> n = 9
>> n !=1 is true.
>> n % i == 0 is true as 9 is divisible by 3.
>> If block executes.
a = [2, 2, 2, 3]
>> n = n/i
>> n = 3 [9/3]
>> n = 3
>> n !=1 is true.
>> n % i == 0 is true. as 3 is divisible by 3.
>> If block executes.
>> a = [2, 2, 2, 3, 3]
>> n = n/i
>> n = 1 [3/3]
>> n = 1
>> n != 1 is false.
>> Loop is terminated.
So,
>> 72 = 2 * 2 * 2 * 3 * 3
In this way, problem is solved.