Python Program to Find Numbers Divisible by 7 and Multiple of 5 in a Given Range
Given a range of numbers, the task is to write a Python program to find numbers divisible by 7 and multiple of 5.
Example:
Input:Enter minimum 100
Enter maximum 200
Output:
105 is divisible by 7 and 5.
140 is divisible by 7 and 5.
175 is divisible by 7 and 5.
Input:Input:Enter minimum 29
Enter maximum 36
Output:
35 is divisible by 7 and 5.
Algorithm -1
- Input the minimum and maximum range from the user.
- Compute the minimum and maximum multiples of 35 (7*5) that are greater than or equal to the minimum and less than or equal to the maximum, respectively. This is done by dividing the minimum and maximum by 35, rounding up or down using the ceil and floor functions in the math library, and then multiplying by 35 to get the actual multiples.
- Generate the desired numbers by creating a range object that starts at the minimum multiple, ends at the maximum multiple, and has a step size of 35 (since the desired numbers are multiples of 35).
- If the range object is not empty, print the numbers in the range with a newline separator. Otherwise, print a message indicating that no numbers were found.
Python3
import math
minimum = 100
maximum = 200
min_multiple = math.ceil(minimum / 35 ) * 35
max_multiple = math.floor(maximum / 35 ) * 35
result = range (min_multiple, max_multiple + 1 , 35 )
if result:
print ( * result, sep = '\n' )
else :
print ( "No numbers found." )
|
Time complexity: O(1) – the time complexity is constant
Auxiliary space: O(1) – only constant space is used.
A set of integers can be checked for divisibility by 7 and 5 by performing the modulo operation of the number with 7 and 5 respectively, and then checking the remainder. This can be done in the following ways :
Python3
start_num = int ( 29 )
end_num = int ( 36 )
cnt = start_num
while cnt < = end_num:
if cnt % 7 = = 0 and cnt % 5 = = 0 :
print (cnt, " is divisible by 7 and 5." )
cnt + = 1
|
Output
35 is divisible by 7 and 5.
This can also be done by checking if the number is divisible by 35, since the LCM of 7 and 5 is 35 and any number divisible by 35 is divisible by 7 and 5 and vice versa also.
Python3
start_num = int ( 68 )
end_num = int ( 167 )
cnt = start_num
while cnt < = end_num:
if (cnt % 35 = = 0 ):
print (cnt, "is divisible by 7 and 5." )
cnt + = 1
|
Output
70 is divisible by 7 and 5.
105 is divisible by 7 and 5.
140 is divisible by 7 and 5.
The time complexity is O(n), where n is the number of integers in the given range from start_num to end_num.
The auxiliary space is O(1), as the amount of memory used by the code remains constant regardless of the size of the input.
Method: find numbers divisible_by 7 and multiple of 5 in_range
- Take input for the minimum and maximum values of the range from the user using the input() function and store them in variables min_val and max_val, respectively.
- Use a for loop to iterate over each number within the range min_val to max_val (inclusive) using the range() function.
- For each number in the loop, check if it is divisible by 7 and a multiple of 5 by using the modulo operator %.
- If the number satisfies both conditions, print it along with a message saying that it is divisible by 7 and 5.
- The program ends when all numbers in the range have been checked.
Python3
min_val = 100
max_val = 200
for num in range (min_val, max_val + 1 ):
if num % 7 = = 0 and num % 5 = = 0 :
print (num, "is divisible by 7 and 5." )
|
Output
105 is divisible by 7 and 5.
140 is divisible by 7 and 5.
175 is divisible by 7 and 5.
The time complexity of this program is O(n),
The auxiliary space of this program is O(1).
List Comprehension Approach:
- Take the minimum and maximum values as input from the user.
- Initialize an empty list to store the numbers that are divisible by 7 and multiple of 5.
- Use list comprehension to filter out the numbers that satisfy the condition.
- Print the list of numbers that are divisible by 7 and multiple of 5.
Python3
minimum = 100
maximum = 200
result_list = []
result_list = [num for num in range (
minimum, maximum + 1 ) if num % 7 = = 0 and num % 5 = = 0 ]
if result_list:
print ( "Numbers divisible by 7 and multiple of 5 in the given range are: " )
for num in result_list:
print (num, "is divisible by 7 and 5." )
else :
print ( "There are no numbers divisible by 7 and multiple of 5 in the given range." )
|
Output
Numbers divisible by 7 and multiple of 5 in the given range are:
105 is divisible by 7 and 5.
140 is divisible by 7 and 5.
175 is divisible by 7 and 5.
Time Complexity: O(n), where n is the range of numbers from the minimum to maximum value.
Auxiliary Space: O(k), where k is the number of numbers that are divisible by 7 and multiple of 5 in the given range.
Last Updated :
16 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...