add a few more tools. most useful: round.c
[tools.git] / math / disclogbrute.c
diff --git a/math/disclogbrute.c b/math/disclogbrute.c
new file mode 100644 (file)
index 0000000..b297725
--- /dev/null
@@ -0,0 +1,21 @@
+#include <stdlib.h>
+#include <stdio.h>
+
+int main(int argc, char **argv) {
+  if (argc != 4) puts("bad invocation"), exit(1);
+  int p = atoi(argv[1]);
+  int a = atoi(argv[2]);
+  int b = atoi(argv[3]);
+
+  int candidate_b = 1;
+  for (int candidate_log = 0; candidate_log < p; candidate_log++) {
+    if (candidate_b == b) {
+      printf("solution: discrete log is %d\n", candidate_log);
+      exit(0);
+    }
+    candidate_b = (candidate_b * a) % p;
+  }
+
+  printf("no hits???\n");
+  return 1;
+}