diff options
Diffstat (limited to 'mat/euler/14')
-rw-r--r-- | mat/euler/14/Makefile | 2 | ||||
-rwxr-xr-x | mat/euler/14/a.out | bin | 0 -> 8432 bytes | |||
-rw-r--r-- | mat/euler/14/prog.c | 29 |
3 files changed, 31 insertions, 0 deletions
diff --git a/mat/euler/14/Makefile b/mat/euler/14/Makefile new file mode 100644 index 0000000..2def8af --- /dev/null +++ b/mat/euler/14/Makefile @@ -0,0 +1,2 @@ +default: + gcc prog.c diff --git a/mat/euler/14/a.out b/mat/euler/14/a.out Binary files differnew file mode 100755 index 0000000..8a10ce9 --- /dev/null +++ b/mat/euler/14/a.out diff --git a/mat/euler/14/prog.c b/mat/euler/14/prog.c new file mode 100644 index 0000000..bb0daf6 --- /dev/null +++ b/mat/euler/14/prog.c @@ -0,0 +1,29 @@ +#include <stdio.h> +#include <stdlib.h> +#define COLLATZ(n) n % 2 == 0 ? n / 2 : 3 * n + 1 +int main (int argc, char ** argv) { + if (argc != 1+1) { + fprintf(stderr, "uporaba: %s <stevilo>\nprimer-najd. collatzova veriga pod zač. 1M: %s 1000000\n", argv[0], argv[0]); + return 1; + } + unsigned long int n = strtol(argv[1], NULL, 10); + unsigned long int c = 0; // števeC + unsigned long int a = 0; // verigA + unsigned long int m = 0; // maksimalna dolžina verige + unsigned long int z = 0; // trenutna Zmagovalna številka z najdaljšo verigo + for (; n >= 1; n--) { + // fprintf(stderr, "\rračunam... potrebnih še %lu iteracij.", n); // botlnck + a = n; + c = 1; + while (a != 1) { + a = COLLATZ(a); + c++; + } + if (c > m) { + m = c; + z = n; + } + } + fprintf(stdout, "\rkonec. najdaljša veriga se začne z %lu in ima %lu elementov.\n", z, m); + return 0; +} |