Advertisement
LightProgrammer000

Quantidade de numeros primos [python3]

Feb 20th, 2020
486
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.05 KB | None | 0 0
  1. '''
  2. Programa 14: Quantidade de numeros primos
  3. '''
  4.  
  5. def main():
  6.  
  7.     i = 1
  8.     qtd = 0
  9.     lim = int(input("# Limite de numeros primos: "))
  10.  
  11.     # Estrutura de repeticao
  12.     while i > 0:
  13.  
  14.         # Chamada de metodo
  15.         a = primo(i)
  16.  
  17.         # Estrutura de decisao
  18.         if a != 0 and qtd != lim:
  19.  
  20.             # Apresentacao
  21.             print("{}: {}".format(qtd + 1, a))
  22.  
  23.             # Incremento de contagem
  24.             qtd += 1
  25.  
  26.         elif qtd == lim:
  27.             break
  28.  
  29.         # Incremento
  30.         i += 1
  31.  
  32. # Metodo: Verificacao do umero primo
  33. def primo(num):
  34.  
  35.     # Variaveis
  36.     i = 1
  37.     cont = 0
  38.  
  39.     # Estrutura de repeticao
  40.     while i <= num:
  41.  
  42.         # Estrutura condicional:
  43.         if num % i == 0:
  44.             cont += 1
  45.  
  46.         # Estrutura condicional: Verificacao 'nao primo'
  47.         if cont > 2:
  48.             break
  49.  
  50.         # Incremento
  51.         i += 1
  52.  
  53.     # Estrutura condicional: Primo
  54.     if cont == 2:
  55.         return num
  56.  
  57.     else:
  58.         return 0
  59.  
  60. if __name__ == '__main__':
  61.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement