Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 32 Online
OpenStudy (anonymous):

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

OpenStudy (anonymous):

update : 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

OpenStudy (anonymous):

or 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

OpenStudy (anonymous):

import 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

OpenStudy (anonymous):

Hi 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

OpenStudy (anonymous):

for 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)

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Latest Questions
YoungBlood: STOP THE YELLOW NAME TAG SLANDER!
4 hours ago 11 Replies 2 Medals
Bubblezz: Art for @euphoriiic
7 hours ago 23 Replies 3 Medals
ilovemybf: i need more drawing ideas
8 hours ago 15 Replies 1 Medal
toga: what is a Mayuri
12 hours ago 3 Replies 1 Medal
Midnight97: Here is a beat I made let me know what y'all think
12 hours ago 24 Replies 2 Medals
toga: who thinks that there should be more titles
13 hours ago 5 Replies 0 Medals
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!