First time here? Check out the FAQ!

Ask Your Question
1

How to sum the log of the first n primes?

asked 1 year ago

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.

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
1

answered 1 year ago

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))
Preview: (hide)
link

Comments

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

toni gravatar imagetoni ( 1 year ago )

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

toni gravatar imagetoni ( 1 year ago )

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 ( 1 year ago )

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

Stats

Asked: 1 year ago

Seen: 715 times

Last updated: May 22 '23