find multiple numbers in python

I am trying to write code that allows me to find the first few multiple numbers. This is one of my attempts:

def printMultiples(n, m):
for m in (n,m):
    print(n, end = ' ')

I realized that, substituting for m in (n, m):it, it will go through a cycle for any number m.

def printMultiples(n, m):
'takes n and m as integers and finds all first m multiples of n'
for m in (n,m):
    if n % 2 == 0:
        while n < 0:
            print(n)

After several searches, I managed to find only sample code in java, so I tried to translate it into python, but I did not get any results. I have a feeling that I should use a function range()somewhere in this, but I have no idea where.

+8
source share
7 answers

If you are trying to find the first countmultiples m, something like this will work:

def multiples(m, count):
    for i in range(count):
        print(i*m)

Alternatively, you can do this with a range:

def multiples(m, count):
    for i in range(0,count*m,m):
        print(i)

, 0 - m, :

range(m,(count+1)*m,m)
+7

, ?

print range(0, (m+1)*n, n)[1:]

m = 5, n = 20

[20, 40, 60, 80, 100]

,

>>> print range(n, (m+1)*n, n)
[20, 40, 60, 80, 100] 

Python3 +

>>> print(list(range(n, (m+1)*n, n)))
[20, 40, 60, 80, 100] 
+4

5,

>>> [5*n for n in range(1,10+1)]
[5, 10, 15, 20, 25, 30, 35, 40, 45, 50]
+1
def multiples(n,m,starting_from=1,increment_by=1):
    """
    # Where n is the number 10 and m is the number 2 from your example. 
    # In case you want to print the multiples starting from some other number other than 1 then you could use the starting_from parameter
    # In case you want to print every 2nd multiple or every 3rd multiple you could change the increment_by 
    """
    print [ n*x for x in range(starting_from,m+1,increment_by) ] 
0

, , :

  • , n 0 , n

, ( 1 100):

>>> multiples_5 = [n for n in range(1, 101) if n % 5 == 0]
>>> multiples_5
[5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100]

:

0

:

def mul_table(n,i=1):
    print(n*i)
    if i !=10:
        mul_table(n,i+1)
mul_table(7)
0
# multiples of number in a given range
def multiples(n, rng):
    multiples_of_n = [x * n for x in range(1, stop) if x * n < rng]
    return multiples_of_n

>>> multiples(3, 20)
{3, 6, 9, 12, 15, 18}
0

All Articles