SowmiN

burg logo

Python program to calculate the factors of the given number


Let's see the code!

This code is written by me and venkatesh about a year ago

            
                from prime_list import prime_list
                from time import time

                def Is_prime(list,n):
	                if n in list :
		                return True
	                else :
		                return False
		
                def factorize(n):
	                status = 1
	                list = prime_list(n)
	                temp = n
	                factors = []
	                while status == 1 :
		                if Is_prime(list,temp): #step 1
			                factors.append(temp)
			                break
		                for j in list:                   #step 2
			                if temp%j == 0:
				                factors.append(j)
				                temp = int(temp/j)
				                break
		                if temp == 1:               #step 3
			                status = 0
	                return factors

                choice = 1	
                while choice == 1:
	                n = int(input("Enter the number to find it's factors : "))
	                t0 = time()
	                print("The factors of "+str(n)+" is "+str(factorize(n)))
	                t1 = time()
	                print("Time taken : "+str(t1-t0)+" sec")
	                choice = int(input("Enter 1 to continue : "))
            
        

The prime_list function is imported by factorize function!

            
                def prime_list(n):
                    status = 1
                    list = []
                    for j in range(2,n+1): #Counter loop
                        for k in range(2,(int(n**0.5)+1)): #Checker loop
                            if j%k == 0 and j != k:
                                status = 0
                                break
                        if status == 1:
                            list.append(j)
                        else:
                            status = 1
                    return list