From d5afd6c71ff283d5755010a8a1e3e3bcf0021320 Mon Sep 17 00:00:00 2001 From: johanleroy Date: Mon, 8 Sep 2025 14:52:47 +0200 Subject: [PATCH] TP03 --- TP03.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 TP03.py diff --git a/TP03.py b/TP03.py new file mode 100644 index 0000000..bcd3a0d --- /dev/null +++ b/TP03.py @@ -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 d’atteindre le 1000ème nombre premier.") + +if __name__ == "__main__": + main()