- 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.
1*12=12 # Remaining 0
2*6=12 # Remaining 0
3*4=12 # Remaining 0
4*3=12 # Remaining 0
5*2=10 # Remaining !=0
6*2=12 # Remaining 0
The numbers 1, 2, 3, 4, 6, and 12 are called Divisors.
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)
This approach requires writing multiple if conditions.
Step 2: We define a variable
we can use a variable called divisor.
no = 12
divisor = 1
while divisor <= no:
if no % divisor == 0:
print(divisor)
divisor = divisor + 1
Output:
1
2
3
4
6
12
Step 3: Optimizing the Loop
The biggest proper divisor of a number cannot be greater than half of that number.
For example:
8 → 4
20 → 10
1000 → 500
10000 → 5000
We can exclude the number itself and check only up to no // 2.
no = 12
divisor = 1
while divisor <= no//2:
if no % divisor == 0:
print(divisor)
divisor = divisor + 1
Output:
2
3
4
6
Find The Special numbers.
no = 2
special_number=0
print("special_number :", end=" ")
while no <20:
divisor=2
while divisor <=no//2:
if no%divisor==0:
break
divisor+=1
else:
print(no,end=" ")
no+=1
OUTPUT: special_number : 2 3 5 7 11 13 17 19
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
print("vip_no_count :",end=" ")
no = 2
while vip_no_count <5:
divisor=2
while divisor <=no//2:
if no%divisor==0:
break
divisor+=1
else:
print(no,end=" ")
vip_no_count+=1
no+=1
Output : vip_no_count : 2 3 5 7 11

Top comments (0)