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
no subject
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