<?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[Primzahl geht nur bis Zahl 32?]]></title><description><![CDATA[<p>Hallo,</p>
<p>Ich habe folgendes Programm geschrieben:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;cmath&gt;
using namespace std;

bool is_prime(int x) {
     for (int i=2; i &lt;= sqrt(x); ++i) {
         if (x % 1 == 0) return false;
         }
         return (x &gt; 1);
         }

int main() {

    int z;

    cout &lt;&lt; &quot;Bitte geben Sie eine ganze Zahl ein: &quot; &lt;&lt; endl;

    if (cin &gt;&gt; z) {

            if (is_prime(z)) {

                             cout &lt;&lt; &quot;Die Zahl &quot; &lt;&lt; z &lt;&lt; &quot; ist eine Primzahl&quot; &lt;&lt; endl;
                             }

            else {
                 cout &lt;&lt; &quot;Die Zahl &quot; &lt;&lt; z &lt;&lt; &quot; ist keine Primzahl&quot; &lt;&lt; endl;
                 }

                 }
                 system(&quot;PAUSE&quot;);

                 }
</code></pre>
<p>(sry, die formatierungen gingen jetzt durch das kopieren kaputt.. mein dummer alter pc ^^)</p>
<p>Wenn ich die Zahl 31 eingebe, dann funktioniert noch alles prima. Aber ab 32 bekomme ich dann ein Minus-Ergebnis und ab der Zahl 34 (oder größer) nur immer das Ergebnis 0 (in Worten: Null).</p>
<p>Warum ist das denn so? Ich denke, dass hat etwas mit &quot;int&quot; zu tun, da &quot;int&quot; nur so und so viel speichern kann, richtig? Aber warum kommt dann auf einmal ein minus-ergebnis raus?</p>
<p>Und &quot;int&quot; kann doch eigentlich gar keine so großen Zahlen speichern - warum kommt nicht schon viel früher immer das Ergebnis 0 raus, sondern erst bei einer ganz großen zahl?</p>
<p>schonmal danke!</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/225899/primzahl-geht-nur-bis-zahl-32</link><generator>RSS for Node</generator><lastBuildDate>Thu, 01 Oct 2026 08:06:43 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/225899.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 26 Oct 2008 17:10:32 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Primzahl geht nur bis Zahl 32? on Sun, 26 Oct 2008 17:10:32 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>Ich habe folgendes Programm geschrieben:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;cmath&gt;
using namespace std;

bool is_prime(int x) {
     for (int i=2; i &lt;= sqrt(x); ++i) {
         if (x % 1 == 0) return false;
         }
         return (x &gt; 1);
         }

int main() {

    int z;

    cout &lt;&lt; &quot;Bitte geben Sie eine ganze Zahl ein: &quot; &lt;&lt; endl;

    if (cin &gt;&gt; z) {

            if (is_prime(z)) {

                             cout &lt;&lt; &quot;Die Zahl &quot; &lt;&lt; z &lt;&lt; &quot; ist eine Primzahl&quot; &lt;&lt; endl;
                             }

            else {
                 cout &lt;&lt; &quot;Die Zahl &quot; &lt;&lt; z &lt;&lt; &quot; ist keine Primzahl&quot; &lt;&lt; endl;
                 }

                 }
                 system(&quot;PAUSE&quot;);

                 }
</code></pre>
<p>(sry, die formatierungen gingen jetzt durch das kopieren kaputt.. mein dummer alter pc ^^)</p>
<p>Wenn ich die Zahl 31 eingebe, dann funktioniert noch alles prima. Aber ab 32 bekomme ich dann ein Minus-Ergebnis und ab der Zahl 34 (oder größer) nur immer das Ergebnis 0 (in Worten: Null).</p>
<p>Warum ist das denn so? Ich denke, dass hat etwas mit &quot;int&quot; zu tun, da &quot;int&quot; nur so und so viel speichern kann, richtig? Aber warum kommt dann auf einmal ein minus-ergebnis raus?</p>
<p>Und &quot;int&quot; kann doch eigentlich gar keine so großen Zahlen speichern - warum kommt nicht schon viel früher immer das Ergebnis 0 raus, sondern erst bei einer ganz großen zahl?</p>
<p>schonmal danke!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1605167</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1605167</guid><dc:creator><![CDATA[dergastuser]]></dc:creator><pubDate>Sun, 26 Oct 2008 17:10:32 GMT</pubDate></item><item><title><![CDATA[Reply to Primzahl geht nur bis Zahl 32? on Sun, 26 Oct 2008 17:14:56 GMT]]></title><description><![CDATA[<p>32 ist nicht wirklich groß...</p>
<pre><code class="language-cpp">for (int i=2; i &lt;= sqrt(x); ++i) {
         if (x % 1 == 0) return false;
         }
         return (x &gt; 1);
         }
</code></pre>
<p>x%1 bringt nicht so viel oder? x%i wäre besser...</p>
<p>Evtl. solltest du auch noch testen ob das Einlesen geklappt hat (if (cin) ), sonst steht in z Mist drin...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1605171</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1605171</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Sun, 26 Oct 2008 17:14:56 GMT</pubDate></item><item><title><![CDATA[Reply to Primzahl geht nur bis Zahl 32? on Sun, 26 Oct 2008 17:21:47 GMT]]></title><description><![CDATA[<p>oh, das ist mir jetzt aber peinlich. Ich habe den falschen Code reinkopiert. Aber hat auch etwas gutes, da ich jetzt den fehler mit %i weiß. Danke!</p>
<p>Hier der richtige Code:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
using namespace std;

int main()
{
   int z;
   int Result = 1;

cout &lt;&lt; &quot;Bitte geben Sie eine ganze Zahl ein, die groesser als 0 ist: &quot; &lt;&lt; endl;
cin &gt;&gt; z;

if (z &lt;= 0) { cout &lt;&lt; &quot;Fehler: Sie haben eine ungueltige Zahl eingegeben&quot; &lt;&lt; endl; system(&quot;PAUSE&quot;); return 0; }

if (z == 0) {Result = 0;}

while( z != 0 )
{
  Result *= z;
  cout &lt;&lt; z;
       if (z != 1) {cout &lt;&lt; &quot;*&quot;;}
  z--;
}

cout &lt;&lt; endl &lt;&lt; endl;
cout &lt;&lt; &quot;Ergebnis: &quot; &lt;&lt; Result &lt;&lt; endl;

  system(&quot;Pause&quot;);
  return 0;

}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1605173</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1605173</guid><dc:creator><![CDATA[dergastuser]]></dc:creator><pubDate>Sun, 26 Oct 2008 17:21:47 GMT</pubDate></item><item><title><![CDATA[Reply to Primzahl geht nur bis Zahl 32? on Sun, 26 Oct 2008 17:23:44 GMT]]></title><description><![CDATA[<p>//edit:<br />
SO habs herausgefunden:</p>
<p>Also bis 12 sind die Ergebnisse korrekt.<br />
Ab 13 werden sie falsch.</p>
<p>Bei 12! kriegt man 479001600 raus.<br />
Das liegt auch noch im Darstellungsbereich.<br />
Bei 13! sind wir schon bei 6 Milliarden....<br />
Und Int(long) geht ja nur −2147483648 (−231) bis +2147483647 (231−1).<br />
Selbst unsigned long int geht nur bis 4 milliarden.<br />
Also überschreitet ab der Eingabe der Zahl 13 die Zahlenlänge des Ergebnisses die 32 Bitlänge der CPU.</p>
<p>STimmt das so wie ich mir das denke?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1605174</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1605174</guid><dc:creator><![CDATA[dergastuser]]></dc:creator><pubDate>Sun, 26 Oct 2008 17:23:44 GMT</pubDate></item><item><title><![CDATA[Reply to Primzahl geht nur bis Zahl 32? on Sun, 26 Oct 2008 20:14:59 GMT]]></title><description><![CDATA[<p>wie oft passiert es eigentlich das das programm an diese stelle springen kann?</p>
<pre><code class="language-cpp">if (z == 0) {Result = 0;}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1605249</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1605249</guid><dc:creator><![CDATA[pkjiuopdt]]></dc:creator><pubDate>Sun, 26 Oct 2008 20:14:59 GMT</pubDate></item><item><title><![CDATA[Reply to Primzahl geht nur bis Zahl 32? on Sun, 26 Oct 2008 21:28:41 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">#include &lt;iostream&gt; 
using namespace std; 

int main() 
{ 
   int z; 
   int Result = 1; 

   cout &lt;&lt; &quot;Bitte geben Sie eine ganze Zahl ein, die groesser als 0 ist: &quot;&lt;&lt;    endl; 
   cin &gt;&gt; z; 

   if (z &lt;= 0) 
   { 
     cout &lt;&lt; &quot;Fehler: Sie haben eine ungueltige Zahl eingegeben&quot; &lt;&lt; endl; 
     system(&quot;PAUSE&quot;); 
     return 0; 
   } 

   if (z == 0) 
   {
      Result = 0;
   } 

   while( z != 0 ) 
   { 
     Result *= z; 
     cout &lt;&lt; z; 
     if (z != 1)
     {
       cout &lt;&lt; &quot;*&quot;;
     } 

    z--; 
   } 

   cout &lt;&lt; endl &lt;&lt; endl; 
   cout &lt;&lt; &quot;Ergebnis: &quot; &lt;&lt; Result &lt;&lt; endl; 

   system(&quot;Pause&quot;); 
   return 0; 

}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1605275</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1605275</guid><dc:creator><![CDATA[Peter4711-0815]]></dc:creator><pubDate>Sun, 26 Oct 2008 21:28:41 GMT</pubDate></item><item><title><![CDATA[Reply to Primzahl geht nur bis Zahl 32? on Mon, 27 Oct 2008 07:01:59 GMT]]></title><description><![CDATA[<p>Also die while-Schleife könnte sollte vielleicht in den Else-Block der vorhergehenden If-Bedingung. Wenn z = 0 ist, dann passiert in der Schleife sowieso nicht mehr viel sinnvolles... (ist zwar schon durch die schleife abgefangen, aber dann ist der Code schneller zu entschlüsseln <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/1605328</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1605328</guid><dc:creator><![CDATA[It0101]]></dc:creator><pubDate>Mon, 27 Oct 2008 07:01:59 GMT</pubDate></item><item><title><![CDATA[Reply to Primzahl geht nur bis Zahl 32? on Mon, 27 Oct 2008 07:53:52 GMT]]></title><description><![CDATA[<p>unsigned long long sollte noch mehr Platz bieten aber bei Fakultäten reicht auch das schnell nicht mehr. Entweder entsprechende Bibliotheken benutzen, selbst was zusammenzimmern oder mit was anderem rechnen. <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/1605348</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1605348</guid><dc:creator><![CDATA[Fellhuhn]]></dc:creator><pubDate>Mon, 27 Oct 2008 07:53:52 GMT</pubDate></item></channel></rss>