<?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[Optimierung eines Primzahlberechners]]></title><description><![CDATA[<p>Hallo, ich habe letztens einen Primzahlenberechnungsalgorithmus geschrieben der so aussieht:</p>
<p>[cpp]<br />
#include&lt;cstdio&gt;<br />
#include&lt;cstdlib&gt;<br />
#include&lt;iostream&gt;<br />
using namespace std;</p>
<p>int main (void)<br />
{<br />
bool marker;<br />
DWORD Anfang = GetTickCount(); //Zur Berechnung der gebrauchten Zeit</p>
<p>for (int i=3; i&lt;300000; i+=2) //Schleife, die alle ungeraden Zahlen<br />
{ //durchgeht<br />
marker = false;<br />
double wurzel = i;<br />
wurzel = sqrt(wurzel);</p>
<p>for (int k=2; k &lt;= wurzel ; k++) //Schleife, die überprüft, ob es sich<br />
{ //um eine Primzahl handelt<br />
if (i % k == 0)<br />
{<br />
marker=true;<br />
break;<br />
}<br />
}</p>
<p>if (marker == false)<br />
{<br />
cout &lt;&lt; i &lt;&lt; &quot;\n&quot;;<br />
}<br />
}<br />
DWORD Ende = GetTickCount();<br />
DWORD Dauer = Ende-Anfang;<br />
cout &lt;&lt; &quot;\n Zeit:&quot; &lt;&lt; Dauer &lt;&lt; &quot;Millisekunden\n&quot; ;<br />
system (&quot;PAUSE&quot;);<br />
}</p>
<p>Ich bin mir allerdings sicher, dass der noch nicht ausoptimiert ist, da lässt sich doch sicher noch Performance rausholen. Wer hätte da einen Vorschlag?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/151992/optimierung-eines-primzahlberechners</link><generator>RSS for Node</generator><lastBuildDate>Sun, 06 Sep 2026 17:03:02 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/151992.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 01 Jul 2006 15:14:05 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Optimierung eines Primzahlberechners on Sat, 01 Jul 2006 15:14:05 GMT]]></title><description><![CDATA[<p>Hallo, ich habe letztens einen Primzahlenberechnungsalgorithmus geschrieben der so aussieht:</p>
<p>[cpp]<br />
#include&lt;cstdio&gt;<br />
#include&lt;cstdlib&gt;<br />
#include&lt;iostream&gt;<br />
using namespace std;</p>
<p>int main (void)<br />
{<br />
bool marker;<br />
DWORD Anfang = GetTickCount(); //Zur Berechnung der gebrauchten Zeit</p>
<p>for (int i=3; i&lt;300000; i+=2) //Schleife, die alle ungeraden Zahlen<br />
{ //durchgeht<br />
marker = false;<br />
double wurzel = i;<br />
wurzel = sqrt(wurzel);</p>
<p>for (int k=2; k &lt;= wurzel ; k++) //Schleife, die überprüft, ob es sich<br />
{ //um eine Primzahl handelt<br />
if (i % k == 0)<br />
{<br />
marker=true;<br />
break;<br />
}<br />
}</p>
<p>if (marker == false)<br />
{<br />
cout &lt;&lt; i &lt;&lt; &quot;\n&quot;;<br />
}<br />
}<br />
DWORD Ende = GetTickCount();<br />
DWORD Dauer = Ende-Anfang;<br />
cout &lt;&lt; &quot;\n Zeit:&quot; &lt;&lt; Dauer &lt;&lt; &quot;Millisekunden\n&quot; ;<br />
system (&quot;PAUSE&quot;);<br />
}</p>
<p>Ich bin mir allerdings sicher, dass der noch nicht ausoptimiert ist, da lässt sich doch sicher noch Performance rausholen. Wer hätte da einen Vorschlag?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1089609</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1089609</guid><dc:creator><![CDATA[ghee*]]></dc:creator><pubDate>Sat, 01 Jul 2006 15:14:05 GMT</pubDate></item><item><title><![CDATA[Reply to Optimierung eines Primzahlberechners on Sat, 01 Jul 2006 15:21:54 GMT]]></title><description><![CDATA[<p><a href="http://www.c-plusplus.net/forum/viewtopic-var-t-is-112808-and-start-is-10.html" rel="nofollow">http://www.c-plusplus.net/forum/viewtopic-var-t-is-112808-and-start-is-10.html</a> :schland::schland::schland::schland::schland::schland::schland::schland::schland::schland:</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1089611</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1089611</guid><dc:creator><![CDATA[:schland::schland::schlan]]></dc:creator><pubDate>Sat, 01 Jul 2006 15:21:54 GMT</pubDate></item><item><title><![CDATA[Reply to Optimierung eines Primzahlberechners on Sat, 01 Jul 2006 15:27:24 GMT]]></title><description><![CDATA[<p>Sorry, hab das [/cpp]-Tag vergessen... hier nochmal der Code:</p>
<pre><code class="language-cpp">#include&lt;cstdio&gt;
#include&lt;cstdlib&gt;
#include&lt;iostream&gt;
using namespace std;

int main (void)
{ 
    bool marker;
    DWORD Anfang = GetTickCount();

    for (unsigned int i=3; i&lt;100; i+=2) 
    { 
        marker = false;
        double wurzel = i;
        wurzel = sqrt(wurzel);

        for (int k=2; k &lt;= wurzel ; k++)
        {
                if (i % k == 0)
                {
                                marker=true; 
                                break;
                }
        }

        if (marker == false)
        {
                cout &lt;&lt; i &lt;&lt; &quot;\n&quot;;
        }
    }
    DWORD Ende = GetTickCount();
    DWORD Dauer = Ende-Anfang;
    cout &lt;&lt; &quot;\n Zeit:&quot; &lt;&lt; Dauer &lt;&lt; &quot;Millisekunden\n&quot; ;
    system (&quot;PAUSE&quot;);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1089614</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1089614</guid><dc:creator><![CDATA[ghee*]]></dc:creator><pubDate>Sat, 01 Jul 2006 15:27:24 GMT</pubDate></item><item><title><![CDATA[Reply to Optimierung eines Primzahlberechners on Sun, 02 Jul 2006 10:02:12 GMT]]></title><description><![CDATA[<p>Du solltest, wenn möglich ein und denselben Datentyp für deine Berechnungen nehmen, z.B. nur 'int'.<br />
Außerdem ist die folgende Anweisung noch optimierbar:</p>
<pre><code class="language-cpp">double wurzel = i;
wurzel = sqrt(wurzel);
</code></pre>
<p>Und du brauchst in der zweiten Schleife auch nur die ungeraden Zahlen prüfen, da 'i' ja niemals durch 2 teilbar ist.</p>
<p>Mein Vorschlag:</p>
<pre><code class="language-cpp">for (int i=3; i&lt;100; i+=2)
    { 
        marker = false;
        int wurzel = (int)sqrt(i);

        for (int k=3; k &lt;= wurzel; k+=2)
        {
                if (i % k == 0)
                {
                                marker=true; 
                                break;
                }
        }

        if (marker == false)
        {
                cout &lt;&lt; i &lt;&lt; &quot;\n&quot;;
        }
    }
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1089986</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1089986</guid><dc:creator><![CDATA[Th]]></dc:creator><pubDate>Sun, 02 Jul 2006 10:02:12 GMT</pubDate></item></channel></rss>