finding multiples of a number in Python

后端 未结 7 1194
甜味超标
甜味超标 2021-02-07 10:48

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):
    prin         


        
7条回答
  •  佛祖请我去吃肉
    2021-02-07 11:26

    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:

    • https://www.mathsisfun.com/definitions/natural-number.html
    • https://www.mathwizz.com/arithmetic/help/help9.htm
    • https://www.calculatorsoup.com/calculators/math/multiples.php

提交回复
热议问题