【问题标题】:how to find sum of logarithm values如何找到对数值的总和
【发布时间】:2020-03-07 07:18:43
【问题描述】:

我刚开始学习 Python 3,我有以下问题需要解决:

"编写一个程序,计算从 1 到某个数 n 的所有素数的对数之和,并打印出素数的对数之和。

一个。输入:整数n

b.输出:log(1),log(2),log(3),...,log(n)之和(log的底为10)"

【问题讨论】:

  • 请求家庭作业帮助的问题必须包括您迄今为止为解决问题所做的工作的总结,以及您在解决问题时遇到的困难的描述。请阅读How to ask homework questionsedit 你的帖子。

标签: python logarithm natural-logarithm


【解决方案1】:

math 模块中有一个log10 函数,所以你不需要自己弄清楚如何计算日志。所以你会做这样的事情:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-09
    • 2020-06-04
    • 1970-01-01
    • 2014-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多