A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 5 years ago
py2: very close  but it's looping and I don't know how to actually check for the prime (I understand the rest) here's the code
th=0
count=3
while count<8000:
count=count+1
if (count/2)*2!=count:
for x in range(2count):
if count%x==0:
print 'not a prime'
else:
th=th+1
print 'the',th,'number is',count
anonymous
 5 years ago
py2: very close  but it's looping and I don't know how to actually check for the prime (I understand the rest) here's the code th=0 count=3 while count<8000: count=count+1 if (count/2)*2!=count: for x in range(2count): if count%x==0: print 'not a prime' else: th=th+1 print 'the',th,'number is',count

This Question is Closed

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0update : still not working though.. import math #sqrt function th=0 # this number initializs the counter count=0 #this number initializes the number to be checked for primality while count<8000: #will check all numbers between 0 and 8000 count=count+1 #add 1 number up if (count/2)*2!=count: #to check if the number is even or odd  if its odd for x in range(2,math.sqrt(count)): #this is where i'm stuck  wanted to set up a range of numbers to use on the number if count%x==0: #if the the formula has no remainder print '' #nothing will happen else: #but if it does have a remainder th=th+1 #add up the number of primes print 'the',th,'number is',count #and print the number

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0or this ? import math #sqrt function ifprime=0 count=0 Prime=False while ifprime<8000: ifprime=ifprime+1 if (ifprime/2)*2!=ifprime: Prime=True else: Prime=False if Prime==True: for i in range(2,ifprime): if ifprime%i==0: Prime=False else: Prime=True if Prime==True: count=count+1 if count==1000 and Prime==True: print ifprime

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0import math #sqrt function ifprime=3 count=0 Prime=False while ifprime<100: ifprime=ifprime+1 if (ifprime/2)*2!=ifprime: Prime=True else: Prime=False if Prime==True: for i in range(2,math.sqrt(ifprime)): if ifprime%i==0: Prime=False else: Prime=True if Prime==True: count=count+1 if Prime==True: print ifprime

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Hi grupiyati, try pasting your code to one of paste sites, dpaste.com or pastebin.com. I've added your code to dpaste.com http://dpaste.com/543243/ This will help with pointing out line numbers and show indentations correctly. There's two mistakes in your code, one at line 12 and one at line 15,16. There's a bunch of coding style issues, that's not functional, but will make your code a lot prettier and easier to understand. Let's go over the mistake first. At line 12, range expects an integer for the start and end value, math.sqrt returns a float, so that's a compiler syntax error. You can cast the math.sqrt value to an int, by doing int(math.sqrt(ifprime)), but that'll just truncate the value. If math.sqrt(ifprime) return 4.6, int(math.sqrt(ifprime)) would return 4. Note, it truncates, not rounding. Hint, you'll need to do more here to fudge the number, make sure you're generating a number greater than math.sqrt(ifprime). At line 15,16: this is your real error. Think about what happens once you found a number that is not prime, do you stop or do you continue with your 'for' loop. You actually continue with your loop. Not only that, the next iteration can potentially set Prime to True again. You can fix it in two ways, 1. stop the loop once you found out the number is not prime using a 'break' statement. or 2. don't allow the variable 'Prime' to be set to True once it has been determine that a number is prime. Note: your counter is off, since you're not accounting for the number 2, and 3. I'm assuming you just want the base code running. So I'm not correcting you on these. Now for style errors: line 7to11. You're really just trying to determine if the number is odd or even, not whether it's prime or not. So you should really use a different variable name. For example, isOdd or isEven. line 17 to 20, a 'if' statement can contain multiple statement inside it. You don't need to have both line 17 and line 19, since the check is the same. Note line 11 needs to be in a separate 'if' statement because you are actually changing the variable 'Prime' inside of the 'if'. line 17 to 20 can be recode to be: if Prime==True: count=count+1 print ifprime

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0for the error at line 15,16. The third option is to only allow Prime to be set to False inside the for loop. (This is actually what I was trying to say with option 2, but nevertheless it's a valid option)
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.