gchick: Small furry animal wearing a tin-foil hat (Default)
gchick ([personal profile] gchick) wrote in [community profile] intro_to_cs 2009-11-12 04:36 am (UTC)

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		
	

Post a comment in response:

This community only allows commenting by members. You may comment here if you're a member of intro_to_cs.
(will be screened if not on Access List)
(will be screened if not on Access List)
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting