The Student Room Group

Logarithms of all the primes.

This problem set is driving me mad. I know how logarithms work but I don't understand how to solve this problem set. Anyone got a idea? Note: I have serious dyslexia.

Problem 2.
Write a program that computes the sum of the logarithms of all the primes from 2 to some
number n, and print out the sum of the logs of the primes, the number n, and the ratio of these
two quantities. Test this for different values of n.
You should be able to make only some small changes to your solution to Problem 1 to solve this
problem as well.
Hints:
While you should see the ratio of the sum of the logs of the primes to the value n slowly get
closer to 1, it does not approach this limit monotonically.
(edited 9 years ago)
can you print problem 1 ? it seems to be related to problem 2
Reply 2
Original post by illegaltobepoor
This problem set is driving me mad. I know how logarithms work but I don't understand how to solve this problem set. Anyone got a idea?

Problem 2.
Write a program that computes the sum of the logarithms of all the primes from 2 to some
number n, and print out the sum of the logs of the primes, the number n, and the ratio of these
two quantities. Test this for different values of n.
You should be able to make only some small changes to your solution to Problem 1 to solve this
problem as well.
Hints:
While you should see the ratio of the sum of the logs of the primes to the value n slowly get
closer to 1, it does not approach this limit monotonically.


If you have certain functions available this is a very simple exercise. If you have to write any of those functions it becomes a more significant task.

We need to know more about what you can use.
Original post by BabyMaths
If you have certain functions available this is a very simple exercise. If you have to write any of those functions it becomes a more significant task. Although not *much* more significant.

But if I had to bet, "Problem 1" involved finding the primes between 1 and n, in which case nearly all the work should have been done there. We really need to know what Problem 1 actually was...
Original post by BabyMaths
If you have certain functions available this is a very simple exercise. If you have to write any of those functions it becomes a more significant task.

We need to know more about what you can use.


Sure. Its from MIT courseware.
http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00-introduction-to-computer-science-and-programming-fall-2008/assignments/pset1a.pdf

The Product of the Primes
There is a cute result from number theory that states that for sufficiently large n the product of
the primes less than n is less than or equal to e**n and that as n grows, this becomes a tight
bound (that is, the ratio of the product of the primes to e**n gets close to 1 as n grows).
Computing a product of a large number of prime numbers can result in a very large number,
which can potentially cause problems with our computation. (We will be talking about how
computers deal with numbers a bit later in the term.) So we can convert the product of a set of
primes into a sum of the logarithms of the primes by applying logarithms to both parts of this
conjecture. In this case, the conjecture above reduces to the claim that the sum of the
logarithms of all the primes less than n is less than n, and that as n grows, the ratio of this sum
to n gets close to 1.
To compute a logarithm, we can use a built in mathematical functions from Python. To have
access to these functions, you need to get them into your environment, which you can do by
including the
from math import *
statement at the beginning of your file. This will allow you to use the function log in your code,
e.g. log(2) will return the logarithm base e of the number 2.

...............................................................................

My code is written in Python 2.5

...............................................................................

from math import *


oddlist = []
primelist = []


def odd():
for item in range(2,7920):
if item % 2 == 1:
oddlist.append(item)
return "numbers generated"


def primeconvey(x):
for i in x:
is_prime(i)


def is_prime(x):
for i in range(2,(x-1)):
if x % i == 0:
return False

primelist.append(x)


#-----------------------------
odd()
primeconvey(oddlist)
print primelist
print len(primelist)
Reply 5
Original post by DFranklin
Although not *much* more significant.

But if I had to bet, "Problem 1" involved finding the primes between 1 and n, in which case nearly all the work should have been done there. We really need to know what Problem 1 actually was...


I was wondering whether OP had to write a log function too. :tongue:
Do I need to do LOG A B = C for all the primes?

https://docs.python.org/2/library/math.html

In Python that is Log(C) / Log(A) = B
Original post by BabyMaths
I was wondering whether OP had to write a log function too. :tongue:


Your make me cry if I had to do that.
I suspect you just need to write a function to sum the logarithms.


I don't know python, but from what you've posted it would be something like:

def sumLogs(x)
S = 0
for i in x:
S+=log(x)
return S

Then you need to print out sumLogs(x) and sumLogs(x)/N (where N = 7920 for the code you've posted) as well as len(primelist)

Quick Reply

Latest