fork download
  1. def primos_maiores_que_n(n, lim):
  2. return primos_menores_que_n(lim, start=n)
  3.  
  4. def maior_primo(n):
  5. for num in reversed(range(1,n+1)):
  6. if all(num%i!=0 for i in range(2,num)):
  7. return num
  8.  
  9. def e_primo(num):
  10. return all(num%i!=0 for i in range(2,num))
  11.  
  12. def primos_menores_que_n(n, start=1):
  13. for num in range(start,n):
  14. if all(num%i!=0 for i in range(2,num)):
  15. yield num
  16.  
  17. print(list(primos_menores_que_n(10))) # [1, 2, 3, 5, 7]
  18. print(list(primos_maiores_que_n(10, 100))) # [11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
  19. print(maior_primo(100)) # 97
  20. print(e_primo(10)) # False
  21. print(e_primo(11)) # True
Success #stdin #stdout 0.01s 27712KB
stdin
Standard input is empty
stdout
[1, 2, 3, 5, 7]
[11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
97
False
True