0

Python Program to Print all Numbers in a Range Divisible by a Given Numbers L=int(input()) #L=[2,4,5] take all input at a time to process with range number divisible with give inputs those numbers print output is 20,40 bcuz of 2,4,5 divisible 20, 40 only

L=int(input))
for i in l:
  for j in range(1,50):
    if j%i==0:
      print(j)

I want out put 20,40

Range divisible with all list l

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61

3 Answers3

0

I'm not sure quite sure what you're asking, but I think the gist of it is that you want to print all numbers in a given range that are divisible by a list.

In that case, there is no need to do a type coercion to int on your list, I'm not sure why you did that.

my_list = [2, 4, 5]

# create an empty output list
output = []
# iterate through a range.
for i in range(1, 50):
    # create a boolean array that checks if the remainder is 0 for each 
    # element in my_list
    if all([i % j == 0 for j in my_list]):
        # if all statements in the boolean list created above are true 
        # (number is divisible by all elements in my_list), append to output list
        output.append(i)

print(output)
Dharman
  • 30,962
  • 25
  • 85
  • 135
mihirs
  • 43
  • 3
  • 5
0

First you need to calculate the least common multiple (lcm) of the numbers in the list. Math library in python 3.9 has included a lcm function, so you can use it. If you dont have python 3.9 you can search some solutions.

import math
lcm = math.lcm(2,4,5)

At this point you know that every integer number that you multiply by 20 is going to be divisible by all the numbers in the list (2 * 20=40, 3 * 20=60 ...). So for a range you must divide the maximum range number (in your case 50) and truncate the result.

high = 50
low = 1
n = int(high/lcm)

Make a loop and multiply from 1 to n by the lcm result. Then for each result check if it is equal or higher than the lower range.

for j in range(1,n):
    res = j*lcm
    if res >= low:
        print(res)
Raúl Mac
  • 3
  • 2
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 13 '21 at 04:53
0
values = int(input('enter value count :'))

n = []

for k in range(values):
  temp = int(input('enter int value :'))
  n.append(temp)

for i in range(1,50):

  count = 0

  for j in n:
    if i%j == 0:   
      count=count+1
  else:
    if count==len(n):
      print(i,end=',')
Dmitriy Zub
  • 1,398
  • 8
  • 35