DEV Community

Vasanthakumari N
Vasanthakumari N

Posted on

Python Day 14: Understanding Divisors and Prime Numbers Using While Loops

1. Divide the Tiles Equally

Problem:
A school is preparing a large square floor for its annual function. The floor has N tiles arranged in a single row. The teacher wants to divide these tiles into equal-sized groups without leaving any tile unused. For example, if there are 12 tiles, the teacher can make:

1 group of 12 tiles
2 groups of 6 tiles
3 groups of 4 tiles
4 groups of 3 tiles
6 groups of 2 tiles
12 groups of 1 tile

Write a Python program that takes the number of tiles N and finds all possible group sizes that can divide the tiles equally.

The numbers 1, 2, 3, 4, 6, and 12 are called Divisors (வகுத்திகள்) of 12.

Step 1: Without a Loop

no = 12

if no % 1 == 0:
    print(1)

if no % 2 == 0:
    print(2)

if no % 3 == 0:
    print(3)

if no % 4 == 0:
    print(4)

if no % 5 == 0:
    print(5)
Enter fullscreen mode Exit fullscreen mode

This approach requires writing multiple if conditions.

Step 2: Understanding the Repeated Action

Instead of checking every number separately, we can use a variable called divisor.

no = 12
divisor = 1

while divisor <= no:
    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

Step 3: Optimizing the Loop

The biggest proper divisor of a number cannot be greater than half of that number.

For example:

12 → 6

1000 → 500

10000 → 5000

We can exclude the number itself and check only up to no // 2.

no = 12
divisor = 1
while divisor <= 12:
    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

Here, we are displaying only the proper divisors, excluding 1 and the number itself.

2.The Special Lock Number

A treasure box has a number lock with a number N. The lock designer follows a special rule: A number is considered a Special Number if it can be divided equally into groups in only two ways:

  • One group containing all the items.
  • Groups containing one item each.

For example, if the number is 7, the items can be arranged equally only as:
1 group of 7 or 7 groups of 1. But if the number is 8, it can also be arranged as 2 groups of 4, so it is not a Special Number.

Write a Python program that accepts a number N and determines whether it is a Special Number or not.

Number 7:

  • 1 group of 7
  • 7 groups of 1
  • Therefore, 7 is a Special Number.

Number 8:

  • 1 group of 8
  • 2 groups of 4
  • 4 groups of 2
  • 8 groups of 1

Therefore, 8 is not a Special Number.

In mathematics, this special number is called a Prime Number.

no = 7
divisor = 2
while divisor <= no//2:
    if no % divisor == 0:
        print('Not a prime number')
        break
    divisor = divisor + 1
else:
    print('prime number')

Enter fullscreen mode Exit fullscreen mode
Output:
prime number
Enter fullscreen mode Exit fullscreen mode

Understanding While-Else

Python supports an else block with a while loop.

  • The else block executes when the loop finishes normally.
  • The else block does not execute if the loop terminates using break.

In this example, if a divisor is found, break stops the loop. Otherwise, the loop completes and the else block executes.

This helps us identify prime numbers.

3.Secret VIP Numbers

A security system assigns special numbers to the first few VIP lockers. A number is considered a VIP number if it can be divided exactly by only 1 and itself. The security officer wants the numbers of the first 5 VIP lockers. Write a Python program to display those 5 numbers.

Expected Result: 2, 3, 5, 7, 11

vip_no_count = 0
no = 2
while vip_no_count < 5:
    divisor = 2 #500 times
    count = 0 
    while divisor <=no//2:
        if no % divisor == 0:
#            print("not Prime")
            break
        divisor = divisor + 1
    else:
        print("Prime Number",no)
        vip_no_count = vip_no_count + 1
    no = no + 1

Enter fullscreen mode Exit fullscreen mode
Output:

2
3
5
7
11
Enter fullscreen mode Exit fullscreen mode

Understanding the Logic

  1. Start checking from number 2.
  2. Set divisor = 2 for every new number.
  3. Check whether the number has any divisor other than 1 and itself.
  4. If a divisor is found, break stops the inner loop.
  5. If no divisor is found, the else block executes and prints the prime number.
  6. Increase vip_no_count only when a prime number is found.
  7. Continue until five prime numbers are displayed.

Top comments (0)