Autor Beitrag
derDoc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 623

Win Vista Prof
D2007 Prof
BeitragVerfasst: Mo 10.03.03 18:49 
Ich bin zur Zeit auf der Suche nach einer Möglichkeit, eine Primzahl zufällig zu erstellen. Kennt jemand dafür eine vernünftige Lösung?

_________________
MfG derDoc
There are only 10 types of people: those who understand binary and those who don't.
Segelflieger
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 124

WinXP Pro
D7 Prof
BeitragVerfasst: Mo 10.03.03 19:22 
Also, mein Vorschlag wäre:

Eine Zufallszahl mit random(max) erzeugen und überprüfen, ob es sich um eine Primzahl handelt. Wenn nicht, dann einfach so lange um 1 erhöhen, bis eine Primzahl daraus wird.
[url=www.delphi-forum.de/viewtopic.php?t=8434]Hier[/url] geht es um eine funktion, mit der man überprüfen kann, ob es eine Primzahl ist.

MfG

_________________
Früher hatten die Menschen Angst vor der Zukunft. Heute muss die Zukunft Angst vor den Menschen haben.
derDoc Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 623

Win Vista Prof
D2007 Prof
BeitragVerfasst: Mo 10.03.03 20:29 
Ich hatte eigentlich gehofft, dass es da eine elegantere Lösung gäbe.

_________________
MfG derDoc
There are only 10 types of people: those who understand binary and those who don't.
tommie-lie
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 4373

Ubuntu 7.10 "Gutsy Gibbon"

BeitragVerfasst: Mo 10.03.03 20:34 
also eigentlich ist Segelfliegers Methode schon eine recht gute und einfache.
Aber du könntest auch einen Pool an Primzahlen anlegen (zum Beispiel mit 'nem array oder so) und dann durch eine Zufallszahl eine Zahl aus diesem array schnappen. Aber dafür musst du deinen Pool erstmal haben. Aber das ist im Prinzip das Vorgehen bei Zufallszahlen aus bestimmten Bereichen. Normalerweise umfasst dieser Bereich alle Zahlen und ist daher sowieso im Zahlensystem festgelegt, aber abstrakt funktioniert auch ein "normaler" Zufallsgeneraor nach diesem Prinzip (siehe 49 Zahlen beim Lotto).

_________________
Your computer is designed to become slower and more unreliable over time, so you have to upgrade. But if you'd like some false hope, I can tell you how to defragment your disk. - Dilbert
derDoc Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 623

Win Vista Prof
D2007 Prof
BeitragVerfasst: Mo 10.03.03 20:37 
Dann müsste ich aber erst wie du schon schreibst einen Pool von Primzahlen haben. Also kann mir jemand sagen, wo ich alle Primzahlen im Bereich von 2^512 Zahlen herbekommen kann?

_________________
MfG derDoc
There are only 10 types of people: those who understand binary and those who don't.
tommie-lie
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 4373

Ubuntu 7.10 "Gutsy Gibbon"

BeitragVerfasst: Mo 10.03.03 20:42 
oben steht ein Link, und da drücken Sie dann drauf...
und schon kannst du iterativ jede Zahl durchgehen und wenn sie prim ist, gehört sie auch in den Pool. Aber 2^512 wird eh nicht machbar sein, weil selbst der Int64 nur 64 bit für eine Zahl hat, nicht 512. Die Zahlen könntest du also schon gar nicht darstellen (sowas wie addieren und so weiter geht auch nicht, auch kein IntToStr, selbst eine simple Ausgabe auf den Bildschirm wäre also nichts). Dafür bräuchtest du eine Unit für große Integerzahlen. Müsstest mal bei Torry oder sonstwo suchen...

_________________
Your computer is designed to become slower and more unreliable over time, so you have to upgrade. But if you'd like some false hope, I can tell you how to defragment your disk. - Dilbert
derDoc Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 623

Win Vista Prof
D2007 Prof
BeitragVerfasst: Mo 10.03.03 21:21 
Das Erstellen von 512 Bit Integern ist nicht mein Problem, eigentlich hatte ich gehofft, dass jemand mal eine Funktion geschrieben hatte oder dass es irgendwo im Internet eine Liste mit Primzahlen gibt und jemand diese kennen würde.

_________________
MfG derDoc
There are only 10 types of people: those who understand binary and those who don't.
mars
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 238

Debian Woody, Win 2000, Win XP
D7 Ent, Kylix 3
BeitragVerfasst: Mo 10.03.03 21:29 
Also Listen gibt es viele; das ist kein Problem. Hier ist eine sehr umfangreiche :D :
quasar.artis.uni-oldenburg.de/cgi-bin/prim

Hier noch ein interessanter Ansatz zur Primzahlenbestimmung (Das Sieb des Erastothenes):
www.cenbells.de/wissen.php?page=primzahlen

Viel Spass.
Darf man übrigens auch fragen, was du genau damit machen willst? (fragen darf ich ja schon, aber antwortet er mir auch?...)
derDoc Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 623

Win Vista Prof
D2007 Prof
BeitragVerfasst: Mo 10.03.03 22:52 
Klar kann ich dir das sagen. Im Prinzip geht es um einen simplen RSA Algorithmus, aber ich muss ihn noch auf große Zahlen auslegen. Das kann ich aber erst machen, wenn ich auch kleine hinbekomme.

_________________
MfG derDoc
There are only 10 types of people: those who understand binary and those who don't.
Motzi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2931

XP Prof, Vista Business
D6, D2k5-D2k7 je Prof
BeitragVerfasst: Di 11.03.03 11:40 
Du willst dir einen Primzahlen-Pool für alle Primzahlen im Bereich von 2^512 anlegen?!? Na dann würd ich dir mal empfehlen dir noch ein paar Festplatten zuzulegen..! :wink:

Ne, also in dem Zahlenbereich kannst du einen Primzahlenpool vergessen! Normalerweise wird eine Zufallszahl erzeugt (wenn möglich mit einem kryptographisch sicheren Pseudo-Zufallsalgorithmus) und diese dann getestet ob sie prim ist. Ist das nicht der Fall wird eine neue Zufallszahl berechnet und wieder getestet, usw. Wichtig hierbei ist, dass nicht die (bei kleinen Primzahlen übliche) Methode angewandt wird, bei der für alle Zahlen < Sqrt(Zufallszahl) getestet wird ob sie Teiler der Zufallszahl ist, denn das dauert dann ewig (Prinzip der Primfaktorisierung (allerdings die ineffizienteste Methode))! Es gibt noch ein paar andere Test, die zwar nicht mit 100%iger, aber mit ausreichender Sicherheit feststellen ob eine Zahl prim ist oder nicht (mir falln nur grad die Namen dieser Tests nicht ein.. müsste zuhaus nochmal nachschaun).

_________________
gringo pussy cats - eef i see you i will pull your tail out by eets roots!
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Di 11.03.03 11:52 
Zitat:
Es gibt noch ein paar andere Test, die zwar nicht mit 100%iger, aber mit ausreichender Sicherheit feststellen ob eine Zahl prim ist oder nicht (mir falln nur grad die Namen dieser Tests nicht ein.. müsste zuhaus nochmal nachschaun).
Einen sehr beliebten habe ich gestern in dem Thread zum Thema "ist prim" gepostet!

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Motzi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2931

XP Prof, Vista Business
D6, D2k5-D2k7 je Prof
BeitragVerfasst: Di 11.03.03 15:02 
Peter Lustig hat folgendes geschrieben:
Einen sehr beliebten habe ich gestern in dem Thread zum Thema "ist prim" gepostet!

Du meinst den Miller-Rabin.. habs schon gesehen..
Es gibt aber auch noch bessere / schnellere... ich schau zu Hause noch mal nach! Aber es sei schonmal gesagt.. diese Algos sind noch wesentlich komplizierter als Miller-Rabin, da bei einem zB das Jacobi-Symbol verwendet wird und für das gibt es in der Unit Maths keine Implementierung -> sprich die muss man dann auch noch selber machn...

_________________
gringo pussy cats - eef i see you i will pull your tail out by eets roots!
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Di 11.03.03 15:21 
Vom Jacobi-Symbol habe ich noch nie gehört und im Bronstein habe ich es auch nicht gefunden. Was ist denn das?
Der Algorithmus sollte natürlich auch für große Zahlen - sprich Units, die wahrscheinlich eine sehr begrenzte Anzahl an mathematischen Funktionen implementieren - nicht allzu schwer umsetzbar sein. Bei Miller-Rabin habe ich das in Java mal für BigInteger gemacht, das war nicht allzu schwer und die Sicherheit, die man nach kurzer Zeit (also 50 oder 100 Tests) erreichen kann, ist sehr hoch.

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
derDoc Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 623

Win Vista Prof
D2007 Prof
BeitragVerfasst: Di 11.03.03 15:33 
Also ich habe gerade mal den Algorithmus von Peter Lustig in das Programm geschrieben und werde mal die Zeit nehmen.

_________________
MfG derDoc
There are only 10 types of people: those who understand binary and those who don't.
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Di 11.03.03 16:17 
Habe gerade (mit dem korrigierten Quelltext, siehe anderer Thread) die Primzahl 4544663 1000mal getestet. Mein XP2000+ hat dafür 16 Millisekunden benötigt. Die Fehlerwahrscheinlichkeit beträgt dabei dann 9,3*10^(-302)!

MfG,
Peter

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
O'rallY
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 563



BeitragVerfasst: Fr 19.09.03 17:06 
Ich hab bei Swissdelphicenter diesen Code entdeckt:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
function IsPrime(N: Cardinal): Boolean; register
  // test if N is prime, do some small Strong Pseudo Prime test in certain bounds 
  // copyright (c) 2000 Hagen Reddmann, don't remove 
asm 
      TEST  EAX,1            { Odd(N) ?? } 
      JNZ   @@1 
      CMP   EAX,2            { N == 2 ?? } 
      SETE  AL 
      RET 
@@1:  CMP   EAX,73           { N        JB    @@C } 
      JE    @@E              { N == 73 ?? } 
      PUSH  ESI 
      PUSH  EDI 
      PUSH  EBX 
      PUSH  EBP 
      PUSH  EAX              { save N as Param for @@5 } 
      LEA   EBP,[EAX - 1]    {  M == N -1, Exponent } 
      MOV   ECX,32           { calc remaining Bits of M and shift M' } 
      MOV   ESI,EBP 
@@2:  DEC   ECX 
      SHL   ESI,1 
      JNC   @@2 
      PUSH  ECX              { save Bits as Param for @@5 } 
      PUSH  ESI              {  save M' as Param for @@5 } 
      CMP   EAX,08A8D7Fh     {  N = 9080191 ?? } 
      JAE   @@3 
      // now if (N       MOV   EAX,31 
      CALL  @@5              { 31^((N-1)(2^s)) mod N } 
      JC    @@4 
      MOV   EAX,73           { 73^((N-1)(2^s)) mod N } 
      PUSH  OFFSET @@4 
      JMP   @@5 
      // now if (N @@3:   MOV   EAX,2 
      CALL  @@5 
      JC    @@4 
      MOV   EAX,7 
      CALL  @@5 
      JC    @@4 
      MOV   EAX,61 
      CALL  @@5 
@@4:  SETNC AL 
      ADD   ESP,4 * 3 
      POP   EBP 
      POP   EBX 
      POP   EDI 
      POP   ESI 
      RET 
// do a Strong Pseudo Prime Test 
@@5:  MOV   EBX,[ESP + 12]     { N on stack } 
      MOV   ECX,[ESP +  8]     { remaining Bits } 
      MOV   ESI,[ESP +  4]     { M' } 
      MOV   EDI,EAX            { T = b, temp. Base } 
@@6:  DEC   ECX 
      MUL   EAX 
      DIV   EBX 
      MOV   EAX,EDX 
      SHL   ESI,1 
      JNC   @@7 
      MUL   EDI 
      DIV   EBX 
      AND   ESI,ESI 
      MOV   EAX,EDX 
@@7:  JNZ   @@6 
      CMP   EAX,1      { b^((N -1)(2^s)) mod N ==  1 mod N ?? } 
      JE    @@A 
@@8:  CMP   EAX,EBP    { b^((N -1)(2^s)) mod N == -1 mod N ?? , EBP = N -1 } 
      JE    @@A 
      DEC   ECX        { second part to 2^s } 
      JNG   @@9 
      MUL   EAX 
      DIV   EBX 
      CMP   EDX,1 
      MOV   EAX,EDX 
      JNE   @@8 
@@9:  STC 
@@A:  RET 
@@B:  DB    3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71 
@@C:  MOV   EDX,OFFSET @@B 
      MOV   ECX,18 
@@D:  CMP   AL,[EDX + ECX] 
      JE    @@E 
      DEC   ECX 
      JNL   @@D 
@@E:  SETE  AL 
end;


//edit: Beim Debuggen heißt es allerdings bei JAE @@3 "Undeclared identifier: '@@3'".

Und da ich keine Ahnung von Assambler hab... Ich würd jetzt einfach mal sagen, dass die Variable (?) @@3 nicht defeniert wurde :?.

_________________
.oO'rallY
Linux is like a tipi: No gates, no windows and a gnu-eating apache inside...
DaRkFiRe
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 526

WinXP Home & Professional
C, C++, Delphi
BeitragVerfasst: Sa 20.09.03 10:32 
Das is ne Sprungadresse - und die wird sicher irgendwo zwischen @@2 und @@4 liegen, oder? Quasi wie ne Funktion aufrufen, die gar nich existiert.

Nach einer ersten halbwegs logischen Überlegung müsste die Zeile, wo
"// now if (N @@3: MOV EAX,2" steht, @@3 sein.

Probiert ma... Kein Bock, mein Delphi aufzumachen

_________________
Lang ist der Weg durch Lehren - kurz und wirksam durch Beispiele! Seneca