anonymous
  • anonymous
Ok, so here is my attempt at problem set 1. Can anyone tell me where I went wrong? import math number=1 primes = [2] moduli=[1] test=3 while number <= 1000: for prime in primes: if (test/prime)>=2: m=test%prime moduli.append(m) moduli.sort() for item in moduli: if 0==item: moduli=[] test=test+2 else: primes.append(test) test=test+2 number=number+1 print primes[1000]
MIT 6.00 Intro Computer Science (OCW)
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Get our expert's

answer on brainly

SEE EXPERT ANSWER

Get your free account and access expert answers to this
and thousands of other questions.

anonymous
  • anonymous
Ok, so here is my attempt at problem set 1. Can anyone tell me where I went wrong? import math number=1 primes = [2] moduli=[1] test=3 while number <= 1000: for prime in primes: if (test/prime)>=2: m=test%prime moduli.append(m) moduli.sort() for item in moduli: if 0==item: moduli=[] test=test+2 else: primes.append(test) test=test+2 number=number+1 print primes[1000]
MIT 6.00 Intro Computer Science (OCW)
jamiebookeater
  • jamiebookeater
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Get this expert

answer on brainly

SEE EXPERT ANSWER

Get your free account and access expert answers to this
and thousands of other questions

anonymous
  • anonymous
ok, so from my understanding you're divising test variables by all primes found so far, right? when I ran your code it gave me 2039 as the 1000th prime which is too low
anonymous
  • anonymous
it seems that by div/mod by primes results in the code finding primes which actually arent
anonymous
  • anonymous
yeah - if we are testing number x, then it is prime if it is NOT evenly divisible by any prime lower than x/2

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

anonymous
  • anonymous
yeah
anonymous
  • anonymous
got it working import math n=1 primes = [2] check=0 test=3 while n <= 1000: for prime in primes: if test%prime==0: check=1 if check==1: test=test+2 check=0 else: primes.append(test) test=test+2 check=0 n=n+1 print primes[999]
anonymous
  • anonymous
just ran your code again to print out the primes when it finds them and it gives e.g. 15, 21,27..all divisible by 3, which is in the list as well - seems like that's responsible
anonymous
  • anonymous
yeah, i got rid of the mod check list, and just had it be a binary result - if one of the primes divides evenly into the test number, it changes the check number to 1, which means test number is not prime
anonymous
  • anonymous
cool! I like your solution - mine was much more complicated - gonna take a closer look at it :)
anonymous
  • anonymous
thanks :) . mind posting your sol'n?
anonymous
  • anonymous
sure: primecount = 0 candidate = 1 while primecount < 999: candidate = candidate +2 divisor = 3 not_prime = False while (divisor < candidate/2) and (not_prime == False): if candidate%divisor == 0: not_prime = True ## print candidate ## print (' is not a prime') divisor = divisor+2 if not_prime == False: ## print (candidate) ## print ('is prime') primecount = primecount +1 print candidate
anonymous
  • anonymous
didn't know about division by primes so divided by all candidates/2 up to the current every time
anonymous
  • anonymous
looks very similar to mine, except for the primes thing. nice.
anonymous
  • anonymous
thx

Looking for something else?

Not the answer you are looking for? Search for more explanations.