16

I'm trying to write a code that lets me find the first few multiples of a number. This is one of my attempts:

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

I figured out that, by putting for m in (n, m):, it would run through the loop for whatever number was 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 multiple searches, I was only able to find a sample code in java, so I tried to translate that into python, but I didn't get any results. I have a feeling I should be using the range() function somewhere in this, but I have no idea where.

2
  • What are n and m here?
    – Amber
    Commented Jan 27, 2013 at 23:03
  • Kindly show an example of usage and expected results.
    – sotapme
    Commented Jan 27, 2013 at 23:08

10 Answers 10

14

If you're trying to find the first count multiples of m, something like this would work:

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

Alternatively, you could do this with range:

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

Note that both of these start the multiples at 0 - if you wanted to instead start at m, you'd need to offset it by that much:

range(m,(count+1)*m,m)
1
  • 1
    After messing around with this code, I was able to get it to work exactly how I needed it. Thank you very much for this!
    – iKyriaki
    Commented Jan 27, 2013 at 23:22
4

Does this do what you want?

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

For m=5, n=20

[20, 40, 60, 80, 100]

Or better yet,

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

For Python3+

>>> print(list(range(n, (m+1)*n, n)))
[20, 40, 60, 80, 100] 
4
  • I tried this out but this was the result: >>> printMultiples(10,2) range(10, 30, 10) >>> printMultiples(10, 10) range(10, 110, 10)
    – iKyriaki
    Commented Jan 27, 2013 at 23:19
  • @iKyriaki It's probably cause he's using Python 2 and you're using Python 3. You can fix it by doing print([i for i in range(n, (m+1)*n, n)]) instead. I believe it's cause range() returns a listin Python 2. In Python 3, range() uses yield so it's an iterator. Correct me if I'm wrong
    – user1632861
    Commented Jan 28, 2013 at 11:32
  • @Mahi, in py3, the range return a generator. You can exhaust the generator by wrapping it with list()
    – sberry
    Commented Jan 28, 2013 at 11:44
  • @sberry Yes, of course a generator since it uses yield, silly me. Good one with the list(), haven't ever really thought of that.
    – user1632861
    Commented Jan 28, 2013 at 11:52
4

Based on mathematical concepts, I understand that:

  • all natural numbers that, divided by n, having 0 as remainder, are all multiples of n

Therefore, the following calculation also applies as a solution (multiples between 1 and 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]

For further reading:

2

For the first ten multiples of 5, say

>>> [5*n for n in range(1,10+1)]
[5, 10, 15, 20, 25, 30, 35, 40, 45, 50]
1
  • 1
    I don't understand what you are saying. Can you please add more descriptions. Commented Apr 7, 2016 at 1:35
1

You can do:

def mul_table(n,i=1):
    print(n*i)
    if i !=10:
        mul_table(n,i+1)
mul_table(7)
1

If this is what you are looking for -

To find all the multiples between a given number and a limit

def find_multiples(integer, limit):
    return list(range(integer,limit+1, integer))

This should return -

Test.assert_equals(find_multiples(5, 25), [5, 10, 15, 20, 25])
1

Another method that can be done is trying to make a list. Here's my example for getting the first 20 multiples of 7.

Input:

multiples_7 = [x * 7 for x in range(1,21)] 

print(multiples_7)

Output:

[7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84, 91, 98, 105, 112, 119, 126, 133, 140]
0
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

For first 10 multiples of 5 you can do as

import numpy as np
#np.arange(1,11) array from 1 to 10 
array_multipleof5 = [5*n for n in np.arange(1,11)]
array_multipleof5 = np.array(array_multipleof5)
print(array_multipleof5)
0

How to calculate the first n multiples of a given number x, in the compact python's lambda notation

n_multiples_of_x = lambda n,x : list( range(x, x*n + 1, x) )

Tests:

assert n_multiples_of_x(5, 5) == [5, 10, 15, 20, 25]

Not the answer you're looking for? Browse other questions tagged or ask your own question.