DEV Community

Mahalakshmi C
Mahalakshmi C

Posted on

Prime Numbers in Python Using While Loop – Divisors and While-Else

Introduction

In Python, a while loop can be used to solve different number-based problems, such as finding divisors, counting factors, checking prime numbers, and displaying a series of prime numbers.

Before understanding prime numbers, let's first learn what a divisor is and how to find the divisors of a number using a while loop.

What Is a Divisor?

A divisor is a number that divides another number exactly without leaving a remainder.

For example, the divisors of 12 are:

1, 2, 3, 4, 6, 12
Enter fullscreen mode Exit fullscreen mode

Because all these numbers divide 12 without a remainder.

In Python, we use the modulus operator % to check the remainder.

  • If no % divisor == 0, the number is divisible by that divisor.
  • If no % divisor != 0, the number is not divisible by that divisor.

Example: Find All Divisors of 12

no = 12
divisor = 1

while divisor <= 12:
    if no % divisor == 0:
        print(divisor)

    divisor = divisor + 1
Enter fullscreen mode Exit fullscreen mode

Output:

1
2
3
4
6
12
Enter fullscreen mode Exit fullscreen mode

Explanation

  • Initially, no = 12 and divisor = 1.
  • The while loop checks each number from 1 to 12.
  • The if condition checks whether 12 is exactly divisible by the current divisor.
  • If the condition is true, the divisor is printed.
  • The divisor increases by 1 in every iteration.

This program displays all the divisors of 12.

2. Finding Divisors Without Checking the Number Itself

We already know that every positive number is divisible by 1 and itself.

For example, the divisors of 12 are 1, 2, 3, 4, 6, and 12.

Instead of checking all numbers up to 12, we can check only up to half of the number to find its proper divisors.

Example: Find Divisors of 12

no = 12
divisor = 2

while divisor <= no // 2:
    if no % divisor == 0:
        print(divisor)

    divisor = divisor + 1
Enter fullscreen mode Exit fullscreen mode

Output:

2
3
4
6
Enter fullscreen mode Exit fullscreen mode

Explanation

  • no // 2 performs floor division.
  • For 12, 12 // 2 gives 6.
  • Therefore, the loop checks divisors from 2 to 6.
  • It does not check 1 or the number itself.

Why do we use no // 2?

For any positive integer greater than 1, a proper divisor other than 1 cannot be greater than half of that number.

For example:

  • 12 → 12 // 2 = 6
  • 100 → 100 // 2 = 50
  • 101 → 101 // 2 = 50

This reduces the number of checks.

3. Counting the Number of Divisors

We can also count how many proper divisors a number has.

For this, we use a variable called count.

Whenever a divisor is found, we increase the count by 1.

Example: Count the Divisors of 100

no = 100
divisor = 2
count = 0

while divisor <= no // 2:
    if no % divisor == 0:
        print(divisor)
        count = count + 1

    divisor = divisor + 1

print("Total divisors:", count)
Enter fullscreen mode Exit fullscreen mode

Output:

2
4
5
10
20
25
50
Total divisors: 7
Enter fullscreen mode Exit fullscreen mode

Explanation

  • Initially, count = 0.
  • Whenever a divisor is found, count increases by 1.
  • After the loop finishes, the final count is printed.

Therefore, 100 has 7 proper divisors other than 1 and itself.

4. What Is a Prime Number?

A prime number is a number greater than 1 that has exactly two distinct positive divisors:

  1. 1
  2. The number itself.

Examples:

  • 2 → Divisors: 1, 2
  • 3 → Divisors: 1, 3
  • 5 → Divisors: 1, 5
  • 7 → Divisors: 1, 7

Numbers such as 4, 6, 8, and 9 are not prime because they have additional divisors.

Important: The number 1 is not a prime number because it has only one positive divisor.

Example: Check Whether 13 Is Prime

no = 13
divisor = 2
count = 0

while divisor <= no // 2:
    if no % divisor == 0:
        print(divisor)
        count = count + 1

    divisor = divisor + 1

print(count)

if count == 0:
    print(no, 'is a prime number')
else:
    print(no, 'is not a prime number')
Enter fullscreen mode Exit fullscreen mode

Output:

0
13 is a prime number
Enter fullscreen mode Exit fullscreen mode

Explanation

  • Initially, count = 0.
  • The loop checks divisors from 2 to 6.
  • None of these numbers divide 13 exactly.
  • Therefore, the count remains 0.
  • Since count == 0, 13 is a prime number.

What Is While-Else in Python?

Python provides a special feature called while-else.

The else block executes when the while loop finishes normally, meaning the loop condition becomes False.

However, if the loop is terminated using break, the else block will not execute.

Syntax

while condition:
    # statements
else:
    # statements
Enter fullscreen mode Exit fullscreen mode

Example: Check Whether 14 Is Prime

no = 14
divisor = 2

while divisor <= no // 2:
    if no % divisor == 0:
        print("Not Prime")
        break

    divisor = divisor + 1
else:
    print("Prime Number")
Enter fullscreen mode Exit fullscreen mode

Output:

Not Prime
Enter fullscreen mode Exit fullscreen mode

Explanation

  • The loop starts checking divisors from 2.
  • When divisor = 2, 14 % 2 == 0.
  • The condition becomes true.
  • Not Prime is printed.
  • The break statement stops the loop.
  • Since the loop stopped using break, the else block is skipped.

Practising these examples helps improve logical thinking and builds a strong foundation in Python programming.

Top comments (0)