簡體   English   中英

如何找到對數值的總和

[英]how to find sum of logarithm values

我剛開始學習 Python 3,我有以下問題需要解決:

“編寫一個程序,計算從 1 到某個數 n 的所有素數的對數之和,並打印出這些素數的對數之和。

一種。 輸入:整數 n

輸出:log(1),log(2),log(3),...,log(n)的總和(以10為基數)"

math模塊中有log10函數,所以不用自己搞清楚怎么計算log。 所以你會做這樣的事情:

import math

def is_prime(x):
    # Write a function that returns whether x is prime

def log_sum(n):
    # Return the sum of all logs for primes smaller than n
    log_total = 0
    for i in range(1, n+1):
        if is_prime(i):
            log_total += math.log10(i)
    return log_total

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM