From 65bbf7b4cd1b0a7958047d54282f372f0fbaf91c Mon Sep 17 00:00:00 2001 From: Le Stagiaire Date: Wed, 20 Nov 2024 09:18:09 +0100 Subject: [PATCH] =?UTF-8?q?Am=C3=A9liorations=20de=20la=20recherche=20de?= =?UTF-8?q?=20nombres=20premiers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Changement de l'outil de plateforme de `v143` à `v142` dans `CPP.vcxproj`. * Ajout de `` et de `setlocale(LC_CTYPE, "fr");` dans `Source8.cpp`. * Suppression du tableau `nombres_premiers[100]`. * Modification de la boucle `for` pour commencer à 3 et incrémenter de 2. * Ajout de l'affichage des nombres premiers trouvés. * Correction de la vérification des diviseurs et des conditions de sortie de boucle. * Modification de la condition d'impression des nombres premiers. --- CPP/CPP.vcxproj | 2 +- CPP/Source8.cpp | 17 +++++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/CPP/CPP.vcxproj b/CPP/CPP.vcxproj index 10ff281..87dd365 100644 --- a/CPP/CPP.vcxproj +++ b/CPP/CPP.vcxproj @@ -48,7 +48,7 @@ Application true - v143 + v142 Unicode diff --git a/CPP/Source8.cpp b/CPP/Source8.cpp index 1923aa3..0e20bae 100644 --- a/CPP/Source8.cpp +++ b/CPP/Source8.cpp @@ -7,24 +7,29 @@ #include #include #include +#include int main(void) { do { + setlocale(LC_CTYPE, "fr"); system("cls"); printf_s("\t\t\tProgramme de calcul des nombres premiers\n"); printf_s("Donnez le nombre limite (>3) : "); - int nombres_premiers[100]; int limite = 3; scanf_s("%d", &limite); - for (int i = 0; i <= limite; i++) { + printf_s("Nombre premiers trouvés : \n\t1\t2\t"); + for (int i = 3; i <= limite; i += 2) { int nb_diviseurs = 0; - for (int j = 2; j <= i; j++) { - if (i % j == 1) { + for (int j = 3; j <= i / 2; j += 2) { + if ((i % j) == 0) { nb_diviseurs++; } + if (nb_diviseurs > 2) { + break; + } } - if (nb_diviseurs == 1) { - + if (nb_diviseurs <= 2) { + printf_s("%i\t", i); } } printf_s("\n\nAppuyez sur Echap pour quitter...");