LOCKERS - Daily Coding Problems
1. LOCKERS!
Your college has N lockers. One day you decide to play a game and open only those
lockers that are multiple of M. Initially,
all lockers are closed. Lockers are numbered from 1 to N. Find the number of
lockers that remain closed.
Input Format:
First Line of input contains testcase T.
For each testcase, there will be two lines of input.
·
First line contains the number of lockers N
·
Second line contains M. You will open lockers that are
multiples of M.
Output:
For each testcase print the
number of lockers remain closed.
Sample Input: 2
10
2
12
3
Sample output: 5
8
Explanation: For the test case 1:
N = 10, M = 2
You will open the lockers
which are multiple of 2 that is [2,4,6,8,10]. So the lockers that remain closed
are [1,3,5,7,9] which is equal to 5.
SOLUTION:
We will
loop in a range from 1 to N+1 and check the condition if i is divisible by m
then skip else print the number.
That’s it.
Here goes
the short sweet code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
t = int(input("Enter the number of the test cases: ")) | |
for i in range(t): #codingsnap.tech | |
n = int(input("Enter the number of lockers: ")) | |
m = int(input("Enter some random numbers to open the lockers: ")) | |
for i in range(1,n+1): | |
if (i%m==0): | |
continue | |
else: | |
print(i) |
0 Comments