elz: (ada-tubes)
elz ([personal profile] elz) wrote in [community profile] intro_to_cs 2009-11-12 04:14 am (UTC)

Okay, this is a little rough, but I'm still at the point where I'm happy that it's a) in Python and b) runs. For the first question:

# Returns the requested prime number
def find_nth_prime(prime_to_find):
    n = 3
    primes = [2]
    print "%i is prime number #%i" % (primes[0], len(primes))
    while len(primes) < prime_to_find:
        n_is_prime = True
        for prime in primes:
            n_is_prime = (n%prime != 0)
            if n_is_prime is False:
                break
        if n_is_prime:
            primes = primes + [n]
            print "%i is prime number #%i" % (n, len(primes))
        n = n + 2
    return primes[prime_to_find - 1]
    
prime_to_find = int(raw_input('Which prime number would you like to find?\n'))
find_nth_prime(prime_to_find)

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