[Python] Finding prime number with Python

Discussão em 'Python' iniciado por Stack, Março 8, 2025.

  1. Stack

    Stack Membro Participativo

    I have following snippet which finds numbers which are not co-prime to 20. But, I want to achieve the opposite i.e. printing the prime numbers. It should be achieved with something like (((i%j)!==0) & ((z%j)!==0)). I am new to python and hence not sure about the syntax of "!". Can someone edit the code so that it prints the prime numbers?

    #Finding co prime of a number (e.g.20)
    k=0
    z=20
    for i in range(3,31):
    for j in range(2,30):
    if(((i%j)==0) & ((z%j)==0)):
    k=k+1
    if(k==1):
    print(i) #the printed number is not co prime to 20
    k=0


    Following is the output of the code snippet:

    4
    5
    6
    8
    10
    12
    14
    15
    16
    18
    20
    22
    24
    25
    26
    28
    30

    Continue reading...

Compartilhe esta Página