Don’t worry, unlock all articles / blogs on PrepInsta by just simply logging in on our website
Python program to find factors of a number
September 28, 2022
Find the Factors of a Number in Python
Given an integer Number as input, the objective is to search for all the factors of the Given integer input. Therefore, we write a program to Find the Factors of a Number in Python Language.
Example
Input : 10
Output : 2 5
Find the Factors of a Number in Python Language
Given an integer input the objective is to find all the factors of the given number. To do so we’ll run a loop and check if any number within the range is a factor of the input number. We’ll use loops and recursion to check for factors. While using recursion we’ll form a recursion tree of all the factors branching out of the number which acts as the node. Here are some of the methods to Find the Factors of a Number in Python Language
Method 1 : Using [1, number] as the range
Method 2 : Using [1, sqrt(number)] as the range
We’ll discuss the above mentioned methods in detail in the upcoming sections.
Method 1: Using [1, number] as the Range
Working
In this method we’ll use for loop to iterate through the given range and check for factors of the given number.
For an integer input as number, we perform the following operations
Run a while loop for the condition iteration
Check if the number is divisible by the current iter variable.
Increment the iter variable.
Print out all the factors.
Let’s implement the above mentioned logic in Python Language.
# method to print the divisors
def printDivisors(n) :
i = 1
while i <= n :
if (n % i==0) :
print (i,end=" ")
i = i + 1
# Driver method
print ("The divisors of 100 are: ")
printDivisors(100)
In this method we’ll use loops to check for factors of a number. We’ll run a while loop with the condition i<sqrt(number) and check for factors within the range.
For an integer input number, we perform the following operations,
Run a while loop with condition iter<=sqrt(number).
Check if the number is divisible by the iter variable.
increment the iter variable by 1.
Append the factors if any to the list.
Print out the list containing all the factors.
Let’s implement the above mentioned logic in Python Language.
Time Complexity : O(sqrt(n)) Auxiliary Space : O(1)
import math
# method to print the divisors
def printDivisors(n) :
# Note that this loop runs till square root
i = 1
while i <= math.sqrt(n):
if (n % i == 0) :
# If divisors are equal, print only one
if (n / i == i) :
print (i,end=" ")
else :
# Otherwise print both
print (i , int(n/i), end=" ")
i = i + 1
# Driver method
print ("The divisors of 100 are: ")
printDivisors(100)
Output
The divisors of 100 are:
1 100 2 50 4 25 5 20 10
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
num=int(input())
for i in range(1,num+1):
if num%i==0:
print(i)
num=12
factor=[]
for i in range(1,num+1):
if (num%i==0):
factor.append(i)
print(factor)
num=int(input(“Enter a number : “))
for i in range(1,num+1):
for j in range(1,num+1):
if i*j==num and num%i==0 and num%j==0:
print(i)
n=int(input(“enter a number : “))
l=[i for i in range(1,n) if(n%i==0)]
for j in l:
print(j,end=” “)
n=int(input(‘enter number:’))
for i in range(2,n):
if n%i==0:
print(i)
num=100
print(*[i for i in range(1,num+1) if num%i==0 ])
n = int(input())
for i in range(1,n+1):
if n%i == 0:
print(i,end =(” “))
num=int(input(“enter the number”))
for i in range(2, num+1):
if num<0:
print("sorry no factor")
elif num%i==0:
print("the factor of",num,"is",i)