[英]Accept a positive integer n as input and find the print the smallest integer that is divisible by all the integers in the range [1, n]
[英]Write a function that input a positive integer n and return the number of n-digit positive integers that divisible by 17
我正在研究鼠尾草。 編寫一個輸入正整數n並返回可被17整除的n位正整數的數目的函數。請確保考慮n = 1的情況。 使用輸入n = 1,2,5測試您的程序。
據我了解,例如,如果我輸入n = 1,則意味着我需要檢查0-9中所有可被17整除的數字。如果我輸入n = 2,則意味着我需要檢查所有0到99之間的數字,可被17整除。
我不會想出一個通用的公式來計算n的長度,然后取可被17整除的數字的正確范圍。
def positive(n):
for n in range(0, 10**n):
if (n%17==0):
print n,
上面的代碼可以和我一起工作,但是它只是打印出可以被17整除的數字。我想知道如何計算它們,所以我知道可以被17整除的數字。
如何使用數論簡化問題並使用
def positive(n):
return 10**n // 17 + 1
我相信Sage使用插入符號而不是雙星號進行冪運算,因此您可以改用
10^n // 17 + 1
加號包含值0,該值當然可以被17整除。您可以使用更長的版本進行檢查,
def positive(n):
return len([x for x in range(10**n) if x % 17 == 0])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.