This commit is contained in:
johanleroy
2025-09-08 14:52:47 +02:00
parent 502f59842b
commit d5afd6c71f

39
TP03.py Normal file
View File

@@ -0,0 +1,39 @@
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()