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