# How to sum the log of the first n primes?

This is a very naive question, and it may not fit the etiquette of the forum.

How do you handle the fact that

log(7) prints out as log(7), and only after expressing it as log(7.0) do you get 1.94591014905531

I would try and generate the sum using the function primes_first_n(n). However, log(primes_first_n(n)) is not a valid expression, and I don't know if it is because of the point about the logarithmic function above, or it is because log cannot be applied to a vectorial expression.

edit retag close merge delete

Sort by ยป oldest newest most voted

For the exact value you can use:

sum(log(p) for p in primes_first_n(100))


or for an approximate numerical value:

sum(log(p).n() for p in primes_first_n(100))

more

What is the most expedient way to get he actual decimal expansion of something like log(7)?

( 2023-05-22 18:02:44 +0200 )edit

How could I include that in a function like y(x) = sum(log(p) for p in primes_first_n(x))

( 2023-05-22 18:26:58 +0200 )edit

You can specify desired precision (in bits) as an argument to .n() method - like log(7).n(100). As for the function: y = lambda x: sum(log(p) for p in primes_first_n(x)) will do the job.

( 2023-05-22 20:25:47 +0200 )edit