A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 5 years ago
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]
anonymous
 5 years ago
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]

This Question is Closed

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0ok, 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
 5 years ago
Best ResponseYou've already chosen the best response.0it seems that by div/mod by primes results in the code finding primes which actually arent

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0yeah  if we are testing number x, then it is prime if it is NOT evenly divisible by any prime lower than x/2

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0got 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
 5 years ago
Best ResponseYou've already chosen the best response.0just 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
 5 years ago
Best ResponseYou've already chosen the best response.0yeah, 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
 5 years ago
Best ResponseYou've already chosen the best response.0cool! I like your solution  mine was much more complicated  gonna take a closer look at it :)

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0thanks :) . mind posting your sol'n?

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0sure: 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
 5 years ago
Best ResponseYou've already chosen the best response.0didn't know about division by primes so divided by all candidates/2 up to the current every time

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0looks very similar to mine, except for the primes thing. nice.
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.