Home > Python > Basic Python Programs

Ch. 3.7 Finding Factors of an Integer

Chapters Index
Program 1

Finding Factors of an Integer

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.

Mathematical Problem Formulation

Finding Factors of n

n = 150: The variable n is initialized with the value 150. This is the number for which we want to find factors.

Initialize an Empty List:

factors = []: An empty list named factors is created to store the factors of \( n \).

Loop through Possible Factors:

The for loop iterates through all integers from 1 to int(n) + 1: \( \text{int}(\sqrt{n}) \) calculates the integer value of the square root of \( n \) (which is \( \sqrt{150} \approx 12.247 \), so the loop will run until 12). By looping only up to the square root, we can significantly reduce the number of iterations compared to looping all the way to \( n \). This works because factors larger than the square root will always have corresponding smaller factors.

Check for Factors:

Inside the loop, the condition if n % i == 0: checks if \( n \) is divisible by \( i \): If this condition is true, it means \( i \) is a factor of \( n \), and it is added to the factors list.

Add the Corresponding Factor:

The line if i != n // i: checks if \( i \) and its corresponding factor \( \frac{n}{i} \) are different: If they are different, the corresponding factor (which is \( n \) divided by \( i \)) is also added to the factors list. This prevents adding the square root twice if \( n \) is a perfect square.

Theoretical Principles & Computational Physics Guide

Integer Factorization: Divisor Identification & Prime Decomposition

Dr. Alorika Chatterjee & Dr. Anirban Shaw Science & Physics Learners Python 3 / NumPy / SciPy

1. Core Principles & Theoretical Framework

Canonical Decomposition: Every integer $N > 1$ factors uniquely into a product of prime powers: $N = \prod p_i^{\alpha_i}$.
Divisor Pairs: Factors appear in symmetrical pairs $(d, N/d)$. Finding one immediately yields the conjugate factor.
Resonance & Symmetry: Factorization determines commensurability in coupled oscillator modes and crystallographic unit cell subdivisions.

2. Mathematical Formulation & Algorithmic Pipeline

Fundamental Theorem of Arithmetic:

$$ N = p_1^{\alpha_1} p_2^{\alpha_2} \cdots p_k^{\alpha_k}, \quad \text{Number of Divisors: } d(N) = \prod_{i=1}^k (\alpha_i + 1) $$

3. Numerical Mechanics & Implementation Logic

  • Trial Factorization: Test potential divisor $d = 2$. While $N \pmod d == 0$, record $d$ and replace $N \leftarrow N / d$.
  • Odd Factors: Increment $d$ by 2 (checking $3, 5, 7, \dots$) up to $\lfloor\sqrt{N}\rfloor$.
  • Prime Remainder: If $N > 1$ after loop, the remaining $N$ is itself the final prime factor.

4. Relevant Computational Practice Exercises

Exercise 1
Write a script that takes an integer $N$ and prints its full prime factorization in standard power format (e.g. $360 = 2^3 \times 3^2 \times 5^1$).
Exercise 2
Find all proper divisors of $N = 28$ and verify whether it is a mathematically perfect number (sum of divisors equals $N$).
Exercise 3
Compute the greatest common divisor $\gcd(a, b)$ of two large numbers using the Euclidean algorithm.
Standard Literature & References:
  • G. H. Hardy and E. M. Wright, "An Introduction to the Theory of Numbers", Oxford (2008).