Prime numbers are the building blocks of all whole numbers, and they protect every secure message sent over the Internet. Testing whether a number is prime, and breaking a number into its prime factors, are two of the oldest problems in computing.
Prime Numbers
Definition: Prime number. A positive integer greater than 1 that has exactly two positive divisors, namely 1 and itself.
The first primes are 2, 3, 5, 7, 11 and 13. A number greater than 1 that is not prime is called composite. The number 1 is neither prime nor composite, because it has only one divisor. The number 2 is the only even prime.
Testing Whether a Number Is Prime
The direct method tries every possible divisor d from 2 upwards. If any d divides n exactly, that is, if n % d == 0, then n is not prime.
It is not necessary to try divisors all the way up to n − 1. Divisors come in pairs: if n = a × b, then one of a and b is at most √n. Table 3.3 shows the pairs for 36. If no divisor is found up to √n, none exists beyond it either. The condition d * d <= n expresses "d is at most √n" without using a square-root function.
Table 3.3: Divisor pairs of 36
| Smaller divisor | Larger divisor | Product |
|---|---|---|
| 1 | 36 | 36 |
| 2 | 18 | 36 |
| 3 | 12 | 36 |
| 4 | 9 | 36 |
| 6 | 6 | 36 |
For n = 97, only the divisors 2 to 9 need be tried, since 10 × 10 exceeds 97.
Prime Factors
Definition: Prime factor. A prime number that divides a given number exactly.
Every integer greater than 1 can be written as a product of primes in exactly one way, apart from the order of the factors. For example, 60 = 2 × 2 × 3 × 5.
Finding the Prime Factors
The method is repeated division.
- Start with d = 2.
- While d divides n exactly, display d and replace n by n / d.
- When d no longer divides n, increase d by 1.
- Repeat until n becomes 1.
Because every factor d is divided out completely before d is increased, the only values of d that can divide what remains are primes. There is no need to test whether d itself is prime.
Example 3.3: Prime test and prime factors
Problem: Read an integer greater than 1. Report whether it is prime, and display its prime factors.
Solution:
- For the prime test, try divisors d while d × d ≤ n; a variable isPrime records the result.
- For the factors, divide out each d as many times as it divides the number.
#include <stdio.h>
int main(void)
{
int n, d, m, isPrime = 1;
printf("Enter an integer greater than 1: ");
scanf("%d", &n);
for (d = 2; d * d <= n; d++)
{
if (n % d == 0)
{
isPrime = 0;
break;
}
}
if (isPrime == 1)
{
printf("\n%d is prime\n", n);
}
else
{
printf("\n%d is not prime\n", n);
}
printf("Prime factors: ");
m = n;
d = 2;
while (m > 1)
{
if (m % d == 0)
{
printf("%d ", d);
m = m / d;
}
else
{
d++;
}
}
printf("\n");
return 0;
}
Input:
60
Output:
Enter an integer greater than 1:
60 is not prime
Prime factors: 2 2 3 5
Result: 60 is divisible by 2, so it is not prime. Dividing out the factors gives 60 → 30 → 15 → 5 → 1, and the prime factors are 2, 2, 3 and 5.
Common mistakes
- Treating 1 as a prime number.
- Testing divisors up to n − 1, which is correct but wastes a great deal of time for large n.
- Increasing d immediately after one successful division, which misses repeated factors such as the second 2 in 60.
Key takeaways
- A prime has exactly two divisors, 1 and itself; 1 is not prime.
- It is enough to test divisors d with d × d ≤ n.
- Every integer greater than 1 has a unique prime factorization.
- Prime factors are found by dividing out each divisor completely before moving to the next.
Practice questions
- Is 91 a prime number?
- Up to which divisor must 53 be tested to confirm that it is prime?
- Write the prime factorization of 84.
Answers to the practice questions
- No. 91 = 7 × 13.
- Up to 7, because 7 × 7 = 49 ≤ 53 and 8 × 8 = 64 > 53.
- 84 = 2 × 2 × 3 × 7.
Sources
- Official BCA syllabus (NEP 2025 scheme), Problem Solving Techniques, course code 25BCA401SEC01, Unit III
