bellerina: (Default)
bellerina ([personal profile] bellerina) wrote in [community profile] intro_to_cs 2009-11-19 05:47 am (UTC)

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

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