在 Python 中查找数字的倍数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14553349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
finding multiples of a number in Python
提问by iKyriaki
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.
我发现,通过 put 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. I have a feeling I should be using the range()function somewhere in this, but I have no idea where.
经过多次搜索,我只能在java中找到示例代码,因此我尝试将其翻译成python,但没有得到任何结果。我有一种感觉,我应该range()在此的某个地方使用该功能,但我不知道在哪里。
采纳答案by Amber
If you're trying to find the first countmultiples of m, something like this would work:
如果您试图找到 的第一个count倍数m,则可以使用以下方法:
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:
请注意,这两者的倍数都从 at 0- 如果您想改为从 开始m,则需要将其抵消那么多:
range(m,(count+1)*m,m)
回答by sberry
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]
回答by Colonel Panic
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]
回答by Vikash Raja Samuel Selvin
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) ]
回答by ivanleoncz
Based on mathematical concepts, I understand that:
基于数学概念,我理解:
- all natural numbers that, divided by
n, having0as remainder, are all multiples ofn
- 所有自然数,除以
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:
进一步阅读:
回答by deeksha
You can do:
你可以做:
def mul_table(n,i=1):
print(n*i)
if i !=10:
mul_table(n,i+1)
mul_table(7)

