Jump to content

Sum of primes ratio

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
NaCl

NaCl

    Newbie

  • Members
  • Pip
  • 3 posts
Ok, I am trying to make a program in python that is supposed to show a relation of the sum of prime numbers.

The guide I am reading says to try to make: "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."

It seems like they are saying to make it do: log(sum) / log(n)
However, that does not give a limit of 1...

Here is the code I have, with the part highlighted

from math import *

nPrime = int(input("n = "))
if nPrime <= 0:
    print "dsfsdfsds"
else:
    finalPrime = 1
    verifiedPrimeCount = 0
    mightBePrime = finalPrime + 1
    stackPrimes = 0
    while verifiedPrimeCount < nPrime:
        primeCheck = True
        for divideBy in xrange(2, mightBePrime, 1):
            if primeCheck:
                primeCheck = ((mightBePrime % divideBy) != 0)
        if primeCheck:
            finalPrime = mightBePrime
            stackPrimes = stackPrimes + finalPrime
            verifiedPrimeCount = verifiedPrimeCount + 1
            [b]print stackPrimes, "||", (log(stackPrimes)) / (log(nPrime))[/b]
        mightBePrime = mightBePrime + 1

For example, I get this with N=10:

Quote

2 || 0.301029995664
5 || 0.698970004336
10 || 1.0
17 || 1.23044892138
28 || 1.44715803134
41 || 1.61278385672
58 || 1.76342799356
77 || 1.88649072517
100 || 2.0
129 || 2.1105897103
This should have kept getting infinitely closer to 1, but it went up to 2+ instead...

I just started learning python 2 days ago... sorry if this is a stupid question.

Thanks.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I believe it wants sum(logs)/n
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog