The definition of prime number : A greater than 1 Natural number of , except 1 And outside of itself , A number that cannot be divided by other natural numbers
That's easy , We just need to n He Cong 1 reach n The operation of remainder of number , If it happens midway n Divisible by a number less than it , Then we can judge n It's not a prime
Put the code at a glance
def sushu(n): if n<2: print(" error : There is no prime before this number ") if n==2: print("2") if n>2: print("2"
) for i in range(3,n+1): for j in range(2,i): if i%j==0: break else: print(i) n=
int(input("n=")) sushu(n)

Code optimization : Because factors that are not prime numbers exist in pairs , We just need to get to the root
import math def sushu(n): if n<2: print(" error : There is no prime before this number ") if n==2: print("2") if n>
2: print("2") for i in range(3,n+1): for j in range(2,int(math.sqrt(i)+1)): if i
%j==0: break else: print(i) n=int(input("n=")) sushu(n)

Pursuing efficiency ,ok no problem !

Technology