How do you find the factor of a number in python?

Source Code

# Python Program to find the factors of a number

# This function computes the factor of the argument passed
def print_factors(x):
   print("The factors of",x,"are:")
   for i in range(1, x + 1):
       if x % i == 0:
           print(i)

num = 320

print_factors(num)

Output

The factors of 320 are:
1
2
4
5
8
10
16
20
32
40
64
80
160
320

Note: To find the factors of another number, change the value of num.

In this program, the number whose factor is to be found is stored in num, which is passed to the print_factors() function. This value is assigned to the variable x in print_factors().

In the function, we use the for loop to iterate from i equal to x. If x is perfectly divisible by i, it's a factor of x.

Here is an example if you want to use the primes number to go a lot faster. These lists are easy to find on the internet. I added comments in the code.

# http://primes.utm.edu/lists/small/10000.txt
# First 10000 primes

_PRIMES = (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 
        31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 
        73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 
        127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 
        179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 
        233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 
        283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 
        353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 
        419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 
        467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 
        547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 
        607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 
        661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 
        739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 
        811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 
        877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 
        947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 
# Mising a lot of primes for the purpose of the example
)


from bisect import bisect_left as _bisect_left
from math import sqrt as _sqrt


def get_factors(n):
    assert isinstance(n, int), "n must be an integer."
    assert n > 0, "n must be greather than zero."
    limit = pow(_PRIMES[-1], 2)
    assert n <= limit, "n is greather then the limit of {0}".format(limit)
    result = set((1, n))
    root = int(_sqrt(n))
    primes = [t for t in get_primes_smaller_than(root + 1) if not n % t]
    result.update(primes)  # Add all the primes factors less or equal to root square
    for t in primes:
        result.update(get_factors(n/t))  # Add all the factors associted for the primes by using the same process
    return sorted(result)


def get_primes_smaller_than(n):
    return _PRIMES[:_bisect_left(_PRIMES, n)]

PythonServer Side ProgrammingProgramming




How do you find the factor of a number in python?

Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

Mohammad Nauman

More Detail

How do you find the factor of a number in python?

Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

MANAS DASGUPTA

More Detail

How do you find the factor of a number in python?

Practical Data Science using Python

22 Lectures 6 hours

MANAS DASGUPTA

More Detail

In order to find factors of a number, we have to run a loop over all numbers from 1 to itself and see if it is divisible.

Example

num=int(input("enter a number"))
factors=[]
for i in range(1,num+1):
    if num%i==0:
       factors.append(i)

print ("Factors of {} = {}".format(num,factors))

If i is able to divide num completely, it is added in the list. Finally the list is diaplayed as the factors of given number

Output

enter a number75
Factors of 75 = [3, 5, 15, 25, 75]

How do you find the factor of a number in python?

Jayashree

Updated on 21-Feb-2020 12:59:32

  • Related Questions & Answers
  • Find minimum sum of factors of number using C++.
  • Find sum of even factors of a number using C++.
  • Find sum of odd factors of a number using C++.
  • Python Program for Find minimum sum of factors of number
  • How to find prime factors of a number in R?
  • Python Program for Find sum of even factors of a number
  • Python Program for Find sum of odd factors of a number
  • Find sum of even factors of a number in Python Program
  • Find all prime factors of a number - JavaScript
  • C++ Program to find sum of even factors of a number?
  • Java Program to find minimum sum of factors of a number
  • Java Program to Find sum of even factors of a number
  • Program to find number of values factors of two set of numbers
  • To find sum of even factors of a number in C++ Program?
  • C Program to Find the minimum sum of factors of a number?

Previous Page Print Page Next Page  

Advertisements

Does Python have a factor function?

factor() method. With the help of sympy. factor() method, we can find the factors of mathematical expressions in the form of variables by using sympy.

How do you find the factors of a number in a for loop in Python?

Python Program to Find the Factors of a Number using For Loop.
Step 1 – Take input number from user..
Step 2 – Iterate For Loop over every number from 1 to the given number..
Step 3 – If the loop iterator evenly divides the provided number i.e. number % i == 0 print it..
Step 4 – Print Result..

How do you find the factor of a number?

What are Factors of a Number?.
By definition of factors, we know, they are the values that divide the original number into equal parts or numbers. ... .
81 ÷ 9 = 9..
Hence, 9 divides 81 into 9 equal parts..
9 + 9 + 9 + 9 + 9 + 9 + 9 + 9 + 9 = 81..
The factors of a prime number are only two, 1 and the number itself..