elz: (ada-tubes)
[personal profile] elz posting in [community profile] intro_to_cs
First:

Open to: Registered Users, detailed results viewable to: All, participants: 25


Tickyboxes!

View Answers

Finished lecture 2
24 (96.0%)

Finished problem set 1
15 (60.0%)



Also, it occurs to me that it might be handy to have a place to post/discuss solutions to the problem sets, to see what we can learn from each other in the absence of TAs. If you actually go to MIT and the problem sets are still the same, don't read these. ;)

Fire away!

Date: 2009-11-12 06:32 am (UTC)
gchick: Small furry animal wearing a tin-foil hat (Default)
From: [personal profile] gchick
Out of curiosity, what was your reason for using a list? Given that the requirement was to return a final value, why go to the trouble of storing and retrieving the intermediate ones?

Date: 2009-11-12 01:40 pm (UTC)
gchick: Small furry animal wearing a tin-foil hat (Default)
From: [personal profile] gchick
nods whereas I looked at the divisibility-by-primes issue and decided to sacrifice the efficiency of *only* testing on primes in favor of the efficiency of not having to repeatedly call on the list comprehension functions (with the assumption that most composite numbers would be divisible by something *kinda* low and so wouldn't have to go through a ton of test iterations anyway). I'm now really curious to test further...

Of course, that's for choosing the 1000th prime; if I were trying to find the umpty-billionth, I'd probably try to find a way that wasn't brute-forcing it at all, but I'd also be making a lot more money in something crypto-related!

LMsxQgfQKZIYxQHrT

Date: 2012-05-03 01:18 pm (UTC)
From: (Anonymous)
As much as I like the EF 35mm f/2, I would have taken a cheaper EF-S f/1.8 if there was one like in Nikonland.As oehtrs have said, a refresh of the f/2 wouldn't hurt. The first time I heard the AFD going on that I thought the world was ending.I also wonder if there is room to move to a three tier prime at 35mm like at 50mm? That is a dirt cheap one (EF-S f/1.8), a regular one (EF f/2 refresh), and an expensive one (f/1.4).

Date: 2009-11-12 04:36 am (UTC)
gchick: Small furry animal wearing a tin-foil hat (Default)
From: [personal profile] gchick
Interesting how completely different mine is from elz's, even for a program this small:

#!/usr/bin/env python
# ps1a.py 
# MIT problem set 1a, by me

# start with some helpful variabubbles
# note that we're starting with the first prime preloaded; this is admittedly kludgetastic

counter = 0
number = 2

while counter < 1000:

	for i in range(2,number): 
		if number % i == 0:      
			break 
				
        # if there were no factors, print the prime and count it
	else:
		counter +=1
		print counter, ": ", number, 'is prime'
			
	number += 1



And for problem 2:

#!/usr/bin/env python
# ps1b.py 
# MIT problem set 1b, by me

from math import *

max = input("enter a number: ")
sum = 0
number = 2

# we're going to work through a whole range of numbers...
while number <= max:

	# test each to see if it's prime
	for j in range(2,number):
		if number % j == 0:      
			break
	else:
		print number, "is prime: "
		sum += log(number)
	number +=1
	
# spit stuff out	
	
print "sum of logs: ", sum
print "n = ", max
print "ratio: ", sum/max		
	
Edited Date: 2009-11-12 04:38 am (UTC)

Date: 2009-11-12 05:48 am (UTC)
ungemmed: (Default)
From: [personal profile] ungemmed
Here are mine! They're a lot like [personal profile] elz's, but I'm only checking factors up to the square root. (Since all factors above the square root are "balanced" by factors below it, if I haven't encountered any factors by the time I get to the square root I'm not going to encounter any.)

Problem 1

import math

primes = [2]
x = 3
while len(primes) < 1000:
    divisible = False
    root = math.sqrt(x)
    for y in primes:
        if x % y == 0:
            divisible = True
            break
        if y > root:
            break
    if not divisible:
        print x
        primes.append(x)
    x += 2

Date: 2009-11-12 05:52 am (UTC)
ungemmed: (Default)
From: [personal profile] ungemmed
argh, Dreamwidth keeps on choking on my solution for problem 2! I guess it's reading the code as some malicious hacking attempt or something.

Date: 2009-11-12 06:18 am (UTC)
badgerbag: (Default)
From: [personal profile] badgerbag
try wrapping it in
  or  tags maybe...

Date: 2009-11-12 06:24 am (UTC)
ungemmed: (Default)
From: [personal profile] ungemmed
Thanks! It actually turned out, though, that NoScript was being overzealous and thought Dreamwidth was trying to hack me. o.O

Date: 2009-11-12 06:25 am (UTC)
ungemmed: (Default)
From: [personal profile] ungemmed
Problem 2
import math

n = int(raw_input('n? '))

primes = [2]
x = 3
logs = 0
while x <= n:
    divisible = False
    root = math.sqrt(x)
    for y in primes:
        if x % y == 0:
            divisible = True
            break
        if y > root:
            break
    if not divisible:
        logs += math.log(x)
        primes.append(x)
    x += 2
print logs
print logs / n

WzOvEpgUHNwG

Date: 2012-05-03 02:40 pm (UTC)
From: (Anonymous)
Well, I actually know two pforos: one leads to a very quick solution but requires the knowledge of some number theory. The other proof works on first principles but leads to a much longer computation. I was assuming most readers would find the second one.

Date: 2009-11-12 06:17 am (UTC)
badgerbag: (Default)
From: [personal profile] badgerbag
spoiler: OMG, I hate the variable name "itersLeft". If you're going to think that way at least write it out as iterations_left !!!

Date: 2009-11-12 06:24 am (UTC)
badgerbag: (Default)
From: [personal profile] badgerbag
will do the problem set tomorrow! too tired!

Date: 2009-11-12 06:47 am (UTC)
exor674: Computer Science is my girlfriend (Default)
From: [personal profile] exor674
from math import *

def primes_upto_n(n):
    primes = [2]
    choice = 1
    while len(primes) < n:
        choice += 2
        rt = sqrt(choice)
        isPrime = True
        # since we need all primes from 2 to n, this optimization works
        # we can prime-factor all non-prime numbers.
        for i in range(0, len(primes)):
            poss = primes[i]
            if (choice % poss) == 0:
                isPrime = False
                break
            if (poss >= rt):
                break
        if isPrime:
            primes.append(choice)
    return primes

primes = primes_upto_n(1000)
nthPrime = primes[-1]
sums = reduce(lambda sum,prime: sum+log(prime),primes)
print "Sum: ", sums
print "Nth Prime: ", nthPrime
print "sums / Nth Prime: ", sums/nthPrime


Yeah, this includes some weird-crazy optimizations >_<

kUsOwOPPqqovUv

Date: 2012-05-03 09:49 am (UTC)
From: (Anonymous)
Developing such a lens would make a lot of sense, since actually the comnpay that's stealing sales from Canon in this type of lens is SIGMA with their 30mm f/1.4 EX DC HSM (not Nikon, since their 35mm f/1.8 can't mount on a Canon anyways.)Sure, Canon has the affordable EF 28mm f/2.8 and EF 35mm f/2.0, but both of these are older designs and really only cover the extreme ends of the normal prime range equivalents on cropped bodies (28mm on APS-C = 46mm on FF, 35mm APS-C = 56mm FF). Of course, it's because both these lenses were originally meant for use on 35mm film cameras, and thus are not necessarily geared towards the cropped APS-C sensors of the Digital Rebels, XXD and 7D bodies.A proprietary Canon EF-S 30mm (49mm equivalent FF) or EF-S 32mm (51mm equivalent) comes closest therefore to the ideal 50mm classic normal focal range, and with an at least f/1.8 max aperture (preferably f/1.4 to compete with Sigma), and USM to boot for around the $200-$350 range, it would be a great buy. IMO, such a lens would almost certainly have to be EF-S in order to keep the cost down and not cut into sales of the cheaper 28mm and 35mm meant for FF bodies, unless Canon phases out those two lenses altogether and restrict FF users to the more expensive 28mm f/1.8 USM and 35mm f/1.4L USM.

Date: 2009-11-12 08:17 pm (UTC)
yhlee: wax seal (Default)
From: [personal profile] yhlee
I first defined a helper function to check primarily (starts from scratch each time but only goes up to the square root)

# So we can use sqrt and log
from math import *

# helper function: is the given number (positive integer) prime?
def is_prime(number):
	i = 2
	sqrt_n = math.sqrt(number)
	while i <= sqrt_n:
		if number%i == 0:
			return 0
		else:
			i = i + 1
	return 1


Then for Problem 1:
def thousandth_prime():
	# first candidate prime after 2
	number = 3
	
	# we start counting with the 2nd prime, as the first is 2
	counter = 2

	while counter <= 1000:
		last_prime = 2
		if is_prime(number) == 1:
			print "Prime no.", counter, "is", number
			last_prime = number
			counter = counter + 1
		number = number + 2
	print "The thousandth prime is", last_prime

Date: 2009-11-14 09:03 pm (UTC)
luckykitty: Cartoon avatar created on madmen site (Default)
From: [personal profile] luckykitty
Thank you for sharing your solution... My solution to problem 1 was really similar to yours! Except I didn't define it as a function, so had to include a flag for whether or not it was a prime and then test that afterwards. I really like having the function, as it makes it so much easier to read. Cool! *learns*

Could you also return True or False rather than 1 or 0, and then just test if is_prime is true? I guess it's six of one?

Date: 2009-11-14 09:16 pm (UTC)
yhlee: wax seal (Default)
From: [personal profile] yhlee
Yes, I believe you could return True or False (boolean) instead of 1 or 0. I think my brain is stuck in some other programming language I learned earlier where booleans return as 1 or 0 so I reverted. Whoops. :-D

I am a great believer in helper functions--they let me break down the problem into chunks. ^_^

Date: 2009-11-12 08:18 pm (UTC)
yhlee: wax seal (Default)
From: [personal profile] yhlee
For Problem 2:

def prime_product(n):
	# first prime, followed by other prime candidates
	number = 3
	
	# the sum of all primes so far, starting with lonesome 2
	sum = math.log(2)
	
	while number <= n:
		if is_prime(number) == 1:
			sum = sum + math.log(number)
		number = number + 2
	
	print "Sum of log of primes:", sum
	print "n: ", n
	print "Ratio of sum to n:", sum/n


I'm sure this is inefficient, but there it is. :-/

Date: 2009-11-14 08:55 pm (UTC)
luckykitty: Cartoon avatar created on madmen site (Default)
From: [personal profile] luckykitty
My basic program was similar to a lot of the ones here but I totally "duh"ed on the fact that you really don't need to store all the primes, you just need to keep track of what prime you're on. I ended up storing them all in an array, then calling up the 1000th. *headdesk*

It's SO educational to see everyone's solutions--clearly I'm not a very efficient coder. Must learn! *determined*

Problem 1a

Date: 2009-11-15 10:30 pm (UTC)
quartzpebble: (Default)
From: [personal profile] quartzpebble
I started with 3 so that I could only check odd numbers, though using a list like some others did might have been more elegant. I also only checked factors up to the square root.


from math import *

target_primes = 1000 #nth prime that we want to compute
current_no = 3
prime_counter = 2
i = 3

while prime_counter < target_primes:
prime_counter += 1
current_no += 2
i = 3

while i <= sqrt(current_no):
if current_no%i == 0:
current_no += 2
i = 3

else:
i += 2
print str(current_no) + " is the " + str(prime_counter) + "th prime."


(Now with better formatting here.)

Not actually better formatting

Date: 2009-11-15 10:32 pm (UTC)
quartzpebble: (Default)
From: [personal profile] quartzpebble
Is there a way to keep the indents in my code, or do I need to replace tabs with spaces here? It looked fine before I posted. :P

Date: 2009-11-16 02:29 am (UTC)
zulu: (house - hesitant)
From: [personal profile] zulu
#time 12:17 - 2:26
# the 1000th prime is 541

poprime = 3 # start with 3 as the first potential prime to get 2 out of the way
counter = 1 # since 2 is prime start the iteration already at 1


while counter < 1000: # begin by checking how many iterations we're at
      if (poprime/2)*2 == poprime: # if poprime is even
            poprime = poprime + 1 # if poprime is even, add one and restart the iteration
      elif poprime%2 > 0: # else if poprime is odd
            factor = 2 # start factors at 2 because a prime is divisible by 1
            while 1 < factor < 0.5*poprime: # all factors greater than half are mirrors
                  if poprime%factor == 0: # no remainder means it's divisible
                        poprime = poprime + 1 # not prime; increase poprime but not counter
                        factor = poprime # this should make the factor too big to get into this loop again
                  if poprime%factor > 0: # in this loop must check all factors
                        factor = factor + 1 # if one factor doesn't work, check the next
            if factor >= 0.5*poprime: # when you've checked all the factors
                  if poprime%factor == 0: # no remainder means it's divisible
                        poprime = poprime + 1 #not prime; increase poprime not counter
                  if poprime%factor > 0: # You need to know that all your tests failed.
                  # But if you get out of your loops every time a test was true,
                   # then if your loops are good, then having checked all possible factors should be enough.
                  # This number is prime.
                        poprime = poprime + 1 # Increase poprime to check the next potential
                        counter = counter + 1 # You found a prime, so increase the counter.
            poprime = poprime +1

if counter == 1000:
      print poprime


This doesn't work, but it's based only on what we've been taught in the lectures and maybe some of the readings. I don't understand lists yet. I can't read the code in the answers upthread of mine--I was looking for things to reverse-engineer, but no such luck!

What is nice about this program is that by pure chance it'll give you 29 as the 10th prime if you set the counter to print at 10. It makes you feel all happy inside until you try it at different numbers!
Edited Date: 2009-11-16 02:41 am (UTC)

Date: 2009-11-16 04:26 am (UTC)
zulu: Omar Epps, looking awesome (house - epps)
From: [personal profile] zulu
On the other hand, check this out:

#odds

# This script will calculate the nth odd number

odd = 0
counter = 0
odds = []

print ("Let's find the nth odd number. Please enter a postive integer: ")
answer = raw_input()
nth = int(answer)

while counter < nth:
     if odd % 2 == 0:
          print str(odd) + (" is even. It doesn't go on the list.")
          odd = odd + 1
          print ("We have found ") + str(counter) +(" odd numbers. Let's keep going.")

     elif odd % 2 != 0:
          print str(odd) + " is odd. It goes on the list."
          odds.append(odd)
          if len(odds) == int(nth):
               print ("The ") + str(nth) + ("th odd number is ") + str(odd)
               break
          else:
               print ("We have found ") + str(len(odds)) +(" odd numbers. Let's keep going.")
               odd = odd + 1
               counter = counter + 1
Edited Date: 2009-11-16 04:28 am (UTC)

Date: 2009-11-16 06:27 am (UTC)
zulu: (muppets - inspired)
From: [personal profile] zulu
Triumph, dudes! The key was really seeing how it worked on the much simpler math of finding a certain number of odds. This works now! Still probably a million times clunkier than some, but I've never programmed before in my life and using what we've learned so far I got to this point. I've documented it a lot so that y'all can see what I did and why. Also, I apologize to [personal profile] elz's inbox, where all my edited comments full of code probably ended up.

# primes
# AKA sleep is for the weak
# This script will calculate the nth prime number

primes = [2] # This is a list called primes, and its first item is 2
counter = 1 # Because 2 is the first prime and your list already has 1 item.

print ("Let's find the nth prime number. Please enter a postive integer: ")
answer = raw_input()
nth = int(answer) # The user enters the answer N. To do math with the answer, you have to make it an integer.
prime = 3 # Start at 3 so you don't have to deal with the fact that 2 is the first prime.

while counter < nth: # Until you reach N, follow these instructions:

     if nth == 2: # So you don't have to deal with 2 being the first prime
          primes.append(3) # Make sure you keep your list up-to-date otherwise nothing else will work
          print ("The ") + str(len(primes)) + ("nd prime number is 3.") # Concatenating strings & integers is fun!
          break # I don't know if this is necessary, but it gets you out of the loop.

     elif 2 < prime and prime % 2 == 0: # Here's where the real work starts. If the potential prime is even:
          print str(prime) + (" is even. It doesn't go on the list.") # You don't go further.
          print ("We have found ") + str(counter) + (" prime numbers. Let's keep going.")
          prime = prime + 1 # Having dealt with one case, make sure you increase the iteration

     elif 2 < prime and prime % 2 != 0: # If the potential prime is odd:
          print str(prime) + (" is odd. Let's keep working with it.") # There's more to do.

          factor = 2 # Start with a factor of 2 because primes are divisible by 1.
          while factor <= prime*0.5: # Once you get to half the number, the rest of the factors are mirrors.
               if prime % factor == 0: # If it's divisible...
                    print str(prime) + (" has a factor. It is not prime.")
                    prime = prime + 1 # Increase the iteration but not the count of prime numbers
                          # (You haven't found any prime numbers yet.)
                    break
               elif prime % factor > 0: # If it's not divisible...
                    print str(factor) + (" is not a factor. Let's keep going.")
                    factor = factor + 1 # You've tried one factor, now try the next.


          if factor > prime*0.5: # You need to know that all your tests failed.
               # But if you get out of your loops every time a test was true,
               # then if your loops are good,
               # so having checked all possible factors should be enough.
               primes.append(prime) # You have found a prime number! Add it to your list.
               if len(primes) == int(nth) and len(primes) == 3: # If you've reached N, print it out.
                    print ("The ") + str(len(primes)) + ("rd prime number is ") + str(prime) # This is just fancy to get the ordinal correct.
                    break
               if len(primes) == int(nth): # If the number of items on your list is equal to N, then stop.
                    print ("The ") + str(nth) + ("th prime number is ") + str(prime)
                    break
               else: # If you haven't reached N, keep going.
                    print ("We have found ") + str(len(primes)) +(" prime numbers. Let's keep going.")
                    prime = prime + 1 # Increase the potential prime by one.
                    counter = counter + 1 # Since you've found a prime, increase the count by one.

if nth == 1:
     print ("The 1st prime number is 2.") # This helps deal with users who enter 1 as N. (Very funny, users. Not.)
if nth == 1000: # To shorten the program run time, comment out all that printing and just leave this one.
     print ("The ") + str(nth) + ("th prime number is ") + str(prime)
Edited Date: 2009-11-16 06:32 am (UTC)

Date: 2009-11-16 09:10 am (UTC)
tassosss: (Default)
From: [personal profile] tassosss
This is my solution to problem 1. It's not very elegant or complicated and I didn't do more than screen out even numbers for the factors.


#Initialization of terms
Number = 3 #lowest odd prime
primeCounter = 2 #inclusive of 2 and 3
Test = 3 #lowest possible factor for odd numbers

while(primeCounter < 1000):
while(Test < Number):
if Number%Test > 0: #checks to see if number is divisible by anything
Test = Test + 2 #if it is not then tries the next odd number
else: Test = Number + 1 #if it is divisible then it halts the loop
if Test == Number: #test can only reach the number if it is non divisible
primeCounter = primeCounter+1 #so the prime counter goes up
Number = Number + 2 #number goes to next odd number
Test = 3 #test is reset to lowest potential odd factor
print 'The', primeCounter,'th prime is' #final result is printed
Number = Number - 2
print Number

eta - I have no idea how to get the indents to show up.
Problem 2 tomorrow.
Edited Date: 2009-11-16 09:15 am (UTC)

Date: 2009-11-17 01:49 am (UTC)
zulu: Karen Gillam from Dr. Who, wearing a saucy top hat (Default)
From: [personal profile] zulu
Still working on problem 2, but I'm stuck because I can't sum logs. When I try, I get "Error: float required". I've tried assigning the answer as a float, but that doesn't work.

Are there any readings that go along with this week that might help?

Here's my test case:

from math import *
number = log(2)
print number # this works
primes = (2, 3, 5, 7, 11, 13) # this is just as a test case
number = 6
sum = sum(log(primes)) # this is where I get an error message
print sum
ratio = sum/number
print ratio

Date: 2009-11-19 05:35 am (UTC)
bellerina: (Default)
From: [personal profile] bellerina
I think that you can't take the log of an array-- it's not distributive. I tried the same thing and got the error, so I ended up taking the log of each prime before I put them into the array. Not the most elegant solution, but it works...

Date: 2009-11-19 03:33 pm (UTC)
zulu: Omar Epps, looking awesome (house - epps)
From: [personal profile] zulu
Ahh, thanks. I'll try that. Still stuck way back here and I think we have another lecture and problem set coming up...eep!

Date: 2009-11-19 05:47 am (UTC)
bellerina: (Default)
From: [personal profile] bellerina
Hmm... it would be really helpful if we actually had "TAs" in this community, i.e., someone to look through our code and tell us whether we chose an efficient way to do things or not (and why), particularly for those of us who don't have a programming background. I wonder if we can lure some experts in to help out?

My solutions also seem a little clunky, but I guess they work...

My solution for problem 1:

count = 2
numb = 3
n = int(raw_input("What prime are you trying to find?"))
while(count<=n):
    i = 2
    isprime = 1
    half = numb/2
    while(isprime > 0 and i < half):
        if numb%i != 0:
            i = i+1
        else:
            isprime = isprime-1
    if isprime == 1:
        if count < n:
            count = count + 1
            numb = numb + 2
        else: break
    else:
        if count <= n:
            numb = numb + 2
print numb,"is prime number",count


My solution for problem 2:

from math import *
n = int(raw_input("What number?"))
logprimes = [log(2)]
numb = 3
while(numb<=n):
    i = 2
    isprime = 1
    root = sqrt(numb)
    while(isprime>0 and i<=root):
        if numb%i != 0:
            i = i+1
        else:
            isprime = isprime-1
    if isprime == 1:
        logprimes.append(log(numb))
        if numb < n:
            numb = numb + 2
        else: break
    else:
        if numb <= n:
            numb = numb + 2

print "The sum of the logs is",sum(logprimes)
print "The number is",n
print "The ratio of the sum and n is",sum(logprimes)/n

Date: 2009-11-19 07:12 pm (UTC)
erda: (Default)
From: [personal profile] erda
I'm learning a lot by looking at everyone's solutions after working out my own much less sophisticated ones.

Here are mine. I stuck to while and if statements, since those are about the the only things I understand so far.

Problem 1:

count=2 #keeps track of which prime number we are at, assume 2 is the first prime
candidate=3 #the number we are checking to see if it is prime
test_divisor=3 #the number we are dividing by
while count<1001: #program will continue until we reach the 1000th prime
while candidate>=(test_divisor*test_divisor): #we can stop dividing when the divisor gets bigger than the square root of our candidate
remainder=candidate%test_divisor
if remainder==0: #if there is no remainder, the number is not prime so we will go to the next candidate
candidate+=2 #only odd numbers need to be checked
test_divisor=3 #must reset this for next candidate
else:test_divisor+=2 #if there is a remainder we will try the next divisor, no need to use even divisors
count+=1
test_divisor=3
candidate+=2
print "The 1000th prime number is", candidate-2

Problem 2:

from math import *
n=input("What number do you want to stop at?")
candidate=3 #the number we are checking to see if it is prime
test_divisor=3 #the number we are dividing by
OldLog=log(2)
while candidate<=n: #program will continue until we reach the input number
while candidate>=(test_divisor*test_divisor): #we can stop dividing when the divisor gets bigger than the square root of our candidate
remainder=candidate%test_divisor
if remainder==0: #if there is no remainder, the number is not prime so we will go to the next candidate
candidate+=2
test_divisor=3 #must reset this for next candidate
else:test_divisor+=2 #if there is a remainder we will try the next divisor
NewLog=OldLog + log(candidate)
OldLog=NewLog
test_divisor=3
candidate+=2
print "Your number is", n
print "The sum is", OldLog
print "The ratio of the sum to your number is", OldLog/n

Date: 2009-11-20 03:35 pm (UTC)
medrin: matlab code with everything but 'hold on' blurred (Default)
From: [personal profile] medrin
My solutions for #1

1a)
from math import*

def primenr(nr):                #input, the n:th prime
    primecount=1                #primecount (number of identified primes)
    n=1                         #starts at 1 because 2 is a prime
    while primecount < nr:      #loop till we find the muber of primes we want
        n+=2                    #since only odd nr are prime, add 2 at the time
        if isprime(n):          #returns True if n is a prime
            primecount+=1       #We found another prime!
    return n                    #return the wanted prime

def isprime(n):                 #checks if it's a prime, returns True,
    prime=True                  #if not, returns False
    for i in range(2,int(sqrt(m))):      #goes throug all possible divisors
        if n%i == 0:            #up to the square root. (no possible higher)
            prime=False         #if it finds a divisor, it's not a prime
    return prime


ans=primenr(1000)
print ans

1b)
from math import*

def isprime(m):         #retyrns true if prime, otherwise false.
    prime=True          #same as in ps1a
    for i in range(2,int(sqrt(m))):
        if m%i == 0:
            prime=False
    return prime

def logratio(n):
    sumlog=log(2.0)         #start with adding the log for 2
    for m in range(3,n+1):  #finds all the primes between 2 and n
        if isprime(m):
            sumlog+=log(m)  #adds the log of the prime to the sum.
    print sumlog, m, sumlog/m 

logratio(1000)

Page generated Jun. 25th, 2017 03:25 pm
Powered by Dreamwidth Studios