<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Crible D'Ératosthène on kenji.blog</title><link>http://kenji.blog/fr/tags/crible-d%C3%A9ratosth%C3%A8ne/</link><description>Recent content in Crible D'Ératosthène on kenji.blog</description><generator>Hugo -- gohugo.io</generator><language>fr</language><copyright>kenjinote</copyright><lastBuildDate>Sun, 09 Apr 2023 12:54:24 +0900</lastBuildDate><atom:link href="http://kenji.blog/fr/tags/crible-d%C3%A9ratosth%C3%A8ne/index.xml" rel="self" type="application/rss+xml"/><item><title>Comment lister les nombres premiers inférieurs à 1000 en utilisant le Crible d'Ératosthène</title><link>http://kenji.blog/fr/p/comment-lister-les-nombres-premiers-inf%C3%A9rieurs-%C3%A0-1000-en-utilisant-le-crible-d%C3%A9ratosth%C3%A8ne/</link><pubDate>Sun, 09 Apr 2023 12:54:24 +0900</pubDate><guid>http://kenji.blog/fr/p/comment-lister-les-nombres-premiers-inf%C3%A9rieurs-%C3%A0-1000-en-utilisant-le-crible-d%C3%A9ratosth%C3%A8ne/</guid><description>&lt;img src="http://kenji.blog/p/%E3%82%A8%E3%83%A9%E3%83%88%E3%82%B9%E3%83%86%E3%83%8D%E3%82%B9%E3%81%AE%E7%AF%A9%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A61000%E4%BB%A5%E4%B8%8B%E3%81%AE%E7%B4%A0%E6%95%B0%E3%82%92%E5%88%97%E6%8C%99%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95/img.png" alt="Featured image of post Comment lister les nombres premiers inférieurs à 1000 en utilisant le Crible d'Ératosthène" />&lt;h2 id="quest-ce-que-le-crible-dératosthène-">Qu&amp;rsquo;est-ce que le Crible d&amp;rsquo;Ératosthène ?
&lt;/h2>&lt;p>Le Crible d&amp;rsquo;Ératosthène est un algorithme utilisé pour trouver tous les nombres premiers jusqu&amp;rsquo;à une limite donnée.
L&amp;rsquo;algorithme est simple et peut être implémenté via les étapes suivantes :&lt;/p>
&lt;ol>
&lt;li>Créer un tableau booléen de N éléments et initialiser tous les éléments à vrai (true).&lt;/li>
&lt;li>Définir les 0ème et 1er éléments du tableau sur faux (false) (car 0 et 1 ne sont pas premiers).&lt;/li>
&lt;li>Si le 2ème élément du tableau est vrai, afficher 2 comme nombre premier.&lt;/li>
&lt;li>Définir tous les multiples de 2 supérieurs ou égaux à $2^2$ sur faux (*).&lt;/li>
&lt;li>Si le 3ème élément du tableau est vrai, afficher 3 comme nombre premier.&lt;/li>
&lt;li>Définir tous les multiples de 3 supérieurs ou égaux à $3^2$ sur faux.&lt;/li>
&lt;li>Répéter le même processus pour les 4ème, 5ème, &amp;hellip;, et N-ième éléments.&lt;/li>
&lt;/ol>
&lt;ul>
&lt;li>La raison pour laquelle on commence à marquer comme faux à partir du carré du nombre est que les nombres inférieurs au carré ont déjà été traités lors des étapes précédentes.&lt;/li>
&lt;/ul>
&lt;p>&lt;img src="http://kenji.blog/p/%E3%82%A8%E3%83%A9%E3%83%88%E3%82%B9%E3%83%86%E3%83%8D%E3%82%B9%E3%81%AE%E7%AF%A9%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A61000%E4%BB%A5%E4%B8%8B%E3%81%AE%E7%B4%A0%E6%95%B0%E3%82%92%E5%88%97%E6%8C%99%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95/Animation_Sieb_des_Eratosthenes.gif"
width="445"
height="369"
srcset="http://kenji.blog/p/%E3%82%A8%E3%83%A9%E3%83%88%E3%82%B9%E3%83%86%E3%83%8D%E3%82%B9%E3%81%AE%E7%AF%A9%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A61000%E4%BB%A5%E4%B8%8B%E3%81%AE%E7%B4%A0%E6%95%B0%E3%82%92%E5%88%97%E6%8C%99%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95/Animation_Sieb_des_Eratosthenes_hua63c6218ac9f9cdba93ccb20db392e0e_206214_480x0_resize_box_1.gif 480w, http://kenji.blog/p/%E3%82%A8%E3%83%A9%E3%83%88%E3%82%B9%E3%83%86%E3%83%8D%E3%82%B9%E3%81%AE%E7%AF%A9%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A61000%E4%BB%A5%E4%B8%8B%E3%81%AE%E7%B4%A0%E6%95%B0%E3%82%92%E5%88%97%E6%8C%99%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95/Animation_Sieb_des_Eratosthenes_hua63c6218ac9f9cdba93ccb20db392e0e_206214_1024x0_resize_box_1.gif 1024w"
loading="lazy"
class="gallery-image"
data-flex-grow="120"
data-flex-basis="289px"
>&lt;/p>
&lt;h2 id="implémentation-en-rust">Implémentation en Rust
&lt;/h2>&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt"> 1
&lt;/span>&lt;span class="lnt"> 2
&lt;/span>&lt;span class="lnt"> 3
&lt;/span>&lt;span class="lnt"> 4
&lt;/span>&lt;span class="lnt"> 5
&lt;/span>&lt;span class="lnt"> 6
&lt;/span>&lt;span class="lnt"> 7
&lt;/span>&lt;span class="lnt"> 8
&lt;/span>&lt;span class="lnt"> 9
&lt;/span>&lt;span class="lnt">10
&lt;/span>&lt;span class="lnt">11
&lt;/span>&lt;span class="lnt">12
&lt;/span>&lt;span class="lnt">13
&lt;/span>&lt;span class="lnt">14
&lt;/span>&lt;span class="lnt">15
&lt;/span>&lt;span class="lnt">16
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">fn main() {
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> let n = 1000;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> let mut is_prime = vec![true; n+1];
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> is_prime[0] = false;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> is_prime[1] = false;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> for i in 2..=n {
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> if is_prime[i] {
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> println!(&amp;#34;{}&amp;#34;, i);
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> let mut j = i * i;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> while j &amp;lt;= n {
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> is_prime[j] = false;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> j += i;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> }
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> }
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> }
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;h2 id="version-légèrement-optimisée">Version légèrement optimisée
&lt;/h2>&lt;p>Nous pouvons optimiser légèrement l&amp;rsquo;implémentation en considérant les points suivants :&lt;/p>
&lt;ul>
&lt;li>Initialiser le tableau à faux (false) au lieu de vrai (true) (cela est plus rapide).&lt;/li>
&lt;li>Puisque les multiples de 2 ne sont pas des nombres premiers, ignorer l&amp;rsquo;étape visant à définir les multiples de 2 sur faux.&lt;/li>
&lt;li>Il n&amp;rsquo;est pas nécessaire de boucler jusqu&amp;rsquo;à n ; trouver les nombres premiers jusqu&amp;rsquo;à la racine carrée de n suffit pour trouver tous les nombres premiers inférieurs ou égaux à n.&lt;/li>
&lt;/ul>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt"> 1
&lt;/span>&lt;span class="lnt"> 2
&lt;/span>&lt;span class="lnt"> 3
&lt;/span>&lt;span class="lnt"> 4
&lt;/span>&lt;span class="lnt"> 5
&lt;/span>&lt;span class="lnt"> 6
&lt;/span>&lt;span class="lnt"> 7
&lt;/span>&lt;span class="lnt"> 8
&lt;/span>&lt;span class="lnt"> 9
&lt;/span>&lt;span class="lnt">10
&lt;/span>&lt;span class="lnt">11
&lt;/span>&lt;span class="lnt">12
&lt;/span>&lt;span class="lnt">13
&lt;/span>&lt;span class="lnt">14
&lt;/span>&lt;span class="lnt">15
&lt;/span>&lt;span class="lnt">16
&lt;/span>&lt;span class="lnt">17
&lt;/span>&lt;span class="lnt">18
&lt;/span>&lt;span class="lnt">19
&lt;/span>&lt;span class="lnt">20
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">fn main() {
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> let n = 1000;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> let mut is_prime = vec![false; n+1];
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> is_prime[2] = true;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> for i in (3..=n).step_by(2) {
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> is_prime[i] = true;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> }
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> for i in 3..=((n as f64).sqrt() as usize) {
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> if is_prime[i] {
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> let mut j = i * i;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> while j &amp;lt;= n {
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> is_prime[j] = false;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> j += i * 2;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> }
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> }
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> }
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> for i in (2..=n).filter(|&amp;amp;x| is_prime[x]) {
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> println!(&amp;#34;{}&amp;#34;, i);
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> }
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;h2 id="référence">Référence
&lt;/h2>&lt;ul>
&lt;li>&lt;a class="link" href="https://ja.wikipedia.org/wiki/%E3%82%A8%E3%83%A9%E3%83%88%E3%82%B9%E3%83%86%E3%83%8D%E3%82%B9%E3%81%AE%E7%AF%A9" target="_blank" rel="noopener"
>Crible d&amp;rsquo;Ératosthène (Wikipedia)&lt;/a>&lt;/li>
&lt;/ul></description></item></channel></rss>