Skip to content
Snippets Groups Projects
Commit ea453911 authored by Peter Gerwinski's avatar Peter Gerwinski
Browse files

weitere Beispiel-Programme 3.12.2020

parent 4b2b6534
No related branches found
No related tags found
No related merge requests found
Showing
with 495 additions and 0 deletions
Fibonacci-Zahl Nr. 199:
* Quelle: http://www.wackerart.de/mathematik/big_numbers/fibonacci_numbers.html
173 402 521 172 797 813 159 685 037 284 371 942 044 301
* Quelle: Wolfram Alpha
173 402 521 172 797 813 159 685 037 284 371 942 044 301
* Quelle: ./loesung-3-12
173 402 521 172 797 813 134 481 939 662 687 621 349 376
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Ab loesung-3-7.c ist die Berechnung der Fibonacci-Zahlen falsch!
~~~~~~
Derartige Rechnungen darf man nur mit ganzen Zahlen durchführen,
nicht jedoch mit Fließkommazahlen.
Dies gilt insbesondere auch für Kryptographie, z.B. RSA-Verschlüsselung.
Die Fibonacci-Zahl Nr. 199 paßt selbst in eine "long long unsigned" nicht hinein.
Wie kann man die Genauigkeit des Rechners erhöhen?
--> "schriftlich" rechnen
13 · 17 = (10 + 3) · (10 + 7)
= 10 · 10 + 10 · 7 + 3 · 10 + 3 · 7 --> nur noch kleines Einmaleins,
evtl. mit Nullen dran
= 100
+ 70
+ 30
+ 21
---1--
221
--> Funktion, um 2 128-Bit-Zahlen zu addieren
- Wir speichern jede 128-Bit-Zahl in einem Array von 4 32-Bit-Zahlen
- Wir addieren jeweils 2 32-Bit-Zahlen ("1 Ziffer").
Das Ergebnis kann größer sein als 32 Bit, aber nicht größer als 64 Bit.
- Die unteren 32 Bit speichern wir als Ergebnis (7 + 5: "2 hin").
Die oberen merken wir uns als Übertrag ("1 im Sinn").
- Damit haben wir den "Einer" addiert.
Als nächstes kommt der "Zehner" (tatsächlich: der 2^32er).
--> Wir gehen das Array in einer Schleife von rechts nach links durch.
Weniger effizient, aber leichter auszugeben: Dezimalziffern als Array speichern.
--> siehe ../20201203/fibonacci-*.c
* Quelle: ../20201203/fibonacci-10.c
173 402 521 172 797 813 159 685 037 284 371 942 044 301
--> :-)
f[9999] = 20793608237133498072112648988642836825087036094015903119682945866528501423455686648927456034305226515591757343297190158010624794267250973176133810179902738038231789748346235556483191431591924532394420028067810320408724414693462849062668387083308048250920654493340878733226377580847446324873797603734794648258113858631550404081017260381202919943892370942852601647398213554479081823593715429566945149312993664846779090437799284773675379284270660175134664833266377698642012106891355791141872776934080803504956794094648292880566056364718187662668970758537383352677420835574155945658542003634765324541006121012446785689171494803262408602693091211601973938229446636049901531963286159699077880427720289235539329671877182915643419079186525118678856821600897520171070499437657067342400871083908811800976259727431820539554256869460815355918458253398234382360435762759823179896116748424269545924633204614137992850814352018738480923581553988990897151469406131695614497783720743461373756218685106856826090696339815490921253714537241866911604250597353747823733268178182198509240226955826416016690084749816072843582488613184829905383150180047844353751554201573833105521980998123833253261228689824051777846588461079790807828367132384798451794011076569057522158680378961532160858387223882974380483931929541222100800313580688585002598879566463221427820448492565073106595808837401648996423563386109782045634122467872921845606409174360635618216883812562321664442822952537577492715365321134204530686742435454505103269768144370118494906390254934942358904031509877369722437053383165360388595116980245927935225901537634925654872380877183008301074569444002426436414756905094535072804764684492105680024739914490555904391369218696387092918189246157103450387050229300603241611410707453960080170928277951834763216705242485820801423866526633816082921442883095463259080471819329201710147828025221385656340207489796317663278872207607791034431700112753558813478888727503825389066823098683355695718137867882982111710796422706778536913192342733364556727928018953989153106047379741280794091639429908796650294603536651238230626
(2090 Ziffern)
#include <stdio.h>
int main (void)
{
long double f0 = 0;
long double f1 = 1;
for (int i = 0; i < 200; i++)
{
printf ("f[%d] = %.0Lf\n", i, f0);
long double f2 = f0 + f1;
f0 = f1;
f1 = f2;
}
return 0;
}
#include <stdio.h>
int main (void)
{
long double f0 = 0;
long double f1 = 1;
for (int i = 0; i < 200; i++)
{
printf ("f[%d] = %30.0Lf\n", i, f0);
long double f2 = f0 + f1;
f0 = f1;
f1 = f2;
}
return 0;
}
#include <stdio.h>
int main (void)
{
long double f0 = 0;
long double f1 = 1;
for (int i = 0; i < 200; i++)
{
printf ("f[%3d] = %60.0Lf\n", i, f0);
long double f2 = f0 + f1;
f0 = f1;
f1 = f2;
}
return 0;
}
#include <stdio.h>
int main (void)
{
long int f0 = 0;
long int f1 = 1;
for (int i = 0; i < 50; i++)
{
printf ("f[%d] = %d\n", i, f0);
int f2 = f0 + f1;
f0 = f1;
f1 = f2;
}
return 0;
}
#include <stdio.h>
int main (void)
{
long int f0 = 0;
long int f1 = 1;
for (int i = 0; i < 50; i++)
{
printf ("f[%d] = %ld\n", i, f0);
long int f2 = f0 + f1;
f0 = f1;
f1 = f2;
}
return 0;
}
#include <stdio.h>
int main (void)
{
long f0 = 0;
long f1 = 1;
for (int i = 0; i < 50; i++)
{
printf ("f[%d] = %ld\n", i, f0);
long f2 = f0 + f1;
f0 = f1;
f1 = f2;
}
return 0;
}
#include <stdio.h>
#include <stdint.h>
int main (void)
{
uint64_t f0 = 0;
uint64_t f1 = 1;
for (int i = 0; i < 100; i++)
{
printf ("f[%d] = %lu\n", i, f0);
uint64_t f2 = f0 + f1;
f0 = f1;
f1 = f2;
}
return 0;
}
#include <stdio.h>
int main (void)
{
long double f0 = 0;
long double f1 = 1;
for (int i = 0; i < 100; i++)
{
printf ("f[%d] = %llf\n", i, f0);
long double f2 = f0 + f1;
f0 = f1;
f1 = f2;
}
return 0;
}
#include <stdio.h>
int main (void)
{
long double f0 = 0;
long double f1 = 1;
for (int i = 0; i < 100; i++)
{
printf ("f[%d] = %Lf\n", i, f0);
long double f2 = f0 + f1;
f0 = f1;
f1 = f2;
}
return 0;
}
#include <stdio.h>
int main (void)
{
long double f0 = 0;
long double f1 = 1;
for (int i = 0; i < 100; i++)
{
printf ("f[%d] = %.0Lf\n", i, f0);
long double f2 = f0 + f1;
f0 = f1;
f1 = f2;
}
return 0;
}
Ich ging im Walde
so für mich hin,
und nichts zu suchen,
das war mein Sinn.
vim ist praktisch.
#include <stdio.h>
int main (void)
{
int f0 = 0;
int f1 = 1;
for (int i = 0; i < 50; i++)
{
printf ("f[%d] = %d\n", i, f0);
int f2 = f0 + f1;
f0 = f1;
f1 = f2;
}
return 0;
}
#include <stdio.h>
#define DIGITS 60
typedef char number[DIGITS];
void add (char *a, char *b, char *c)
{
char carry = 0;
for (int i = 0; i < DIGITS; i++)
{
c[i] = a[i] + b[i] + carry;
if (c[i] > 9)
{
carry = 1;
c[i] -= 10;
}
else
carry = 0;
}
}
void copy (char *a, char *b)
{
for (int i = 0; i < DIGITS; i++)
b[i] = a[i];
}
void print (char *a)
{
int i = DIGITS - 1;
while (i > 0 && a[i] == 0)
i--;
while (i >= 0)
{
printf ("%d", a[i]);
i--;
}
}
int main (void)
{
number f0 = { 0 };
number f1 = { 1 };
for (int i = 0; i < 200; i++)
{
printf ("f[%d] = ", i);
print (f0);
printf ("\n");
number f2;
add (f0, f1, f2);
copy (f1, f0);
copy (f2, f1);
}
return 0;
}
#include <stdio.h>
#define DIGITS 20000
typedef char number[DIGITS];
void add (char *a, char *b, char *c)
{
char carry = 0;
for (int i = 0; i < DIGITS; i++)
{
c[i] = a[i] + b[i] + carry;
if (c[i] > 9)
{
carry = 1;
c[i] -= 10;
}
else
carry = 0;
}
}
void copy (char *a, char *b)
{
for (int i = 0; i < DIGITS; i++)
b[i] = a[i];
}
void print (char *a)
{
int i = DIGITS - 1;
while (i > 0 && a[i] == 0)
i--;
while (i >= 0)
{
printf ("%d", a[i]);
i--;
}
}
int main (void)
{
number f0 = { 0 };
number f1 = { 1 };
for (int i = 0; i < 10000; i++)
{
printf ("f[%d] = ", i);
print (f0);
printf ("\n");
number f2;
add (f0, f1, f2);
copy (f1, f0);
copy (f2, f1);
}
return 0;
}
#include <stdio.h>
void add (int *a, int *b, int *c)
{
*c = *a + *b;
}
int main (void)
{
int f0 = 0;
int f1 = 1;
for (int i = 0; i < 50; i++)
{
printf ("f[%d] = %d\n", i, f0);
int f2;
add (&f0, &f1, &f2);
f0 = f1;
f1 = f2;
}
return 0;
}
#include <stdio.h>
typedef int number;
void add (number *a, number *b, number *c)
{
*c = *a + *b;
}
int main (void)
{
number f0 = 0;
number f1 = 1;
for (int i = 0; i < 50; i++)
{
printf ("f[%d] = %d\n", i, f0);
number f2;
add (&f0, &f1, &f2);
f0 = f1;
f1 = f2;
}
return 0;
}
#include <stdio.h>
#define DIGITS 42
typedef char number[DIGITS];
void add (number *a, number *b, number *c)
{
c[0] = a[0] + b[0];
}
void copy (number *a, number *b)
{
for (int i = 0; i < DIGITS; i++)
b[i] = a[i];
}
int main (void)
{
number f0 = { 0 };
number f1 = { 1 };
for (int i = 0; i < 50; i++)
{
printf ("f[%d] = %d\n", i, f0);
number f2;
add (&f0, &f1, &f2);
copy (f1, f0);
copy (f2, f1);
}
return 0;
}
#include <stdio.h>
#define DIGITS 42
typedef char number[DIGITS];
void add (char *a, char *b, char *c)
{
c[0] = a[0] + b[0];
}
void copy (char *a, char *b)
{
for (int i = 0; i < DIGITS; i++)
b[i] = a[i];
}
int main (void)
{
number f0 = { 0 };
number f1 = { 1 };
for (int i = 0; i < 50; i++)
{
printf ("f[%d] = %d\n", i, f0);
number f2;
add (f0, f1, f2);
copy (f1, f0);
copy (f2, f1);
}
return 0;
}
#include <stdio.h>
#define DIGITS 42
typedef char number[DIGITS];
void add (char *a, char *b, char *c)
{
c[0] = a[0] + b[0];
}
void copy (char *a, char *b)
{
for (int i = 0; i < DIGITS; i++)
b[i] = a[i];
}
void print (char *a)
{
int i = DIGITS - 1;
while (i > 0 && a[i] == 0)
i--;
while (i >= 0)
{
printf ("%d", a[i]);
i--;
}
}
int main (void)
{
number f0 = { 0 };
number f1 = { 1 };
for (int i = 0; i < 50; i++)
{
printf ("f[%d] = ", i);
print (f0);
printf ("\n");
number f2;
add (f0, f1, f2);
copy (f1, f0);
copy (f2, f1);
}
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment