Program to Find the Factors of a Number in Python and C++ Programming

Python Program to Find the Factors of a Number | C++ Program to Find Factors

Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.

Factors of a number:

When two whole numbers are multiplied, the result is a product. The factors of the product are the numbers we multiply.

In mathematics, a factor is a number or algebraic expression that equally divides another number or expression, leaving no remainder.

The prime number is defined as a number that has only two factors one and itself. Composite numbers are those that contain more than two variables.

Given a number the task is to print the factors of the given number.

Examples:

Example1:

Input:

given_number=360

Output:

printing the factors of given number
1
2
3
4
5
6
8
9
10
12
15
18
20
24
30
36
40
45
60
72
90
120
180
360

Example2:

Input:

given_number=10

Output:

printing the factors of given number
1
2
5
10

Program to Find the Factors of a Number

Below are the ways to find the factors of a number:

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

1)Using for loop to loop from 1 to N in Python

Algorithm:

Divide a positive number “N” by the natural numbers 1 to “N” to find the factor. If a number is divisible by a natural number, the factor is the natural number. A number N can only have factors in the range of 1 to N.

Approach:

  • Assume the input is a number N.
  • Create an iterator variable and set its value to 1.
  • Using an iterator variable to divide the number N
  • It is a factor of the given number N if it is divisible.
  • The iterator variable should be increased.
  • Rep steps 4 and 5 until the iterator variable reaches the value N.

This is the simplest and most straightforward way to find variables in a Python number programme. While declaring the variables, we’ll take a number. Python software that uses a for-loop to find factors of a number and print the factors on the screen.

Below is the implementation:

# given number
number = 360
# printing the factors of given number
print("printing the factors of given number : ")
# using for loop
for i in range(1, number+1):
    # checking if iterator divides the number if so then print it(because it is factor)
    if(number % i == 0):
        print(i)

Output:

printing the factors of given number : 
1
2
3
4
5
6
8
9
10
12
15
18
20
24
30
36
40
45
60
72
90
120
180
360

2)Using for loop to loop from 1 to N in C++

It is similar to method 1 except the syntax. Let us see how to find factors  using for loop in c++

Below is the implementation:

#include <iostream>
using namespace std;
int main()
{ // given number
    int number = 360;
    // printing the factors
    cout << "printing the factors of given number :"
         << endl;
    // using for loop to loop from 1 to N
    for (int i = 1; i <= number; i++)
        // checking if iterator divides the number if so
        // then print it(because it is factor)
        if (number % i == 0)
            cout << i << endl;
    return 0;
}

Output:

printing the factors of given number :
1
2
3
4
5
6
8
9
10
12
15
18
20
24
30
36
40
45
60
72
90
120
180
360

We can see that the outputs of both the programs are same

3)Limitations of running loop from 1 to N

In these two methods the loop runs from 1 to number N.

Hence we can say that the time complexity of above methods are O(n).

What if the number is very large?

Like 10^18 the above methods takes nearly 31 years to execute.

Then How to avoid this?

We can see that the factors of the numbers exist from 1 to N/2 except number itself.

But this also takes nearly 15 yrs to execute.

So to above this we loop till square root of N in next method which gives Time Complexity O(Sqrt(n)).

4)Using while loop to loop from 1 to SQRT(N) in Python

Many of the divisors are present in pairs if we look closely. For eg, if n = 16, the divisors are (1,16), (2,8), and (3,8), (4,4)
We could significantly speed up our program if we took advantage of this fact.
However, if there are two equal divisors, as in the case of, we must be cautious (4, 4). In that case, we’d just print one of them.

We use while loop to loop from 1 to sqrt(number)

Below is the implementation:

# importing math module
import math
# given number
number = 360
# taking a iterator and initlaizing it with 1
i = 1
print("printing the factors of given number : ")
# looping till sqrt(number) using while
while i <= math.sqrt(number):

    if (number % i == 0):

        # If both thee divisors are equal then print only one divisor
        if (number / i == i):
            print(i)
        else:
            # else print both the divisors
            print(i)
            print(number//i)
   # increment the iterator by 1
    i += 1

Output:

printing the factors of given number : 
1
360
2
180
3
120
4
90
5
72
6
60
8
45
9
40
10
36
12
30
15
24
18
20

Related Programs: