Ask Your Question

How to sum the log of the first n primes?

asked 2023-05-22 14:10:52 +0200

toni gravatar image

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 flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2023-05-22 16:17:26 +0200

Max Alekseyev gravatar image

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))
edit flag offensive delete link more


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

toni gravatar imagetoni ( 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))

toni gravatar imagetoni ( 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.

Max Alekseyev gravatar imageMax Alekseyev ( 2023-05-22 20:25:47 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2023-05-22 14:10:52 +0200

Seen: 293 times

Last updated: May 22 '23