Files
ENI-Python/TP03.py
johanleroy d5afd6c71f TP03
2025-09-08 14:52:47 +02:00

40 lines
1.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
def est_premier(n: int) -> bool:
if n < 2:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
def nombres_premiers_jusqu_a(limite: int) -> list[int]:
premiers = []
for n in range(2, limite + 1):
if est_premier(n):
premiers.append(n)
return premiers
def main():
limite_input = input("Saisir la limite supérieure pour la recherche de nombres premiers (par défaut 1000) : ").strip()
if limite_input == '':
limite = 1000
else:
try:
limite = int(limite_input)
if limite < 2:
print("La limite doit être au moins 2. Utilisation de 1000.")
limite = 1000
except ValueError:
print("Entrée invalide. Utilisation de 1000.")
limite = 1000
premiers = nombres_premiers_jusqu_a(limite)
print(f"\nNombres premiers jusqu'à {limite} :")
print(premiers)
print(f"\nNombre total de nombres premiers : {len(premiers)}")
if len(premiers) >= 1000:
print(f"Le 1000ème nombre premier est : {premiers[999]}")
else:
print("La limite saisie ne permet pas datteindre le 1000ème nombre premier.")
if __name__ == "__main__":
main()