Python 2.5.4: how to find the sum of the values ​​of a logarithm

I am learning Python 2.5.4 and I have the following problem:

"Write a program that calculates the sum of the logarithms of all primes from 2 to a certain number n and displays the sum of the logs of primes, the number n and the ratio of these two quantities. This is for different values ​​of n.

This is what I have so far:

from math import *
n = raw_input('This is a logarithm ratio tester. Which number do you want to test? ')
for x in range(2,n):                            #picks numbers to test
    for divisor in range(2, 1+int(sqrt(x+1))):
        if x%divisor == 0:                      #checks if x is prime
            log(x)                              #computes log of prime

Unfortunately, I'm not sure how to implement a function to summarize all the logs. I would suggest that as soon as I succeed, I just need to complete the program:

print 'Sum of the logs of the primes is',logsum
print 'n =',n
print 'Ratio of sum to n is',logsum/n

Or some option. But what would be a good way to get what I called logsum? Please note that I have been learning programming for no more than a week, I know very few statements / functions by heart, and I'm not a mathematician. If in doubt, assume I'm an idiot. Thanks!

+4
3

, , log (x) logsum :

from math import *
logsum = 0
n = raw_input('This is a logarithm ratio tester. Which number do you want to test? ')
for x in range(2,n):                            #picks numbers to test
    isprime = True
    for divisor in range(2, 1+int(sqrt(x+1))):
        if x%divisor == 0:                      #checks if x is prime
            isprime = False                                  #computes log of prime
            break
    if isprime:
        logsum += log(x)

, , , The Sieve of Eratoshenes .

EDIT: Juri Robl, :

from math import *
logsum = 0
n = raw_input('This is a logarithm ratio tester. Which number do you want to test? ')
for x in range(2,n):                            #picks numbers to test
    for divisor in range(2, 1+int(sqrt(x+1))):
        if x%divisor == 0:                      #checks if x is prime
            break
    else:
        logsum += log(x)
+3

prime:

def isprime(x):
  for j in range(2,1 + int(sqrt(x - 1))):
    if x%j == 0:
      return False
  return True

def primes(n):
    return [j for j in range(2, n) if isprime(n)]

logsum = 0
for p in primes(n):
    logsum += log(p)
+4

, log (2) + log (3) +... + log (n) = log (2 * 3 *... * n)

, , , .

import math
import operator

def filter_primes(alist, newlist):
    for x in alist[1:]:
        if x%alist[0] != 0:
            newlist.append(x)
    return newlist

n = int(raw_input('This is a logarithm ratio tester. Which number do you want to test?'))
alist = range(2, n)
sieve_list = []
primes = [2]

while alist[0] < math.sqrt(n):
    a = filter_primes(alist, sieve_list)
    primes.append(a[0])
    alist = sieve_list
    sieve_list = []
for j in alist[1:]: primes.append(j)

product = reduce(lambda x, y: x*y, primes)
ans = math.log(product)

print 'The sum of the logs is: %d \nn is: %d \nThe ratio of these two quantities is: %s' %   (ans, n, float(ans/n))
+1

Source: https://habr.com/ru/post/1527232/


All Articles