Hi guys, I'm doing the 2008 course and I think it's going well but I have no way to check my assignments. So I'm attaching my solution to the second problem on set 1 - Product of the primes. I would love it if someone would give me feedback. My output is pretty close to 1, but I'm not sure if it's close enough. This is my solution: ``` from math import * def prime_compute(n): is_it_prime = 3 # prime_list = [2] #put 2 inside. 2 is a Prime prime_sum = log1p(2) remains = 1 while is_it_prime < n: for i in range(3, (is_it_prime/2)): remains *= is_it_prime%i if remains == 0: remains = 1 else: # prime_list.append(is_it_prime) prime_sum += log1p(is_it_prime) remains = 1 is_it_prime += 2 print "the sum of all primes is ", prime_sum print "the prime number is ", n print "the ratio is ", float(n/prime_sum) n = 9973 prime_compute(n) ``` Thanks in advance! Michelle [Hakabuk]
There is code quoting here and code paste sites that make sharing code easier. To code quote here, use ``` (the one on the ~ key) above and below the code block. ``` from math import * def prime_compute(n): is_it_prime = 3 ``` As you can see, this does code highlighting. I have attached what I typed so you can see what the input looked like to get that output.
Thanks for the tip, my bad. Here's the code again: ``` from math import * def prime_compute(n): is_it_prime = 3 # prime_list = [2] #put 2 inside. 2 is a Prime prime_sum = log1p(2) remains = 1 while is_it_prime < n: for i in range(3, (is_it_prime/2)): remains *= is_it_prime%i if remains == 0: remains = 1 else: # prime_list.append(is_it_prime) prime_sum += log1p(is_it_prime) remains = 1 is_it_prime += 2 print "the sum of all primes is ", prime_sum print "the prime number is ", n print "the ratio is ", float(n/prime_sum) n = 9973 prime_compute(n) ```
Hey, first off, lots of thought you've put into that! You're good!! I have a couple of questions, if you're interested in style comments. But, first, I wanted to make sure that I'm correctly understanding your algorithm. Just to make sure I'm understanding your intent, I've added comments saying what I think your algorithm is doing. Does it look like I'm understanding your intent correctly? ``` def prime_compute(n): # IS_IT_PRIME is a number that MAY be a prime. We can start it at # 3, skipping 2, because we initialize our PRIME_SUM with # the log of 2, in the statement that follows this one. So, we know we'll # take care of 2- which we KNOW is a prime- by using it to start our sum. is_it_prime = 3 # take care of 2, the first prime, by starting with its log as our sum. prime_sum = log1p(2) # we must initialize REMAINS, because we multiply by it in the loop below remains = 1 while is_it_prime < n: # In the following FOR, we check to see if IS_IT_PRIME can be evenly # divided- i.e. can be divided without leaving any remainder. # # We can start the FOR loop at 3 because we KNOW that IS_IT_PRIME isn't # divisible by 2: we only give set it to the odd values 3, 5, 7... for i in range(3, (is_it_prime/2)): # When remainder (%) operation in next statement returns 0, we have # REMAINS = REMAINS * 0, which means REMAINS will become 0. # After that, REMAINS will STAY 0, since it's one of the operands # in the multiplication, and multiplying with 0 always gives 0. remains *= is_it_prime%i # if REMAINS is now 0, then the remainder at some point must have # been 0. So, IS_IT_PRIME is NOT a prime, since there was a number # that divided it evenly, because we had some remainder that was 0. if remains == 0: # IS_IT_PRIME was NOT a prime. # Set REMAINS back to 1 to make ready for the next FOR iteration. remains = 1 else: # IS_IT_PRIME indeed WAS a prime, so add its log into our sum prime_sum += log1p(is_it_prime) # then, as we did in the other clause of this IF statement, # set REMAINS back to 1 to make ready for the next FOR iteration. remains = 1 # increment IS_IT_PRIME by 2, to make it the next odd number: 3, 5, 7... is_it_prime += 2 print "the sum of all primes is ", prime_sum print "the prime number is ", n print "the ratio is ", float(n/prime_sum) n = 9973 prime_compute(n) ```
you can compare your answer with mine: https://gist.github.com/jhonny111s/10928777
Here's mine (python 3 code): from math import * n = 100000 log_primes = log(2) possible_primes = range(3, n, 2) for i in possible_primes: for j in range(2, i//2): if i%j == 0: break else: log_primes += log(i) print("The sum of the log of primes less than %d is %f. The ratio is %f" % (n, log_primes, log_primes/n))
Join our real-time social learning platform and learn together with your friends!