[英]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.