Hi all, I need some help on ps1. After finding all odd number with a if statement, I don't know how to find the prime number. Any hint or advice would be appreciated. This is what I have so far: count = 2 #set to already include 1 and 2 as prime. num = 2 while count <=1000: num = num+1 if (num/2)*2!=num: #number is odd if it does not equal itself #print num count = count +1 # Use last to count if a prime is identified
Hi! To find out if an odd number is a prime number, you need to run a loop that divides the odd by numbers from 2 to the odd itself minus 1! so you need to test this: if odd%divisor == 0: % is the modulo operator, it finds the remainder of division of one number by another, so if the result is zero then the odd number can be divided which means it's not a prime number! I hope this helps, and please excuse my English!
And to make your current code slightly more efficient, why not start with num = 3 so that you can directly use num += 2 while being certain that all are odd?
Join our real-time social learning platform and learn together with your friends!