0

I just started learning Python 3 and I have the following problem to solve:

"Write a program that computes the sum of the logarithms of all the primes from 1 to some number n, and print out the sum of the logs of the primes.

a. input: integer n

b. output: the sum of log(1),log(2),log(3),...,log(n) (the base of log is 10)"

Angel
  • 9
  • 1
  • 3
    Questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it. Please read [How to ask homework questions](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions) and [edit] your post. – Nick Mar 07 '20 at 07:44

1 Answers1

-1

There is a log10 function in the math module, so you don't need to figure out how to calculate the log yourself. So you would do something like:

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
chuck
  • 1,420
  • 4
  • 19