gcd_difference

bruteforce for https://oeis.org/A199768
git clone https://a3nm.net/git/gcd_difference/
Log | Files | Refs

commit c2328312eada1d6fc7f266ed23bd60aea55299b0
Author: Antoine Amarilli <a3nm@a3nm.net>
Date:   Sun, 15 Jan 2017 21:56:15 +0100

explain

Diffstat:
main.c | 44++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 44 insertions(+), 0 deletions(-)

diff --git a/main.c b/main.c @@ -0,0 +1,44 @@ +#include<stdio.h> +#include<stdbool.h> +#include<stdlib.h> + +// bruteforce https://oeis.org/A213918 + +int res[10]; +int num = 0; + +bool ok(int i, int found) { + for (int j = 0; j < found; j++) { + if (res[j] % (i - res[j])) + return false; + } + return true; +} + +bool search(int ci, int maxi, int found, int tofind) { + if (found == tofind) + return true; + for (int i = ci+1; i < maxi; i++) { + if (!ok(i, found)) + continue; + res[found] = i; + if (search(i, maxi, found+1, tofind)) + return true; + } + return false; +} + +int main(int argc, char **argv) { + int k = atoi(argv[1]); + int n = atoi(argv[2]); + res[0] = 0; + if (search(0, k, 1, n)) { + printf("solution found:\n"); + for (int i = 0; i < n; i++) + printf("%d ", res[i]); + printf("\n"); + } else { + printf("nothing found :(\n"); + } +} +