Poll & solutions
Nov. 11th, 2009 10:52 pmFirst:
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!
Open to: Registered Users, detailed results viewable to: All, participants: 25
Tickyboxes!
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!
no subject
Date: 2009-11-12 04:14 am (UTC)# 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)no subject
Date: 2009-11-12 06:32 am (UTC)no subject
Date: 2009-11-12 01:15 pm (UTC)(I actually don't know if I knew that about prime numbers at some point and had forgotten it, but I spent quite a few minutes going, "Wait, is that true? I think it might be! Let's see if it works.")
Also, I've been writing a lot of Ruby on Rails code, and it's possible I'm just used to using arrays for everything. :)
no subject
Date: 2009-11-12 01:40 pm (UTC)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!
no subject
Date: 2009-11-12 03:10 pm (UTC)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!
Heh, yes indeed.
no subject
Date: 2009-11-12 03:03 pm (UTC)def find_nth_prime(prime_to_find): n = 1 primes = [] if prime_to_find < 1: print "Please enter a number greater than zero." return None else: while len(primes) < prime_to_find: if len(primes) == 0: primes.append(2) print "%i is prime number #%i" % (primes[0], len(primes)) else: root = math.sqrt(n) for prime in primes: if n%prime == 0: break elif prime > root: primes.append(n) print "%i is prime number #%i" % (n, len(primes)) break n = n + 2 return primes[prime_to_find - 1]