Reputation: 101
The requirement for my program is:
Complete the following program so that the loop stops when it has found the smallest positive integer greater than 1000 that is divisible by both 33 and 273.
This is my code that I have tried so far:
n = 1001 #This one is required
while True: #This one too
for i in range(n,___): # I don't know what should i put in the blank
if i%33 == 0 and i%273 == 0: # I really confused about this line
break # Should i break it now?, or in the other lines?
print(f"The value of n is {n}") #This one is also required
I have tried to use the break under for-statement but nothing returns. I don't know where I should put a break, in which lines or if I even need it at all.
How should I write the loops to get the correct result?
Upvotes: 5
Views: 388
Reputation: 19
Use the Least Common Multiple (LCM) of 33 and 273:
import math
# Get user input
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
sn = int(input("Enter the starting number (must be greater than 0): "))
# Find the LCM of the two numbers
lcm = math.lcm(num1, num2)
# Find the smallest multiple of LCM greater than sn-1
n = sn if sn % lcm == 0 else ((sn // lcm) + 1) * lcm
print(f"The smallest number greater than {sn-1} that is divisible by {num1} and {num2} is {n}")
The output:
Enter the first number: 33
Enter the second number: 273
Enter the starting number (must be greater than 0): 1001
The smallest number greater than 1000 that is divisible by 33 and 273 is 3003
Upvotes: 0
Reputation: 3817
Well you could still use a for
loop, as long as the upper limit is at least as high as the maximum possible result. The result would be in i
, not in n, and the for
loop will suffice, not an additional while
loop. The for
loop will break when the remainder when dividing by both 33 and 237 is zero (i.e. they are both factors).
n = 1001
for i in range(n, 33 * 237 + 1):
if i % 33 == 0 and i % 237 == 0:
break
print(f"The value of i is {i}")
You could also use a while loop and use the same logic for the condition. In this case we test that at least one is not a factor and continue the loop until both 33 and 237 are evenly divisible into i.
n = 1001
i = n
while i % 33 or i % 237:
i += 1
print(f"The value of i is {i}")
Upvotes: 1
Reputation: 3799
There are few things to explain:
n%33 is the remainder of dividing n by 33. So 66%33 is 0 and 67%33 is 1.
For loops are generally when you need to loop over a defined range (not always, but usually). E.g. "add up the first 100 integers". A while loop makes more sense here. It will definitely terminate, because at some point you'll get to 33 * 237.
if i%33 == 0 and i%237 == 0: means we want to do something when the number can be divided evenly (no remainder) by both 37 and 237.
n=1001 while True: if n%33==0 and n%237==0: print(n) break n+=1
Upvotes: 1
Reputation: 22784
You already have a while True:
loop, you don't need the inner for
loop to search for your number, just keep incrementing n
in the while
loop instead of adding a new counter, when the number you're looking for is found, the infinite while True:
loop will stop (using break
), and so your print statement will be executed:
n = 1001 # start at 1001
while True: # start infinite loop
if n % 33 == 0 and n % 273 == 0: # if `n` found
break # exit the loop
n += 1 # else, increment `n` and repeat
print(f"The value of n is {n}") # done, print the result
Output:
The value of n is 3003
Upvotes: 4
Reputation: 6438
A for loop will not help you here, because you don't know when to end the loop. You usually use for loops when the range of things you want to loop over is already known.
Instead, do the following:
before starting your while: True
loop: set i
to 0,
then increase i
with 1 every time to the loop
also, don't forget to stop the loop when i>1000
!
Upvotes: 1