<?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[Kleiner fermatscher Satz]]></title><description><![CDATA[<p>Hallo Zusammen,</p>
<p>ich beschäftige mich seit kurzem ein wenig mit C++ (in der Schule hatte ich mehr mit C zu tun). Heute habe ich mal ein kleines Programm geschrieben das mit Fermats kleinem Satz(<a href="http://de.wikipedia.org/wiki/Kleiner_fermatscher_Satz" rel="nofollow">http://de.wikipedia.org/wiki/Kleiner_fermatscher_Satz</a>) prüft, ob eine Zahl eine Primzahl ist. Natürlich lässt sich darüber streiten ob man soetwas überhaupt in C++ schreiben muss, war aber wie gesagt nur zur Übung. Mich würde jetzt interessieren, was ich generell anders machen sollte / müsste oder was kompletter Blödsinn ist. Da ich mich eigentlich für ziemlich kritikfähig halte, könnt ihr ruhig eure ganze Wut an mir auslassen ;). In dem Programm verwende ich eine BigInt Klasse von sourceforge,bei der ich eine kleine Methode(binary_value() -&gt;Zahl binär darstellen) eingebaut habe. Wenn sich einer für alle Dateien interessiert, dann kann er die hier herunterladen: <a href="http://www.turboupload.com/files/get/5qM8JrISN2/prim.zip" rel="nofollow">http://www.turboupload.com/files/get/5qM8JrISN2/prim.zip</a></p>
<p>Ansonsten:<br />
<em><strong>fermat.h:</strong></em></p>
<pre><code class="language-cpp">#include &quot;bigint.h&quot;

//If TestedNumber is a prime number then
//base^(TestedNumber-1) = 1 (mod TestedNumber)
// for every base &lt; TestedNumber
class FermatTest {
	private:
		RossiBigInt* TestedNumber;
		RossiBigInt* Base;

	public:
		FermatTest(void);
		FermatTest(const string&amp;, const string&amp;);
		FermatTest(const string&amp;);
		bool IsPrimeNumber(void);
};
</code></pre>
<p><em><strong>fermat.cpp:</strong></em></p>
<pre><code class="language-cpp">#include &quot;fermat.h&quot;

//Default Constructor
FermatTest::FermatTest(void) {
	Base = new RossiBigInt(&quot;2&quot;, DEC_DIGIT);
	TestedNumber = new RossiBigInt(&quot;2&quot;, DEC_DIGIT);
}
//------------------------------------------------------------------------------

//Parameter List Constructor
//Parameter: The Number that is tested as string
FermatTest::FermatTest(const string &amp;testnumber) {
	Base = new RossiBigInt(&quot;2&quot;, DEC_DIGIT);
	TestedNumber = new RossiBigInt(testnumber, DEC_DIGIT);
}
//------------------------------------------------------------------------------

//Parameter List Constructor
//Parameters: testnumber: The Number that is tested as string
//            base: the base that shall be used for the calculation
FermatTest::FermatTest(const string&amp; testnumber, const string&amp; base) {
	RossiBigInt ZERO(&quot;0&quot;, DEC_DIGIT), TWO(&quot;2&quot;, DEC_DIGIT);
	Base = new RossiBigInt(base, DEC_DIGIT);
	TestedNumber = new RossiBigInt(testnumber, DEC_DIGIT);
	if(*Base &gt;= *TestedNumber || *Base &lt;= ZERO) //The base has to be smaller than the tested Number and &gt; 0
		*Base = TWO;;
}
//------------------------------------------------------------------------------

//Fermats primality test
//The test is not always correct because there are some pseudoprimes like 341 (11 * 31)
//where Fermats primility test says that they are prime.
//To make the method run faster the test uses iterated squaring.
//Complexity: log2(TestedNumber) loops
bool FermatTest::IsPrimeNumber(void) {
	RossiBigInt ZERO(&quot;0&quot;, DEC_DIGIT), ONE(&quot;1&quot;, DEC_DIGIT), TWO = ONE + ONE, exp = *TestedNumber, addi = ONE, itsquare = *Base;
	if(*TestedNumber == TWO) return true; //2 is a Prime Number
	if(*TestedNumber == ZERO || *TestedNumber == ONE) return false; //by definition 0 and 1 are not prime numbers
	string binary = (--exp).binary_value();
	for(long i=binary.size()-1;i&gt;=0;i--) {
		if(binary[i] == '1')
			addi = (addi * itsquare) % *TestedNumber;
		itsquare = (itsquare * itsquare) % *TestedNumber;
	}
	return (addi == ONE ? true : false);
}
//------------------------------------------------------------------------------
</code></pre>
<p>Achso Nochwas: Die englischen Kommentare bitte nicht auf Grammatik und Rechtschreibung prüfen... <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/topic/235362/kleiner-fermatscher-satz</link><generator>RSS for Node</generator><lastBuildDate>Thu, 24 Sep 2026 05:42:22 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/235362.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 28 Feb 2009 22:23:20 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Kleiner fermatscher Satz on Sat, 28 Feb 2009 22:23:20 GMT]]></title><description><![CDATA[<p>Hallo Zusammen,</p>
<p>ich beschäftige mich seit kurzem ein wenig mit C++ (in der Schule hatte ich mehr mit C zu tun). Heute habe ich mal ein kleines Programm geschrieben das mit Fermats kleinem Satz(<a href="http://de.wikipedia.org/wiki/Kleiner_fermatscher_Satz" rel="nofollow">http://de.wikipedia.org/wiki/Kleiner_fermatscher_Satz</a>) prüft, ob eine Zahl eine Primzahl ist. Natürlich lässt sich darüber streiten ob man soetwas überhaupt in C++ schreiben muss, war aber wie gesagt nur zur Übung. Mich würde jetzt interessieren, was ich generell anders machen sollte / müsste oder was kompletter Blödsinn ist. Da ich mich eigentlich für ziemlich kritikfähig halte, könnt ihr ruhig eure ganze Wut an mir auslassen ;). In dem Programm verwende ich eine BigInt Klasse von sourceforge,bei der ich eine kleine Methode(binary_value() -&gt;Zahl binär darstellen) eingebaut habe. Wenn sich einer für alle Dateien interessiert, dann kann er die hier herunterladen: <a href="http://www.turboupload.com/files/get/5qM8JrISN2/prim.zip" rel="nofollow">http://www.turboupload.com/files/get/5qM8JrISN2/prim.zip</a></p>
<p>Ansonsten:<br />
<em><strong>fermat.h:</strong></em></p>
<pre><code class="language-cpp">#include &quot;bigint.h&quot;

//If TestedNumber is a prime number then
//base^(TestedNumber-1) = 1 (mod TestedNumber)
// for every base &lt; TestedNumber
class FermatTest {
	private:
		RossiBigInt* TestedNumber;
		RossiBigInt* Base;

	public:
		FermatTest(void);
		FermatTest(const string&amp;, const string&amp;);
		FermatTest(const string&amp;);
		bool IsPrimeNumber(void);
};
</code></pre>
<p><em><strong>fermat.cpp:</strong></em></p>
<pre><code class="language-cpp">#include &quot;fermat.h&quot;

//Default Constructor
FermatTest::FermatTest(void) {
	Base = new RossiBigInt(&quot;2&quot;, DEC_DIGIT);
	TestedNumber = new RossiBigInt(&quot;2&quot;, DEC_DIGIT);
}
//------------------------------------------------------------------------------

//Parameter List Constructor
//Parameter: The Number that is tested as string
FermatTest::FermatTest(const string &amp;testnumber) {
	Base = new RossiBigInt(&quot;2&quot;, DEC_DIGIT);
	TestedNumber = new RossiBigInt(testnumber, DEC_DIGIT);
}
//------------------------------------------------------------------------------

//Parameter List Constructor
//Parameters: testnumber: The Number that is tested as string
//            base: the base that shall be used for the calculation
FermatTest::FermatTest(const string&amp; testnumber, const string&amp; base) {
	RossiBigInt ZERO(&quot;0&quot;, DEC_DIGIT), TWO(&quot;2&quot;, DEC_DIGIT);
	Base = new RossiBigInt(base, DEC_DIGIT);
	TestedNumber = new RossiBigInt(testnumber, DEC_DIGIT);
	if(*Base &gt;= *TestedNumber || *Base &lt;= ZERO) //The base has to be smaller than the tested Number and &gt; 0
		*Base = TWO;;
}
//------------------------------------------------------------------------------

//Fermats primality test
//The test is not always correct because there are some pseudoprimes like 341 (11 * 31)
//where Fermats primility test says that they are prime.
//To make the method run faster the test uses iterated squaring.
//Complexity: log2(TestedNumber) loops
bool FermatTest::IsPrimeNumber(void) {
	RossiBigInt ZERO(&quot;0&quot;, DEC_DIGIT), ONE(&quot;1&quot;, DEC_DIGIT), TWO = ONE + ONE, exp = *TestedNumber, addi = ONE, itsquare = *Base;
	if(*TestedNumber == TWO) return true; //2 is a Prime Number
	if(*TestedNumber == ZERO || *TestedNumber == ONE) return false; //by definition 0 and 1 are not prime numbers
	string binary = (--exp).binary_value();
	for(long i=binary.size()-1;i&gt;=0;i--) {
		if(binary[i] == '1')
			addi = (addi * itsquare) % *TestedNumber;
		itsquare = (itsquare * itsquare) % *TestedNumber;
	}
	return (addi == ONE ? true : false);
}
//------------------------------------------------------------------------------
</code></pre>
<p>Achso Nochwas: Die englischen Kommentare bitte nicht auf Grammatik und Rechtschreibung prüfen... <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1672196</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1672196</guid><dc:creator><![CDATA[DerFrosch]]></dc:creator><pubDate>Sat, 28 Feb 2009 22:23:20 GMT</pubDate></item><item><title><![CDATA[Reply to Kleiner fermatscher Satz on Sat, 28 Feb 2009 22:29:15 GMT]]></title><description><![CDATA[<p>FermatTest sollte schlicht eine funktion sein und gar keine klasse.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1672201</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1672201</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Sat, 28 Feb 2009 22:29:15 GMT</pubDate></item><item><title><![CDATA[Reply to Kleiner fermatscher Satz on Sat, 28 Feb 2009 23:10:18 GMT]]></title><description><![CDATA[<p>volkard schrieb:</p>
<blockquote>
<p>FermatTest sollte schlicht eine funktion sein und gar keine klasse.</p>
</blockquote>
<p>Ja, stimmt eigentlich, war ja auch mehr dazu gedacht um mich mit den Konstrukten in C++ vertraut zu machen...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1672216</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1672216</guid><dc:creator><![CDATA[DerFrosch]]></dc:creator><pubDate>Sat, 28 Feb 2009 23:10:18 GMT</pubDate></item><item><title><![CDATA[Reply to Kleiner fermatscher Satz on Sun, 01 Mar 2009 13:44:56 GMT]]></title><description><![CDATA[<p>Ein paar Sachen:</p>
<p>1. Wie volkard schon sagte, eine Klasse ist hier eigentlich nicht sinnvoll. Und du willst schließlich lernen, wie man ein Problem &quot;richtig&quot; in C++ angeht, oder?</p>
<p>2. Zu jedem new gehört immer auch ein delete. Du hast Speicherlecks, weil deine auf dem Heap allozierten RossiBigInts niemals freigegeben werden.</p>
<p>3. In C++ schreibt man nicht foo(void), sondern einfach foo().</p>
<p>4. Dein Default-Konstruktor kommt mir eher überflüssig vor (Punkt 1 einmal außer Acht gelassen).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1672372</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1672372</guid><dc:creator><![CDATA[dooooomi]]></dc:creator><pubDate>Sun, 01 Mar 2009 13:44:56 GMT</pubDate></item><item><title><![CDATA[Reply to Kleiner fermatscher Satz on Sun, 01 Mar 2009 15:01:19 GMT]]></title><description><![CDATA[<p>dooooomi schrieb:</p>
<blockquote>
<p>3. In C++ schreibt man nicht foo(void), sondern einfach foo().</p>
</blockquote>
<p>Nö.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1672409</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1672409</guid><dc:creator><![CDATA[audacia]]></dc:creator><pubDate>Sun, 01 Mar 2009 15:01:19 GMT</pubDate></item><item><title><![CDATA[Reply to Kleiner fermatscher Satz on Sun, 01 Mar 2009 16:20:33 GMT]]></title><description><![CDATA[<p>dooooomi schrieb:</p>
<blockquote>
<p>Ein paar Sachen:</p>
<p>1. Wie volkard schon sagte, eine Klasse ist hier eigentlich nicht sinnvoll. Und du willst schließlich lernen, wie man ein Problem &quot;richtig&quot; in C++ angeht, oder?</p>
</blockquote>
<p>Gut, dann ändere ich das mal ab...</p>
<p>dooooomi schrieb:</p>
<blockquote>
<p>2. Zu jedem new gehört immer auch ein delete. Du hast Speicherlecks, weil deine auf dem Heap allozierten RossiBigInts niemals freigegeben werden.</p>
</blockquote>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f921.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--clown_face"
      title=":clown:"
      alt="🤡"
    /> Oh, daran habe ich ja überhaupt nich gedacht...</p>
<p>dooooomi schrieb:</p>
<blockquote>
<p>3. In C++ schreibt man nicht foo(void), sondern einfach foo().</p>
</blockquote>
<p>Das werde ich dann auch mal ändern...</p>
<p>dooooomi schrieb:</p>
<blockquote>
<p>4. Dein Default-Konstruktor kommt mir eher überflüssig vor (Punkt 1 einmal außer Acht gelassen).</p>
</blockquote>
<p>Der wird dann auch verschwinden...</p>
<p>Danke für die Tipps!!! <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1672445</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1672445</guid><dc:creator><![CDATA[DerFrosch]]></dc:creator><pubDate>Sun, 01 Mar 2009 16:20:33 GMT</pubDate></item><item><title><![CDATA[Reply to Kleiner fermatscher Satz on Sun, 01 Mar 2009 17:36:15 GMT]]></title><description><![CDATA[<p>Wenn ich das richtig verstanden habe, dann soll das etwa so aussehen:</p>
<pre><code class="language-cpp">#include &quot;bigint.h&quot;

//If TestedNumber is a prime number then
//base^(TestedNumber-1) == 1 (mod TestedNumber)
// for every base &lt; TestedNumber

//Fermats primality test
//The test is not always correct because there are some pseudoprimes like 341 (11 * 31)
//where Fermats primility test says that they are prime.
//To make the method run faster the test uses iterated squaring.
//Complexity: log2(TestedNumber) loops
bool IsPrimeNumber(const string&amp;, const string&amp;);

bool IsPrimeNumber(const string &amp;testnum, const string &amp;base) {
	RossiBigInt TestedNumber(testnum, DEC_DIGIT), Base(base, DEC_DIGIT);
	RossiBigInt ZERO(&quot;0&quot;, DEC_DIGIT), ONE(&quot;1&quot;, DEC_DIGIT), TWO = ONE + ONE, exp = TestedNumber, addi = ONE, itsquare = Base;
	if(TestedNumber == TWO) return true; //2 is a Prime Number
	if(TestedNumber == ZERO || TestedNumber == ONE) return false; //by definition 0 and 1 are not prime numbers
	string binary = (--exp).binary_value();
	for(long i=binary.size()-1;i&gt;=0;i--) {
		if(binary[i] == '1')
			addi = (addi * itsquare) % TestedNumber;
		itsquare = (itsquare * itsquare) % TestedNumber;
	}
	return (addi == ONE ? true : false);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1672472</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1672472</guid><dc:creator><![CDATA[DerFrosch]]></dc:creator><pubDate>Sun, 01 Mar 2009 17:36:15 GMT</pubDate></item><item><title><![CDATA[Reply to Kleiner fermatscher Satz on Sun, 01 Mar 2009 17:58:39 GMT]]></title><description><![CDATA[<p>DerFrosch schrieb:</p>
<blockquote>
<p>Wenn ich das richtig verstanden habe, dann soll das etwa so aussehen:</p>
</blockquote>
<p>fein.</p>
<pre><code class="language-cpp">return (addi == ONE ? true : false);
</code></pre>
<p>ist nur</p>
<pre><code class="language-cpp">return addi == ONE;
</code></pre>
<p>fein, daß du kein new mehr hast.</p>
<pre><code class="language-cpp">bool IsPrimeNumber(const string&amp;, const string&amp;);
</code></pre>
<p>der protityp sollte auch variablennamen haben. macht das lesen dann viel toller.<br />
manche (ich auch) meinen, daß string const&amp; viel hübscher als const string&amp; ist.</p>
<pre><code class="language-cpp">bool IsPrimeNumber(const string &amp;testnum, const string &amp;base) {
	RossiBigInt TestedNumber(testnum, DEC_DIGIT), Base(base, DEC_DIGIT);
	RossiBigInt ZERO(&quot;0&quot;, DEC_DIGIT), ONE(&quot;1&quot;, DEC_DIGIT), TWO = ONE + ONE, exp = TestedNumber, addi = ONE, itsquare = Base;
	if(TestedNumber == TWO) return true; //2 is a Prime Number
</code></pre>
<p>der test könnte auch als erste zeile sein. und alle variablen so spät wie möglich anlegen. nicht einfach alle zu beginn der funktion auf vorrat.</p>
<pre><code class="language-cpp">string binary = (--exp).binary_value();
</code></pre>
<p>sollten zwei zeilen sein. weil zwei dinge geschehen.</p>
<pre><code class="language-cpp">for(long i=binary.size()-1;i&gt;=0;i--) {
</code></pre>
<p>long? nee, da gabs nen size-typ im string.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1672475</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1672475</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Sun, 01 Mar 2009 17:58:39 GMT</pubDate></item><item><title><![CDATA[Reply to Kleiner fermatscher Satz on Sun, 01 Mar 2009 18:07:45 GMT]]></title><description><![CDATA[<p>oder gleich nur</p>
<pre><code class="language-cpp">if(TestedNumber == RossiBigInt(2)) return true; //2 is a Prime Number
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1672476</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1672476</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Sun, 01 Mar 2009 18:07:45 GMT</pubDate></item><item><title><![CDATA[Reply to Kleiner fermatscher Satz on Sun, 01 Mar 2009 21:25:03 GMT]]></title><description><![CDATA[<p>volkard schrieb:</p>
<blockquote>
<pre><code class="language-cpp">bool IsPrimeNumber(const string &amp;testnum, const string &amp;base) {
	RossiBigInt TestedNumber(testnum, DEC_DIGIT), Base(base, DEC_DIGIT);
	RossiBigInt ZERO(&quot;0&quot;, DEC_DIGIT), ONE(&quot;1&quot;, DEC_DIGIT), TWO = ONE + ONE, exp = TestedNumber, addi = ONE, itsquare = Base;
	if(TestedNumber == TWO) return true; //2 is a Prime Number
</code></pre>
<p>der test könnte auch als erste zeile sein. und alle variablen so spät wie möglich anlegen. nicht einfach alle zu beginn der funktion auf vorrat.</p>
</blockquote>
<p>Müssen die Variablen denn nicht sowieso beim Aufruf der Funktion angelegt werden???</p>
<p>Ansonsten:</p>
<pre><code class="language-cpp">bool IsPrimeNumber(string const&amp; testnum, string const&amp; base);

bool IsPrimeNumber(string const&amp;  testnum, string const&amp; base) {
	if(!testnum.compare(&quot;2&quot;)) return true; //2 is a Prime Number
	if(!testnum.compare(&quot;0&quot;) || !testnum.compare(&quot;1&quot;)) return false; //by definition 0 and 1 are not prime numbers
	RossiBigInt TestedNumber(testnum, DEC_DIGIT), Base(base, DEC_DIGIT);
	RossiBigInt ONE(1), exp = TestedNumber, addi = ONE, itsquare = Base;
	--exp;
	string binary = exp.binary_value();
	for(string::size_type i=binary.size();i&gt;0;i--) {
		if(binary[i - 1] == '1')
			addi = (addi * itsquare) % TestedNumber;
		itsquare = (itsquare * itsquare) % TestedNumber;
	}
	return (addi == ONE);
}
</code></pre>
<p>Geht das jetzt so einigermaßen???</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1672542</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1672542</guid><dc:creator><![CDATA[DerFrosch]]></dc:creator><pubDate>Sun, 01 Mar 2009 21:25:03 GMT</pubDate></item><item><title><![CDATA[Reply to Kleiner fermatscher Satz on Sun, 01 Mar 2009 22:01:31 GMT]]></title><description><![CDATA[<p>DerFrosch schrieb:</p>
<blockquote>
<p>Geht das jetzt so einigermaßen???</p>
</blockquote>
<p>die klammern bei return stören mich immer noch.<br />
den rest könnte man wohl als vorbildlich einstufen. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f60b.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_savoring_food"
      title=":yum:"
      alt="😋"
    /><br />
ONE könnte noch const werden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1672546</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1672546</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Sun, 01 Mar 2009 22:01:31 GMT</pubDate></item><item><title><![CDATA[Reply to Kleiner fermatscher Satz on Sun, 01 Mar 2009 22:12:36 GMT]]></title><description><![CDATA[<p>volkard schrieb:</p>
<blockquote>
<p>DerFrosch schrieb:</p>
<blockquote>
<p>Geht das jetzt so einigermaßen???</p>
</blockquote>
<p>die klammern bei return stören mich immer noch.<br />
den rest könnte man wohl als vorbildlich einstufen. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f60b.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_savoring_food"
      title=":yum:"
      alt="😋"
    /><br />
ONE könnte noch const werden.</p>
</blockquote>
<p>Gut, dann machen wa das mal:</p>
<pre><code class="language-cpp">bool IsPrimeNumber(string const&amp; testnum, string const&amp; base);

bool IsPrimeNumber(string const&amp;  testnum, string const&amp; base) {
	if(!testnum.compare(&quot;2&quot;)) return true; //2 is a Prime Number
	if(!testnum.compare(&quot;0&quot;) || !testnum.compare(&quot;1&quot;)) return false; //by definition 0 and 1 are not prime numbers
	RossiBigInt const ONE(1);
	RossiBigInt addi = ONE, TestedNumber(testnum, DEC_DIGIT), Base(base, DEC_DIGIT), exp = TestedNumber, itsquare = Base;
	--exp;
	string binary = exp.binary_value();
	for(string::size_type i=binary.size();i&gt;0;i--) {
		if(binary[i - 1] == '1')
			addi = (addi * itsquare) % TestedNumber;
		itsquare = (itsquare * itsquare) % TestedNumber;
	}
	return addi == ONE;
}
//------------------------------------------------------------------------------
</code></pre>
<p>Vielen Dank für die Tipps Volkard... <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /> <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /> <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1672554</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1672554</guid><dc:creator><![CDATA[DerFrosch]]></dc:creator><pubDate>Sun, 01 Mar 2009 22:12:36 GMT</pubDate></item><item><title><![CDATA[Reply to Kleiner fermatscher Satz on Sun, 01 Mar 2009 23:18:52 GMT]]></title><description><![CDATA[<p>Vllt. noch eine winzige Änderung <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
<pre><code class="language-cpp">bool IsPrimeNumber(string const&amp; testnum, string const&amp; base);

bool IsPrimeNumber(string const&amp;  testnum, string const&amp; base) {
	if(!testnum.compare(&quot;2&quot;)) return true; //2 is a Prime Number
	if(!testnum.compare(&quot;0&quot;) || !testnum.compare(&quot;1&quot;)) return false; //by definition 0 and 1 are not prime numbers
	RossiBigInt const ONE(1);
	RossiBigInt addi = ONE, TestedNumber(testnum, DEC_DIGIT), Base(base, DEC_DIGIT), exp = TestedNumber - ONE, itsquare = Base;
	string binary = exp.binary_value();
	for(string::size_type i=binary.size();i&gt;0;i--) {
		if(binary[i - 1] == '1')
			addi = (addi * itsquare) % TestedNumber;
		itsquare = (itsquare * itsquare) % TestedNumber;
	}
	return addi == ONE;
}
//------------------------------------------------------------------------------
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1672577</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1672577</guid><dc:creator><![CDATA[DerFrosch]]></dc:creator><pubDate>Sun, 01 Mar 2009 23:18:52 GMT</pubDate></item><item><title><![CDATA[Reply to Kleiner fermatscher Satz on Sun, 01 Mar 2009 23:48:54 GMT]]></title><description><![CDATA[<p>und noch was winziges:</p>
<pre><code class="language-cpp">bool IsPrimeNumber(string const&amp; testnum, string const&amp; base);

bool IsPrimeNumber(string const&amp;  testnum, string const&amp; base) {
	if(!testnum.compare(&quot;2&quot;)) return true; //2 is a Prime Number
	if(!testnum.compare(&quot;0&quot;) || !testnum.compare(&quot;1&quot;)) return false; //by definition 0 and 1 are not prime numbers
	RossiBigInt addi = One, TestedNumber(testnum, DEC_DIGIT), Base(base, DEC_DIGIT), exp = TestedNumber - One, itsquare = Base;
	string binary = exp.binary_value();
	for(string::size_type i=binary.size();i&gt;0;i--) {
		if(binary[i - 1] == '1')
			addi = (addi * itsquare) % TestedNumber;
		itsquare = (itsquare * itsquare) % TestedNumber;
	}
	return addi == ONE;
}
//------------------------------------------------------------------------------
</code></pre>
<p>ups, doch nicht. der hat ja One global static gemacht. welche ein frevel. wöre so hübsch als puvlic static in der klasse. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1672580</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1672580</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Sun, 01 Mar 2009 23:48:54 GMT</pubDate></item></channel></rss>