<?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[problem mit array bei primzahlensuche]]></title><description><![CDATA[<p>Hallo zusammen, habe gerade angefangen mir c++ beizubringen und als eines der ersten programme mal diese primzahlensuche (sieb des ereratosthenes) geschrieben:</p>
<pre><code>#include &lt;iostream&gt;

int main () {

    int arr[1400];
    arr[0]=2;
    arr[1]=3;
    int v;
    int i=3;
    int prim =1;

    while(i&lt;10000){
    i++;
    v=0;              
                  while(v&lt;=prim){
                               if (i%arr[v]==0){break; }
                               if(v==prim){
                                           prim++;
                                           arr[prim]=i;
                                           }
                               v++;
                                 }

                 }
    int zz=0;
    while(zz&lt;=prim){
                    std::cout&lt;&lt;arr[zz]&lt;&lt;&quot;  &quot;;
                    zz++;
                    }
    std::cout&lt;&lt;&quot; Primzahlen gefunden:&quot;&lt;&lt;prim ;
    getchar();
    return 0;

}
</code></pre>
<p>das funktioniert soweit auch, nur wenn ich größere primzahlen suchen will (zb bis 20000) dann muss ich das array größer machen und es kommt immer zu einem crash des programms...woran liegt das?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/316788/problem-mit-array-bei-primzahlensuche</link><generator>RSS for Node</generator><lastBuildDate>Wed, 29 Jul 2026 17:16:53 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/316788.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 16 May 2013 17:09:55 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to problem mit array bei primzahlensuche on Thu, 16 May 2013 17:09:55 GMT]]></title><description><![CDATA[<p>Hallo zusammen, habe gerade angefangen mir c++ beizubringen und als eines der ersten programme mal diese primzahlensuche (sieb des ereratosthenes) geschrieben:</p>
<pre><code>#include &lt;iostream&gt;

int main () {

    int arr[1400];
    arr[0]=2;
    arr[1]=3;
    int v;
    int i=3;
    int prim =1;

    while(i&lt;10000){
    i++;
    v=0;              
                  while(v&lt;=prim){
                               if (i%arr[v]==0){break; }
                               if(v==prim){
                                           prim++;
                                           arr[prim]=i;
                                           }
                               v++;
                                 }

                 }
    int zz=0;
    while(zz&lt;=prim){
                    std::cout&lt;&lt;arr[zz]&lt;&lt;&quot;  &quot;;
                    zz++;
                    }
    std::cout&lt;&lt;&quot; Primzahlen gefunden:&quot;&lt;&lt;prim ;
    getchar();
    return 0;

}
</code></pre>
<p>das funktioniert soweit auch, nur wenn ich größere primzahlen suchen will (zb bis 20000) dann muss ich das array größer machen und es kommt immer zu einem crash des programms...woran liegt das?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2323799</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2323799</guid><dc:creator><![CDATA[neuhier123d]]></dc:creator><pubDate>Thu, 16 May 2013 17:09:55 GMT</pubDate></item><item><title><![CDATA[Reply to problem mit array bei primzahlensuche on Thu, 16 May 2013 17:16:00 GMT]]></title><description><![CDATA[<p>Nicht genug Speicher.<br />
Allokiere das Array dynamisch (std::vector), dann ist es auf dem Heap, dort ist in der Regel mehr Platz (beim Computer).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2323800</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2323800</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Thu, 16 May 2013 17:16:00 GMT</pubDate></item><item><title><![CDATA[Reply to problem mit array bei primzahlensuche on Thu, 16 May 2013 18:17:36 GMT]]></title><description><![CDATA[<p>Das ist gar nicht das Sieb des Eratosthenes. Da hast du wohl etwas Missverstanden.</p>
<p>Nimm std::vector statt Arrays. Das macht das ganze viel einfacher.</p>
<p>Um den Code lesbarer zu kriegen, kannst du den Primzahltest auch in einer Funktion auslagern. Z.B. so:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;vector&gt;

bool gemeinsamer_teiler(std::vector&lt;long&gt; const&amp; pri, long kan)
{
    ...
}

int main()
{
    std::vector&lt;long&gt; primzahlen = {2,3};
    for (long kandidat=5; kandidat&lt;=20000; ++kandidat) {
        if (!gemeinsamer_teiler(primzahlen,kandidat)) {
            primzahlen.push_back(kandidat);
        }
    }
    for (long p : primzahlen) {
        std::cout &lt;&lt; p &lt;&lt; '\n';
    }
}
</code></pre>
<p>Das <code>const&amp;</code> da macht aus <code>pri</code> eine Referenz, die sich wegen des Aufrufs in main auf <code>primzahlen</code> bezieht, wobei aber nur Lesezugriff gewährt wird. Dadurch verhindert man eine unnötige Kopie, was bei Vektoren ganz praktisch ist, weil das Kopieren von diesen Dingern potentiell teuer ist.</p>
<p>Ich hoffe, Du hast dir ein schlaues Buch besorgt. Woher hast du die Verwendung von rohen Arrays ( <code>int[]</code> )?</p>
<p>Viel Erfolg!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2323820</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2323820</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Thu, 16 May 2013 18:17:36 GMT</pubDate></item><item><title><![CDATA[Reply to problem mit array bei primzahlensuche on Thu, 16 May 2013 19:24:58 GMT]]></title><description><![CDATA[<p>ok super, vielen dank für die vorschläge, das mit einem starren array zu machen ist wohl nicht wirklich der eleganteste weg...habe diese array verwendung nicht aus einem buch, konnte mich aber erinnern so ein programm vor vielen jahren mal in basic geschrieben zu haben und ich habe es dann einfach in c++ &quot;übersetzt&quot;. trotzdem verstehe ich noch nicht ganz das problem mit dem array, in zeiten von ram im gb bereich müsste ich doch ein array mit ein paar 1000 ints resevieren können</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2323837</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2323837</guid><dc:creator><![CDATA[neuhier123d]]></dc:creator><pubDate>Thu, 16 May 2013 19:24:58 GMT</pubDate></item><item><title><![CDATA[Reply to problem mit array bei primzahlensuche on Thu, 16 May 2013 19:28:49 GMT]]></title><description><![CDATA[<p>Jein.<br />
Es gibt verschiedene Arten von Speicher (unter den meisten Computersystemen, ist aber nicht im Standard festgelegt).<br />
Dadrunter ist der Stack und der Heap. Im Stack landen alle Variablen, die du normal in Funktionen deklarierst. Im Heap ist der Speicher, der via new angefordert wurde. In der Regel ist der Heap wesentlich größer als der Stack. In den Stack deines Rechners passt offenbar nicht dieses Array. Wenn du es aber dynamisch allokierst (mit new[], empfehle aber std::vector, der macht das automatisch und löscht das auch für dich), klappt es.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2323840</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2323840</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Thu, 16 May 2013 19:28:49 GMT</pubDate></item><item><title><![CDATA[Reply to problem mit array bei primzahlensuche on Thu, 16 May 2013 19:33:21 GMT]]></title><description><![CDATA[<p>Nathan, der Stack ist kein physisches Ding im Rechner. Der Compiler erzeugt bloß Code, der sich ein Stückchen des Gesamtspeichers für die automatischen Variablen reserviert und den Rest als dynamischen Speicher frei lässt. Die Stackgröße ist daher bloß eine Zahl in deiner Executable.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2323842</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2323842</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Thu, 16 May 2013 19:33:21 GMT</pubDate></item><item><title><![CDATA[Reply to problem mit array bei primzahlensuche on Thu, 16 May 2013 19:40:17 GMT]]></title><description><![CDATA[<p>Das ist mir klar. Kam das so rüber?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2323846</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2323846</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Thu, 16 May 2013 19:40:17 GMT</pubDate></item><item><title><![CDATA[Reply to problem mit array bei primzahlensuche on Thu, 16 May 2013 19:43:12 GMT]]></title><description><![CDATA[<p>ok d.h. ich könnte theoretisch die compilereinstellungen ändern um mehr platz für das array zu bekommen? (schon klar das sollte ich nicht machen, nur zum verständnis)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2323849</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2323849</guid><dc:creator><![CDATA[neuhier123d]]></dc:creator><pubDate>Thu, 16 May 2013 19:43:12 GMT</pubDate></item><item><title><![CDATA[Reply to problem mit array bei primzahlensuche on Thu, 16 May 2013 19:47:49 GMT]]></title><description><![CDATA[<p>SeppJ schrieb:</p>
<blockquote>
<p>Der <strong>Compiler</strong> erzeugt bloß Code, der sich ein Stückchen des Gesamtspeichers für die automatischen Variablen reserviert und den Rest als dynamischen Speicher frei lässt. Die Stackgröße ist daher bloß eine Zahl in deiner Executable.</p>
</blockquote>
<p>Nein, das Stacklimit ist i.A. Sache des Betriebssystems.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2323851</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2323851</guid><dc:creator><![CDATA[nopemrmod]]></dc:creator><pubDate>Thu, 16 May 2013 19:47:49 GMT</pubDate></item><item><title><![CDATA[Reply to problem mit array bei primzahlensuche on Thu, 12 Jun 2014 00:53:03 GMT]]></title><description><![CDATA[<p>...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2323856</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2323856</guid><dc:creator><![CDATA[Swordfish]]></dc:creator><pubDate>Thu, 12 Jun 2014 00:53:03 GMT</pubDate></item><item><title><![CDATA[Reply to problem mit array bei primzahlensuche on Thu, 16 May 2013 19:53:46 GMT]]></title><description><![CDATA[<p>neuhier123d schrieb:</p>
<blockquote>
<p>ok d.h. ich könnte theoretisch die compilereinstellungen ändern um mehr platz für das array zu bekommen? (schon klar das sollte ich nicht machen, nur zum verständnis)</p>
</blockquote>
<p>Jain. Siehe unten.</p>
<p>nopemrmod schrieb:</p>
<blockquote>
<p>SeppJ schrieb:</p>
<blockquote>
<p>Der <strong>Compiler</strong> erzeugt bloß Code, der sich ein Stückchen des Gesamtspeichers für die automatischen Variablen reserviert und den Rest als dynamischen Speicher frei lässt. Die Stackgröße ist daher bloß eine Zahl in deiner Executable.</p>
</blockquote>
<p>Nein, das Stacklimit ist i.A. Sache des Betriebssystems.</p>
</blockquote>
<p>Jain. Kommt auf das System an. Es gibt da so ein recht verbreitetes System, bei dem das Sache der Executable ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2323857</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2323857</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Thu, 16 May 2013 19:53:46 GMT</pubDate></item><item><title><![CDATA[Reply to problem mit array bei primzahlensuche on Thu, 16 May 2013 19:58:41 GMT]]></title><description><![CDATA[<p>Darum auch &quot;i.A.&quot;.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2323858</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2323858</guid><dc:creator><![CDATA[nopemrmod]]></dc:creator><pubDate>Thu, 16 May 2013 19:58:41 GMT</pubDate></item><item><title><![CDATA[Reply to problem mit array bei primzahlensuche on Thu, 16 May 2013 20:19:10 GMT]]></title><description><![CDATA[<p>nopemrmod schrieb:</p>
<blockquote>
<p>Darum auch &quot;i.A.&quot;.</p>
</blockquote>
<p>Wenn du mit 10% die Allgemeinheit meinst <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f644.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_rolling_eyes"
      title=":rolling_eyes:"
      alt="🙄"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2323867</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2323867</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Thu, 16 May 2013 20:19:10 GMT</pubDate></item><item><title><![CDATA[Reply to problem mit array bei primzahlensuche on Fri, 17 May 2013 07:00:05 GMT]]></title><description><![CDATA[<p>Hmm.</p>
<p>Was auch schön wäre, wäre eine Kombination mit std::remove_if:</p>
<pre><code>#include &lt;vector&gt;
#include &lt;iostream&gt;
#include &lt;numeric&gt;
#include &lt;iterator&gt;
#include &lt;algorithm&gt;

int main()
{
    using namespace std;

    vector&lt;int&gt; vec(1000); // Platz für Tausend Zahlen

    iota( begin(vec), end(vec), 1 ); // vec mit Zahlen von 1 bis 1000 füllen

    for( unsigned ct = 2; ct &lt; vec.size(); ++ct ) // Alle Teiler in einer Schleife durchgehen.
        vec.erase( remove_if( begin(vec), end(vec), [=](int i){ return i % ct == 0; } ), end(vec) ); /// Alle Elemente aus vector entfernen die sich durch ct teilen lassen.

    copy( begin(vec), end(vec), ostream_iterator&lt;int&gt;(cout, &quot;, &quot;) ); // vec ausgeben (Zahlen durch Komma getrennt
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2323910</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2323910</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Fri, 17 May 2013 07:00:05 GMT</pubDate></item><item><title><![CDATA[Reply to problem mit array bei primzahlensuche on Fri, 17 May 2013 07:02:52 GMT]]></title><description><![CDATA[<p>Sone schrieb:</p>
<blockquote>
<p>Hmm.</p>
<p>Was auch schön wäre, wäre eine Kombination mit std::remove_if:</p>
<pre><code>#include &lt;vector&gt;
#include &lt;iostream&gt;
#include &lt;numeric&gt;
#include &lt;iterator&gt;
#include &lt;algorithm&gt;

int main()
{
    using namespace std;

    vector&lt;int&gt; vec(1000); // Platz für Tausend Zahlen

    iota( begin(vec), end(vec), 1 ); // vec mit Zahlen von 1 bis 1000 füllen

    for( unsigned ct = 2; ct &lt; vec.size(); ++ct ) // Alle Teiler in einer Schleife durchgehen.
        vec.erase( remove_if( begin(vec), end(vec), [=](int i){ return i % ct == 0; } ), end(vec) ); /// Alle Elemente aus vector entfernen die sich durch ct teilen lassen.

    copy( begin(vec), end(vec), ostream_iterator&lt;int&gt;(cout, &quot;, &quot;) ); // vec ausgeben (Zahlen durch Komma getrennt
}
</code></pre>
</blockquote>
<p>Du hast hiermit die einzigen beiden unter 1000 liegenden Primzahlen 420 und 840 entdeckt. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f621.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--pouting_face"
      title=":rage:"
      alt="😡"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2323911</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2323911</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Fri, 17 May 2013 07:02:52 GMT</pubDate></item><item><title><![CDATA[Reply to problem mit array bei primzahlensuche on Fri, 17 May 2013 10:15:10 GMT]]></title><description><![CDATA[<p>neuhier123d schrieb:</p>
<blockquote>
<p>Hallo zusammen, habe gerade angefangen mir c++ beizubringen und als eines der ersten programme mal diese primzahlensuche (sieb des ereratosthenes) geschrieben</p>
</blockquote>
<p>Nein, Du hast das Sieb des E nicht implementiert, sondern Trial Division mit sich gemerkten Primzahlen.<br />
Ungefähr sowas:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;vector&gt;
using namespace std;

bool istTeilerDrin(vector&lt;int&gt;&amp; teiler,int zahl){
    for(auto t:teiler){
                                     //&lt;-
        if(zahl%t==0)
            return true;
    }
    return false;
}

int main ()
{
    vector&lt;int&gt; primzahlen{2};

    for(int kandidat=3;kandidat&lt;10000;kandidat+=2)
        if(!istTeilerDrin(primzahlen,kandidat))
            primzahlen.push_back(kandidat);

    for(auto p:primzahlen)
        cout&lt;&lt;p&lt;&lt;' ';

    cout&lt;&lt;&quot; Primzahlen gefunden:&quot;&lt;&lt;primzahlen.size()&lt;&lt;'\n';
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2323912</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2323912</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Fri, 17 May 2013 10:15:10 GMT</pubDate></item><item><title><![CDATA[Reply to problem mit array bei primzahlensuche on Fri, 17 May 2013 09:19:25 GMT]]></title><description><![CDATA[<p>volkard schrieb:</p>
<blockquote>
<p>Du hast hiermit die einzigen beiden unter 1000 liegenden Primzahlen 420 und 840 entdeckt. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f621.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--pouting_face"
      title=":rage:"
      alt="😡"
    /></p>
</blockquote>
<p>Ach, mein Verfahren ist bullshit. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_down"
      title=":-1:"
      alt="👎"
    /></p>
<p>Bzw. eigentlich auch nicht, s.u. - da war ein Denkfehler.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2323929</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2323929</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Fri, 17 May 2013 09:19:25 GMT</pubDate></item><item><title><![CDATA[Reply to problem mit array bei primzahlensuche on Fri, 17 May 2013 09:05:30 GMT]]></title><description><![CDATA[<p>Hier das Sieb des Eratosthenes:</p>
<pre><code>#include &lt;vector&gt;
#include &lt;iostream&gt;

int main()
{
    using namespace std;

    unsigned const N = 1000;

    vector&lt;pair&lt;int, bool&gt; &gt; vec(N - 1); // Platz. Erster Wert ist die Zahl, der zweite bool-Wert gibt an ob diese Zahl noch eine Primzahl sein könnte.

    for( unsigned ct = 0; ct &lt; vec.size(); ++ct )
        vec[ct] = make_pair(ct + 2, true);

    for( unsigned ct = 0; ct &lt; vec.size(); ++ct )
        if( vec[ct].second )
        {
            int value = vec[ct].first;

            for( unsigned multiple = value*value; multiple &lt; vec.size() + 2; multiple += value ) /// Durch alle Vielfachen gehen und diese als Nicht-Prim markieren
                vec[multiple - 2].second = false;
        }

    /// Ausgabe:
    for( unsigned ct = 0; ct &lt; vec.size(); ++ct )
        if( vec[ct].second )
            cout &lt;&lt; vec[ct].first &lt;&lt; &quot;, &quot;;
}
</code></pre>
<p>Ideone: <a href="http://ideone.com/JnCKGc" rel="nofollow">http://ideone.com/JnCKGc</a></p>
<p>Ist jetzt ganz sicher richtig.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2323935</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2323935</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Fri, 17 May 2013 09:05:30 GMT</pubDate></item><item><title><![CDATA[Reply to problem mit array bei primzahlensuche on Fri, 17 May 2013 09:18:40 GMT]]></title><description><![CDATA[<p>Meine vorherige Version ist an sich auch möglich, muss aber angepasst werden:</p>
<pre><code>#include &lt;vector&gt;
#include &lt;iostream&gt;
#include &lt;numeric&gt;
#include &lt;iterator&gt;
#include &lt;algorithm&gt;

int main()
{
    using namespace std;

    vector&lt;int&gt; vec(1000); // Platz für Tausend Zahlen

    iota( begin(vec), end(vec), 2 ); // vec mit Zahlen von 2 bis 1001 füllen

    for( unsigned ct = 2; ct &lt; vec.size() - 2; ++ct ) // Alle Teiler in einer Schleife durchgehen.
        vec.erase( remove_if( begin(vec) + ct + 1, end(vec), [=](int i){ return i % ct == 0; } ), end(vec) ); /// Alle Elemente aus vector entfernen die sich durch ct teilen lassen. 
/// Im Gegenteil zu der ersten, nicht funktionierenden Methode wird hier nicht bei begin(vec) sondern bei begin(vec) + ct + 1 angefangen, wodurch der Fehler dass der Modul selbst entfernt wird, nicht  mehr auftritt.

    copy( begin(vec), end(vec), ostream_iterator&lt;int&gt;(cout, &quot;, &quot;) ); // vec ausgeben (Zahlen durch Komma getrennt
}
</code></pre>
<p>Ideone: <a href="http://ideone.com/wisLEG" rel="nofollow">http://ideone.com/wisLEG</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2323937</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2323937</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Fri, 17 May 2013 09:18:40 GMT</pubDate></item><item><title><![CDATA[Reply to problem mit array bei primzahlensuche on Fri, 17 May 2013 09:56:12 GMT]]></title><description><![CDATA[<p>Sone schrieb:</p>
<blockquote>
<p>Meine vorherige Version ist an sich auch möglich, muss aber angepasst werden:</p>
<pre><code>#include &lt;vector&gt;
#include &lt;iostream&gt;
#include &lt;numeric&gt;
#include &lt;iterator&gt;
#include &lt;algorithm&gt;

int main()
{
    using namespace std;

    vector&lt;int&gt; vec(1000); // Platz für Tausend Zahlen

    iota( begin(vec), end(vec), 2 ); // vec mit Zahlen von 2 bis 1001 füllen

    for( unsigned ct = 2; ct &lt; vec.size() - 2; ++ct ) // Alle Teiler in einer Schleife durchgehen.
        vec.erase( remove_if( begin(vec) + ct + 1, end(vec), [=](int i){ return i % ct == 0; } ), end(vec) ); /// Alle Elemente aus vector entfernen die sich durch ct teilen lassen. 
/// Im Gegenteil zu der ersten, nicht funktionierenden Methode wird hier nicht bei begin(vec) sondern bei begin(vec) + ct + 1 angefangen, wodurch der Fehler dass der Modul selbst entfernt wird, nicht  mehr auftritt.

    copy( begin(vec), end(vec), ostream_iterator&lt;int&gt;(cout, &quot;, &quot;) ); // vec ausgeben (Zahlen durch Komma getrennt
}
</code></pre>
<p>Ideone: <a href="http://ideone.com/wisLEG" rel="nofollow">http://ideone.com/wisLEG</a></p>
</blockquote>
<p>Trotzdem bist Du wieder auf der Insel des Bullshits.</p>
<p>Ich hab mein Verfahren gerade mal geändert, daß es nicht alle Primzahlen bis Obergrenze und dann die Anzahl, sondern nur die Anzahl ausgibt.</p>
<p>Für Obergrenze 1000000 braucht es 12.4s.<br />
Mhhm. Das müßte man beschleunigen.</p>
<p>Oh, muss nur</p>
<pre><code>if(t*t&gt;zahl)
            break;
</code></pre>
<p>reinschreiben und bin bei 0.05s. Bei Dir sehe ich gar nicht, wo ich es reinschreiben sollte.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2323951</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2323951</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Fri, 17 May 2013 09:56:12 GMT</pubDate></item><item><title><![CDATA[Reply to problem mit array bei primzahlensuche on Fri, 17 May 2013 10:32:17 GMT]]></title><description><![CDATA[<p>Sone schrieb:</p>
<blockquote>
<p>Hier das Sieb des Eratosthenes:</p>
<pre><code>#include &lt;vector&gt;
#include &lt;iostream&gt;

int main()
{
    using namespace std;

    unsigned const N = 1000;

    vector&lt;pair&lt;int, bool&gt; &gt; vec(N - 1); // Platz. Erster Wert ist die Zahl, der zweite bool-Wert gibt an ob diese Zahl noch eine Primzahl sein könnte.

    for( unsigned ct = 0; ct &lt; vec.size(); ++ct )
        vec[ct] = make_pair(ct + 2, true);

    for( unsigned ct = 0; ct &lt; vec.size(); ++ct )
        if( vec[ct].second )
        {
            int value = vec[ct].first;

            for( unsigned multiple = value*value; multiple &lt; vec.size() + 2; multiple += value ) /// Durch alle Vielfachen gehen und diese als Nicht-Prim markieren
                vec[multiple - 2].second = false;
        }

    /// Ausgabe:
    for( unsigned ct = 0; ct &lt; vec.size(); ++ct )
        if( vec[ct].second )
            cout &lt;&lt; vec[ct].first &lt;&lt; &quot;, &quot;;
}
</code></pre>
<p>Ideone: <a href="http://ideone.com/JnCKGc" rel="nofollow">http://ideone.com/JnCKGc</a></p>
<p>Ist jetzt ganz sicher richtig.</p>
</blockquote>
<p>Ich lach mich tot.</p>
<pre><code>vector&lt;bool&gt;
</code></pre>
<p>ist fast so gut wie</p>
<pre><code>map&lt;int,bool&gt;
</code></pre>
<p>oder Dein</p>
<pre><code>vector&lt;pair&lt;int, bool&gt;&gt;
</code></pre>
<p>Schau:</p>
<pre><code>#include &lt;vector&gt;
#include &lt;iostream&gt;

#define first UNUSED

int main()
{
    using namespace std;

    unsigned const N = 1000;

    vector&lt;pair&lt;int, bool&gt; &gt; vec(N - 1); // Platz. Erster Wert ist die Zahl, der zweite bool-Wert gibt an ob diese Zahl noch eine Primzahl sein könnte.

    for( unsigned ct = 0; ct &lt; vec.size(); ++ct )
        vec[ct] = make_pair(ct + 2, true);//Dieses +2 ist dran schuld!

    for( unsigned ct = 0; ct &lt; vec.size(); ++ct )
        if( vec[ct].second )
        {
//            int value = vec[ct].first;
            int value = ct+2;

            for( unsigned multiple = value*value; multiple &lt; vec.size() + 2; multiple += value )
                vec[multiple - 2].second = false;
        }

    /// Ausgabe:
    for( unsigned ct = 0; ct &lt; vec.size(); ++ct )
        if( vec[ct].second )
//            cout &lt;&lt; vec[ct].first &lt;&lt; &quot;, &quot;;
            cout &lt;&lt; ct+2 &lt;&lt; &quot;, &quot;;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2323959</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2323959</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Fri, 17 May 2013 10:32:17 GMT</pubDate></item><item><title><![CDATA[Reply to problem mit array bei primzahlensuche on Thu, 12 Jun 2014 10:12:58 GMT]]></title><description><![CDATA[<p>...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2323963</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2323963</guid><dc:creator><![CDATA[Swordfish]]></dc:creator><pubDate>Thu, 12 Jun 2014 10:12:58 GMT</pubDate></item><item><title><![CDATA[Reply to problem mit array bei primzahlensuche on Fri, 17 May 2013 10:31:55 GMT]]></title><description><![CDATA[<p>ok super, da habe ich noch einiges auszuprobieren und nachzulesen... hab mal überlegt, meine methode sollte O(N^2/ln(N)) sein, das Sieb von E ist ja O(N), hab bei Wikipedia noch das Sieb von Aktin gefunden,das anscheinend O(N/(ln(ln(N))) ist, was ja auch nicht viel besser als O(N) ist^^ gibt es noch einen besseren algorithmus zur primzahlenbestimmung der schneller ist?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2323965</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2323965</guid><dc:creator><![CDATA[neuhier123d]]></dc:creator><pubDate>Fri, 17 May 2013 10:31:55 GMT</pubDate></item><item><title><![CDATA[Reply to problem mit array bei primzahlensuche on Fri, 17 May 2013 10:39:09 GMT]]></title><description><![CDATA[<p>neuhier123d schrieb:</p>
<blockquote>
<p>ok super, da habe ich noch einiges auszuprobieren und nachzulesen... hab mal überlegt, meine methode sollte O(N^2/ln(N)) sein, das Sieb von E ist ja O(N), hab bei Wikipedia noch das Sieb von Aktin gefunden,das anscheinend O(N/(ln(ln(N))) ist, was ja auch nicht viel besser als O(N) ist^^ gibt es noch einen besseren algorithmus zur primzahlenbestimmung der schneller ist?</p>
</blockquote>
<p>Eigentlich macht das Sieb des E alle anderen Platt.<br />
<a href="http://code.google.com/p/primesieve/" rel="nofollow">http://code.google.com/p/primesieve/</a><br />
Es ist auch recht leicht, es pfiffig zu implementieren.</p>
<p>Mir ist unbekannt, ab wo das Sieb des Atkin schneller wird, aber wohl schon erst bei recht großen Zahlen. Ups, kriegt man es auch so einfach cache-friendly?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2323967</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2323967</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Fri, 17 May 2013 10:39:09 GMT</pubDate></item></channel></rss>