Advertisement
elizeub

Calculadora de números primos em python

Jul 31st, 2021
2,151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.38 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. #  NumerosPrimos.py
  5. #  
  6. #  Copyright 2021 Elizeu Barbosa Abreu <elizeubcorreios@gmail.com>
  7. #  
  8. #  This program is free software; you can redistribute it and/or modify
  9. #  it under the terms of the GNU General Public License as published by
  10. #  the Free Software Foundation; either version 2 of the License, or
  11. #  (at your option) any later version.
  12. #  
  13. #  This program is distributed in the hope that it will be useful,
  14. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. #  GNU General Public License for more details.
  17. #  
  18. #  You should have received a copy of the GNU General Public License
  19. #  along with this program; if not, write to the Free Software
  20. #  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  21. #  MA 02110-1301, USA.
  22. #  
  23. #
  24.  
  25. import os
  26. from time import sleep
  27.  
  28. class cor:
  29.     vermelho = '\033[91m'
  30.     verde = '\033[92m'
  31.     inversa = '\033[7m'
  32.     fim = '\033[m'
  33.  
  34. def cls():
  35.     os.system('clear')
  36.  
  37. count = 0
  38.  
  39. # PARA SER PRIMO O NÚMERO PODE SER DIVISÍVEL APENAS POR ELE MESMO OU POR 1.
  40. # USEI UM CONTADOR QUE CONTA TODAS AS VEZES QUE O NUMERO SENDO DIVIDIDO PELOS SEUS
  41. # ANTECESSORES RETORNA TRUE. SEI QUE ESTE NÃO É O MÉTODO MAS RÁPIDO MAS FOI A MANEIRA ENCONTRADA
  42.  
  43. def primo(count, numero):
  44.     for n in range(numero, 0, -1):
  45.         if numero % n == 0:
  46.             count += 1
  47.     if count == 2:
  48.         print('\n{}{} É UM NÚMERO PRIMO...{}'.format(cor.verde, numero, cor.fim ))
  49.     else:
  50.         print('\n{}{} NÃO É UM NÚMERO PRIMO...{}'.format(cor.vermelho, numero, cor.fim))
  51.  
  52.  
  53. def main():
  54.  
  55.     cls()
  56.    
  57.     print('{}{:^45}{}'.format(cor.inversa, 'CALCULADORA DE NÚMEROS PRIMOS', cor.fim))
  58.  
  59.     print('''
  60.     ESTE ALGORITMO DESCOBRE SE UM NÚMERO DIGITADO É PRIMO!!!
  61.     CUIDADO! O CÁLCULO DE NÚMEROS PRIMOS DE VÁRIOS DÍGITOS EXIGE BASTANTE MEMÓRIA...
  62.     SEJA PACIENTE AO DIGITAR NÚMEROS MAIORES. VÁRIOS CÁLCULOS ESTÃO SENDO REALIZADOS!!!!
  63.    
  64.     * Para sair pressione [CTRL+C] a qualquer momento!!!
  65. ''')
  66.     x = str(input('Digite um número inteiro para analisar: '))
  67.  
  68.     # ANÁLISE SE O NÚMERO DIGITADO É NUMÉRICO
  69.     if x.isnumeric() == False:
  70.         print('\n{}ENTRADA INVÁLIDA... VOCÊ NÃO DIGITOU UM VALOR NUMÉRICO!!!!{}'.format(cor.vermelho, cor.fim))
  71.         sleep(3)
  72.         main()
  73.     else:
  74.         numero = int(x)
  75.    
  76.     primo(count, numero)
  77.     sleep(7)
  78.  
  79. while True:
  80.     main()
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement