Scratchbook

Das Leben ist immer anders als die Realität.

Primzahlen berechnen – PHP machts möglich!

Claude, 8. Februar 2005, 20:58 Uhr

Warning: Use of undefined constant ri_rand_compare - assumed 'ri_rand_compare' (this will throw an Error in a future version of PHP) in /home/httpd/vhosts/scratchbook.ch/httpdocs/wp-content/plugins/random-image-widget/random_image.php on line 129 Warning: Use of undefined constant ri_rand_compare - assumed 'ri_rand_compare' (this will throw an Error in a future version of PHP) in /home/httpd/vhosts/scratchbook.ch/httpdocs/wp-content/plugins/random-image-widget/random_image.php on line 130 Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/httpd/vhosts/scratchbook.ch/httpdocs/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384 Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/httpd/vhosts/scratchbook.ch/httpdocs/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384 Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/httpd/vhosts/scratchbook.ch/httpdocs/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384 Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/httpd/vhosts/scratchbook.ch/httpdocs/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384 Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/httpd/vhosts/scratchbook.ch/httpdocs/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384 Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/httpd/vhosts/scratchbook.ch/httpdocs/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384 Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/httpd/vhosts/scratchbook.ch/httpdocs/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384 Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/httpd/vhosts/scratchbook.ch/httpdocs/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384 Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/httpd/vhosts/scratchbook.ch/httpdocs/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384 Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/httpd/vhosts/scratchbook.ch/httpdocs/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384 Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/httpd/vhosts/scratchbook.ch/httpdocs/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384 Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/httpd/vhosts/scratchbook.ch/httpdocs/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384 Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/httpd/vhosts/scratchbook.ch/httpdocs/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384 Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/httpd/vhosts/scratchbook.ch/httpdocs/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384 Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/httpd/vhosts/scratchbook.ch/httpdocs/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384 Warning: preg_match_all(): Compilation failed: invalid range in character class at offset 4 in /home/httpd/vhosts/scratchbook.ch/httpdocs/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 700 Warning: Invalid argument supplied for foreach() in /home/httpd/vhosts/scratchbook.ch/httpdocs/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 707 Warning: preg_match_all(): Compilation failed: invalid range in character class at offset 4 in /home/httpd/vhosts/scratchbook.ch/httpdocs/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 700 Warning: Invalid argument supplied for foreach() in /home/httpd/vhosts/scratchbook.ch/httpdocs/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 707

219.jpg

Heute hab ich zum Spass einen kleinen Algorithmus entwickelt, um Primzahlen zu berechnen. Eine Primzahl ist eine Zahl, die nur durch sich selbst und durch 1 teilbar ist. Realisiert habe ich das ganze mit Schleifen und Abfragen (logisch was sonst 😉

Zur Funktionsweise:
Die vom User eingegebene Maximalzahl wird um 1 reduziert und in der Variable $now gespeichert. $result wird auf einen NICHT-Integer Wert gesetzt (einfach 0.0, Hauptsache es enthält Kommas).
Jetzt gehts auch schon los mit dem Rechnen. Während das Resultat ($result) nicht eine ganze Zahl ist, dividiere $max durch $now, wobei sich $now pro Durchlauf immer um 1 verringert.
Sobald $result eine ganze Zahl ist (integer), wird die while-Schleife abgebrochen und der nächste Schritt der For-Schleife wird aufgerufen, mit der um 1 verringerten Maximalzahl, und die ganze Berechnung wird mit den neuen Werten nochmals durchlaufen. Sobald die Maximalzahl 1 ist, wird das ganze beendet und ausgegeben.
Ich bin sicher, das Ganze würde schneller gehen, wenn ich die Berechnungsreihenfolge umkehren würde (von der kleinsten bis zur grössten Zahl).
Ach ja, wer nach dieser kleinen Erläuterung immer noch Bahnhof versteht, kann ja mal den Verbose-Modus einschalten. Dann wird jeder einzelne Schritt angezeigt.
Das ganze lässt sich natürlich auch in Java programmieren. Nur – Java ist mir einfach zu umständlich. Bei PHP muss ich mich nicht um Datentypen, Variablendeklarationen und solchem Quatsch herumschlagen, PHP macht einfach genau das was ich will.

Ihr findet den Primer im PHP-Playground:
[Link]

Longneck

ha s komfortmässig no bitz erwiteret:

ob_end_flush();
[Berechnung]
flush();

Die beide zyle sorge defür, dass s aktuelle zwüscheergäbnis grad sofort an browser gschiggt wird; vorher hets zerscht berächnet, und erst nachem rechevorgang alles uf ei schlag gschiggt.

Longneck

WWWOOOOWW!!! Was für e Gschwindigkeitszuewachs! Jetzt hani dr Code optimiert, d.h. d berechnig foht jetzt bi eins a und hört mi $max uf. isch öppe 3 bis 4 mol so schnell wie vorher! neui version im playground.

phex

Öhm… Nehmed mer mal a irgendöpper git en grösseri Zahl i… Das würd doch em Server nid schade oder?

Longneck

notürlich nid! alli skripts wärde nach 30 sec automatisch abbroche. chaschs ruhig usprobiere!

Warning: count(): Parameter must be an array or an object that implements Countable in /home/httpd/vhosts/scratchbook.ch/httpdocs/wp-content/themes/scratchbook/navigation_bar_bottom.php on line 1