40 lines
1.3 KiB
Python
40 lines
1.3 KiB
Python
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()
|