简体   繁体   English

在 Python 中查找数字的倍数

[英]finding multiples of a number in Python

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 .我发现,通过将for m in (n, m):中,无论数字是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.经过多次搜索,我只能找到java中的示例代码,因此我尝试将其翻译成python,但没有得到任何结果。 I have a feeling I should be using the range() function somewhere in this, but I have no idea where.我觉得我应该在这个地方使用range()函数,但我不知道在哪里。

If you're trying to find the first count multiples of m , something like this would work:如果你试图找到m的第一个count倍数,这样的事情会起作用:

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:请注意,这两个都从0开始倍数 - 如果您想改为从m开始,则需要将其偏移这么多:

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

Does this do what you want?这是做你想做的吗?

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

For m=5, n=20对于 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+对于 Python3+

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

Based on mathematical concepts, I understand that:基于数学概念,我理解:

  • all natural numbers that, divided by n , having 0 as remainder, are all multiples of n所有除以n余数为0的自然数都是n的倍数

Therefore, the following calculation also applies as a solution (multiples between 1 and 100):因此,以下计算也适用于解决方案(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]

For further reading:进一步阅读:

For the first ten multiples of 5, say对于 5 的前十个倍数,比如说

>>> [5*n for n in range(1,10+1)]
[5, 10, 15, 20, 25, 30, 35, 40, 45, 50]

You can do:你可以做:

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

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])

Another method that can be done is trying to make a list.可以做的另一种方法是尝试制作一个列表。 Here's my example for getting the first 20 multiples of 7.这是我获取 7 的前 20 个倍数的示例。

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]
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) ] 

For first 10 multiples of 5 you can do as对于 5 的前 10 个倍数,您可以这样做

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)

How to calculate the first n multiples of a given number x, in the compact python's lambda notation如何在紧凑的 python 的 lambda 表示法中计算给定数字 x 的前 n 倍数

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM