<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Primzahlen]]></title><description><![CDATA[<p>Hallo!</p>
<p>Ich habe da ein Problem.<br />
Ich will rausbekommen ob in einer while-Schleife, die eine Zahl hochzählt, diese Zahl eine Primzahl ist. Da bekomme ich trotzdem falsche Ergebnisse bei meiner selbstprogrammierten Schleife, die dies macht.</p>
<p>Wüsste einer wie das so lösen wäre? Vllt. ein neuer Ansatz?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/123263/primzahlen</link><generator>RSS for Node</generator><lastBuildDate>Sun, 23 Aug 2026 13:09:50 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/123263.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 14 Oct 2005 13:04:38 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Primzahlen on Fri, 14 Oct 2005 13:04:38 GMT]]></title><description><![CDATA[<p>Hallo!</p>
<p>Ich habe da ein Problem.<br />
Ich will rausbekommen ob in einer while-Schleife, die eine Zahl hochzählt, diese Zahl eine Primzahl ist. Da bekomme ich trotzdem falsche Ergebnisse bei meiner selbstprogrammierten Schleife, die dies macht.</p>
<p>Wüsste einer wie das so lösen wäre? Vllt. ein neuer Ansatz?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/892327</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/892327</guid><dc:creator><![CDATA[karl05]]></dc:creator><pubDate>Fri, 14 Oct 2005 13:04:38 GMT</pubDate></item><item><title><![CDATA[Reply to Primzahlen on Fri, 14 Oct 2005 13:06:45 GMT]]></title><description><![CDATA[<p>Villeicht mal deine Schleife zeigen, dann kann man dir sagen was falsch ist und du hast den größten Lerneffekt. Ansonsten Google -&gt; Primzahltest.</p>
<p>Gruß</p>
]]></description><link>https://www.c-plusplus.net/forum/post/892331</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/892331</guid><dc:creator><![CDATA[FireFlow]]></dc:creator><pubDate>Fri, 14 Oct 2005 13:06:45 GMT</pubDate></item><item><title><![CDATA[Reply to Primzahlen on Fri, 14 Oct 2005 13:07:15 GMT]]></title><description><![CDATA[<p>Eine while-Schleife die eine Zahl hochzählt klingt nach einer for-Schleife:</p>
<pre><code class="language-cpp">for(int i = 0; Abbruchsbedingung; ++i)
{
    // Hier auf Primzahl testen
    if(isPrim(i)) cout &lt;&lt; i &lt;&lt; &quot; ist eine Primzahl&quot;;
}
</code></pre>
<p>isPrim() ist eine Funktion von dir die eben irgendwie testet obs eine Primzahl ist (Einfachster Weg: Durch alle Zahlen vor der Zahl % ausführeun und auf == 0 prüfen).</p>
<p>Wenn die Schleife für den Algorithmus gebaut wurde zeigst du uns vielleicht besser deine selbstgemachte Version damit wir dir damit helfen können <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>MfG SideWinder</p>
]]></description><link>https://www.c-plusplus.net/forum/post/892332</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/892332</guid><dc:creator><![CDATA[SideWinder]]></dc:creator><pubDate>Fri, 14 Oct 2005 13:07:15 GMT</pubDate></item><item><title><![CDATA[Reply to Primzahlen on Fri, 14 Oct 2005 13:08:04 GMT]]></title><description><![CDATA[<p>na denn zeischn doch ma her deinen Ansatz!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/892333</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/892333</guid><dc:creator><![CDATA[Ansatz]]></dc:creator><pubDate>Fri, 14 Oct 2005 13:08:04 GMT</pubDate></item><item><title><![CDATA[Reply to Primzahlen on Fri, 14 Oct 2005 16:49:43 GMT]]></title><description><![CDATA[<p>Ok, hier ist er:</p>
<pre><code class="language-cpp">while (ende &gt; 0) 
{
  i++; 
  a = i; a++;
  isPrime = 0; isPrime2 = 0;
  for (div=2; isPrime &amp;&amp; div &lt; i; div++)
  {
    if (0==i % div)
    {
      isPrime = 0;
    }
  }
  for (div2=2; isPrime2 &amp;&amp; div2 &lt; a; div2++)
  {
    if (0==a % div2)
    {
      isPrime2 = 0;
    }
  }
  if (isPrime == 0 &amp;&amp; isPrime2 == 0)
  { 
    cout &lt;&lt; &quot;P:  &quot; &lt;&lt; i &lt;&lt;  endl;  
    continue; 
  }
  //weitere andere Rechenoperationen, wenn keine Primzahl vorhanden ist.

}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/892510</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/892510</guid><dc:creator><![CDATA[karl05]]></dc:creator><pubDate>Fri, 14 Oct 2005 16:49:43 GMT</pubDate></item><item><title><![CDATA[Reply to Primzahlen on Fri, 14 Oct 2005 18:17:32 GMT]]></title><description><![CDATA[<p>Ein offensichtlicher Fehler ist die falsche Initialisierung von isPrime:</p>
<pre><code class="language-cpp">isPrime = 1; isPrime2 = 1;
for (div=2; isPrime &amp;&amp; div &lt; i; div++)
</code></pre>
<p>Ansonsten läuft Deine Schleife nicht, weil isPrime nicht erfüllt ist.</p>
<p>Pack diesen Teil Deiner Routine in eine Funktion (wie SideWinder es vorgeschlagen hat), dann musst Du sie nicht zweimal hinschreiben. Ausserdem ist bool isPrime netter als so ein int.</p>
<p>Für die while Schleife hast Du keine Abbruchbedingung, aber ich nehme mal an, dass das nicht Dein Problem ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/892561</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/892561</guid><dc:creator><![CDATA[niemand]]></dc:creator><pubDate>Fri, 14 Oct 2005 18:17:32 GMT</pubDate></item><item><title><![CDATA[Reply to Primzahlen on Mon, 17 Oct 2005 06:34:28 GMT]]></title><description><![CDATA[<p>So ich habe das jetzt so verändert:</p>
<pre><code>int ende;
int i = 10, a;
while (ende &gt; 0) {
  i++; 
  a = i; a++;

  if (isPrime(i) == 0 &amp;&amp; isPrime(a) == 0)
  { 
    cout &lt;&lt; &quot;P:  &quot; &lt;&lt; i &lt;&lt;  endl; 
    continue; 
  }
  //hier gehts nur weiter, wenn es NICHT zwei aufeinanderfolgende Primzahlen    gibt
}

int isPrime(int zahl)
{
  int div;
  for (div=2; div &lt; zahl; div++)
  {
    if (0 == zahl % div)
    {
      return 0;
    }
    else if (1 == zahl % div)
    {
      return 1;
    }
  }  
}
</code></pre>
<p>Das will immer noch nicht richtig funktionieren. Mit den Variablen i u. a will ich 2 Zahlenwerte habe, die nicht Primzahlen sind, aber wie schon gesagt, a muss i + 1 sein und beide dürfen keine Primzahlen sein. Dann kann erst in der while-Schleife fortgefahren werden(darum die if Abzweigung).</p>
<p>Ich hoffe ihr könnt mir helfen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/893846</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/893846</guid><dc:creator><![CDATA[karl05]]></dc:creator><pubDate>Mon, 17 Oct 2005 06:34:28 GMT</pubDate></item><item><title><![CDATA[Reply to Primzahlen on Mon, 17 Oct 2005 06:58:25 GMT]]></title><description><![CDATA[<p>Immer noch keine Code-Tags...</p>
<p>Deine Routine isPrime ist fehlerhaft.</p>
<pre><code class="language-cpp">int isPrime(int zahl)
{
  int div;
  for (div=2; div &lt; zahl; div++)
  {
    if (0 == zahl % div)
    {
      return 0;
    }
    else if (1 == zahl % div)
    {
      return 1;
    }
  }
}
</code></pre>
<p>Was genau soll das bewirken? In dem Augenblick wo Du eine Zahl gefunden hast,<br />
die Rest 1 ergibt, sagst Du die Zahl ist prim?<br />
Beispiel: zahl = 4; div = 3 -&gt; 4%3 == 1 -&gt; 4 ist Primzahl?</p>
<p>Es ist eine Primzahl, wenn sie sich durch keine (!) Zahl ohne Rest teilen lässt, d.h. Du kannst erst nach Beendigung Deiner Schleife sagen, dass es eine Primzahl ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/893856</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/893856</guid><dc:creator><![CDATA[niemand]]></dc:creator><pubDate>Mon, 17 Oct 2005 06:58:25 GMT</pubDate></item><item><title><![CDATA[Reply to Primzahlen on Mon, 17 Oct 2005 08:25:31 GMT]]></title><description><![CDATA[<p>warum soll man denn alle zahlen, die kleiner sind durchprobieren? ab der wurzel kann doch nix mehr kommen, oder? schau mal, ob du was hiermit anfangen kannst:</p>
<pre><code class="language-cpp">bool isPrime(unsigned long number)
{
	unsigned long j = 1, limit;

	if (number &lt; 2)
		return false;

	limit = static_cast&lt;unsigned long&gt;(sqrt(number));
	while (j &lt;= limit &amp;&amp; number % ++j != 0) ;

	return j &gt; limit;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/893897</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/893897</guid><dc:creator><![CDATA[schorsch code]]></dc:creator><pubDate>Mon, 17 Oct 2005 08:25:31 GMT</pubDate></item><item><title><![CDATA[Reply to Primzahlen on Mon, 17 Oct 2005 08:31:14 GMT]]></title><description><![CDATA[<p>Danke für eure Hilfe ich habe es jetzt raus. Die Funktion muss natürlich so lauten:</p>
<pre><code class="language-cpp">bool isPrime(int zahl)
{
  int div;
  bool e = true;
  for (div=2; e &amp;&amp; div &lt; zahl; div++)
  {
    if (0 == zahl % div)
    {
      e = false;
    }
  }
  return e;  
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/893901</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/893901</guid><dc:creator><![CDATA[karl05]]></dc:creator><pubDate>Mon, 17 Oct 2005 08:31:14 GMT</pubDate></item><item><title><![CDATA[Reply to Primzahlen on Mon, 17 Oct 2005 12:04:27 GMT]]></title><description><![CDATA[<p>Informiere Dich mal über das &quot;Sieb des Eratosthenes&quot; <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/894059</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/894059</guid><dc:creator><![CDATA[ChrisPlusPlus]]></dc:creator><pubDate>Mon, 17 Oct 2005 12:04:27 GMT</pubDate></item></channel></rss>