Weitere Varianten.
This commit is contained in:
parent
243c54a8ef
commit
6a45dceeaf
|
@ -1,6 +1,7 @@
|
|||
CFLAGS = -Wall -O9
|
||||
all: collatz collatz2
|
||||
all: collatz collatz2 collatz3 modf
|
||||
|
||||
collatz:
|
||||
|
||||
collatz2:
|
||||
collatz2: collatz2.o
|
||||
$(CC) -o $@ $^ -lm
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
#define USE_INT 1
|
||||
#define USE_DOUBLE 1
|
||||
|
||||
#ifdef USE_INT
|
||||
typedef int T;
|
||||
|
@ -11,10 +12,18 @@
|
|||
typedef long T;
|
||||
#define FT "%ld"
|
||||
#define even(x) ((x) % 2 == 0)
|
||||
#elif USE_LONG_LONG
|
||||
typedef long long T;
|
||||
#define FT "%lld"
|
||||
#define even(x) ((x) % 2 == 0)
|
||||
#elif USE_DOUBLE
|
||||
typedef double T;
|
||||
#define FT "%20g"
|
||||
#define FT "%20.1f"
|
||||
#ifdef USE_FLOOR
|
||||
#define even(x) (floor((x) / 2) == ((x) / 2))
|
||||
#else
|
||||
#define even(x) (fmod((x), 2.0) == 0.0)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
long collatz(T j) {
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define USE_INT 1
|
||||
|
||||
#ifdef USE_INT
|
||||
typedef int T;
|
||||
#define FT "%d"
|
||||
#define odd(x) ((x) % 2 == 1)
|
||||
#elif USE_LONG
|
||||
typedef long T;
|
||||
#define FT "%ld"
|
||||
#define odd(x) ((x) % 2 == 1)
|
||||
#elif USE_DOUBLE
|
||||
typedef double T;
|
||||
#define FT "%20g"
|
||||
#define odd(x) (floor((x) / 2) != ((x) / 2))
|
||||
#endif
|
||||
|
||||
long collatz(T j) {
|
||||
|
||||
long c = 0;
|
||||
while (j != 1) {
|
||||
printf("%ld: " FT "\n", c, j);
|
||||
if (odd(j)) {
|
||||
j = j * 3 + 1;
|
||||
} else {
|
||||
j = j / 2;
|
||||
}
|
||||
c++;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
T i = 113383;
|
||||
long c = collatz(i);
|
||||
printf("%ld\n", c);
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
int main(void) {
|
||||
double x = 3.14;
|
||||
double y, z;
|
||||
|
||||
y = modf(x, &z);
|
||||
printf("%g, %g\n", y, z);
|
||||
|
||||
y = modf(x, NULL);
|
||||
printf("%g, %g\n", y, z);
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
Ich kannte dieses Problem ursprünglich als "Ulams Vermutung".
|
||||
|
||||
Laut http://de.wikipedia.org/wiki/Collatz-Problem stammt es aber
|
||||
ursprünglich von Lothar Collatz.
|
Loading…
Reference in New Issue