diff options
| author | Abigail <abigail@abigail.freedom.nl> | 2022-01-11 19:04:33 +0100 |
|---|---|---|
| committer | Abigail <abigail@abigail.freedom.nl> | 2022-01-11 19:04:33 +0100 |
| commit | 46a9f55050cb852410da9c80c7e248d8df0328a8 (patch) | |
| tree | 4c909ce6fcb5052106da5741f0b00e6cabdbe396 | |
| parent | 95d45c5753b63b7656a434973453dae12e227a6a (diff) | |
| download | perlweeklychallenge-club-46a9f55050cb852410da9c80c7e248d8df0328a8.tar.gz perlweeklychallenge-club-46a9f55050cb852410da9c80c7e248d8df0328a8.tar.bz2 perlweeklychallenge-club-46a9f55050cb852410da9c80c7e248d8df0328a8.zip | |
Week 147: C solutions
| -rw-r--r-- | challenge-147/abigail/c/ch-1.c | 70 | ||||
| -rw-r--r-- | challenge-147/abigail/c/ch-2.c | 66 |
2 files changed, 136 insertions, 0 deletions
diff --git a/challenge-147/abigail/c/ch-1.c b/challenge-147/abigail/c/ch-1.c new file mode 100644 index 0000000000..d02433f475 --- /dev/null +++ b/challenge-147/abigail/c/ch-1.c @@ -0,0 +1,70 @@ +# include <stdlib.h> +# include <stdio.h> +# include <string.h> +# include <stdbool.h> + +/* + * See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 + */ + +/* + * Run as: cc -o ch-1.o ch-1.c; ./ch-1.o + */ +# define COUNT 20 + +bool is_prime (int n) { + if (n % 2 == 0) {return n == 2;} + for (int i = 3; i * i <= n; i += 2) { + if (n % i == 0) {return false;} + } + return true; +} + +int main (void) { + int * todo; + if ((todo = (int *) malloc (4 * sizeof (int))) == NULL) { + perror ("Malloc failed"); + return (1); + } + todo [0] = 2; + todo [1] = 3; + todo [2] = 5; + todo [3] = 7; + size_t high = 4; + + for (size_t i = 0; i < high; i ++) { + printf ("%d ", todo [i]); + } + + int count = COUNT - high; + + int pow = 10; + while (count > 0) { + int * next; + int next_high = 0; + if ((next = (int *) malloc (9 * high * sizeof (int))) == NULL) { + perror ("Malloc failed"); + return (1); + } + for (int d = 1; d <= 9 && count > 0; d ++) { + for (size_t i = 0; i < high && count > 0; i ++) { + int candidate = d * pow + todo [i]; + if (is_prime (candidate)) { + next [next_high ++] = candidate; + printf ("%d ", candidate); + count --; + } + } + } + if (!next_high) { + break; + } + free (todo); + todo = next; + high = next_high; + pow *= 10; + } + free (todo); + + printf ("\n"); +} diff --git a/challenge-147/abigail/c/ch-2.c b/challenge-147/abigail/c/ch-2.c new file mode 100644 index 0000000000..02ff39bd1e --- /dev/null +++ b/challenge-147/abigail/c/ch-2.c @@ -0,0 +1,66 @@ +# include <stdlib.h> +# include <stdio.h> +# include <string.h> +# include <stdbool.h> + +/* + * See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 + */ + +/* + * Run as: cc -o ch-2.o ch-2.c; ./ch-2.o + */ + +bool is_pentagonal (int candidate, int * pentagon, size_t max) { + size_t low = 0; + size_t high = max; + while (low < high) { + size_t mid = (low + high) / 2; + if (pentagon [mid] == candidate) {return true;} + if (pentagon [mid] < candidate) {low = mid + 1;} + if (pentagon [mid] > candidate) {high = mid;} + } + return false; +} + + +int main (void) { + int * pentagon = NULL; + int n = 0; + int p = 0; + size_t cap = 100; + size_t high = 0; + bool done = false; + + if ((pentagon = (int *) malloc (cap * sizeof (int))) == NULL) { + perror ("Malloc failed"); + return (1); + } + + while (!done) { + p += n + n + n + 1; + n ++; + if (high >= cap) { + cap *= 2; + if ((pentagon = + (int *) realloc (pentagon, cap * sizeof (int))) == NULL) { + perror ("Realloc failed"); + return (1); + } + } + pentagon [high ++] = p; + + for (size_t i = 0; i < high && + pentagon [i] + pentagon [i] < p && + !done; i ++) { + int seen = pentagon [i]; + if (is_pentagonal (p - seen, pentagon, high) && + is_pentagonal (p - seen - seen, pentagon, high)) { + printf ("%d %d\n", seen, p - seen); + done = true; + } + } + } + + return (0); +} |
