add a few more tools. most useful: round.c
authorJann Horn <jann@thejh.net>
Sat, 26 Apr 2014 21:18:43 +0000 (23:18 +0200)
committerJann Horn <jann@thejh.net>
Sat, 26 Apr 2014 21:18:43 +0000 (23:18 +0200)
math/disclogbrute.c [new file with mode: 0644]
math/ordnung.c [new file with mode: 0644]
math/round.c [new file with mode: 0644]
tools/writeonlynet.c [new file with mode: 0644]

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;
+}
diff --git a/math/ordnung.c b/math/ordnung.c
new file mode 100644 (file)
index 0000000..c49e893
--- /dev/null
@@ -0,0 +1,18 @@
+#include <stdlib.h>
+#include <stdio.h>
+
+int main(int argc, char **argv) {
+  if (argc != 3) puts("invocation: <p> <a>"), exit(1);
+  int p = atoi(argv[1]);
+  int a = atoi(argv[2]);
+
+  int n = a;
+  int order = 1;
+  while (n != 1) {
+    n = (((long long)n) * a) % p;
+    order++;
+  }
+
+  printf("Die Ordnung ist %d.%s\n", order, (order==p-1)?" Primitives Element":"");
+  return 0;
+}
diff --git a/math/round.c b/math/round.c
new file mode 100644 (file)
index 0000000..e422fd0
--- /dev/null
@@ -0,0 +1,16 @@
+#include <jh.h>
+#include <math.h>
+
+int main(int argc, char **argv) {
+  if (argc != 2) xperror("invocation: round <target>", 0);
+  double target = strtod(argv[1], NULL);
+  char line[128];
+  while (fgets(line, sizeof(line), stdin)) {
+    double n = strtod(line, NULL);
+    double quot = n / target;
+    quot = round(quot);
+    n = quot * target;
+    printf("%f\n", n);
+  }
+  return 0;
+}
diff --git a/tools/writeonlynet.c b/tools/writeonlynet.c
new file mode 100644 (file)
index 0000000..3bef9f9
--- /dev/null
@@ -0,0 +1,27 @@
+#include <jh.h>
+#include <unistd.h>
+
+int main(int argc, char **argv) {
+  if (argc != 3) xperror("invocation: <host> <port>", 0);
+  int s = netopen(argv[1], argv[2], JH_TCP_HINTS);
+  if (s < 0) {
+    fprintf(stderr, "unable to connect: %s\n", gai_strerror(s));
+    exit(1);
+  }
+  while (1) {
+    char buf[4096];
+    fprintf(stderr, "reading...\n");
+    ssize_t count = fail_on_neg(read(0, buf, sizeof(buf)), "read failed", 1);
+    if (count == 0) return 0;
+    fprintf(stderr, "read %zd chars, writing...\n", count);
+    ssize_t wcount = fail_on_neg(write(s, buf, count), "write failed", 1);
+    if (wcount == 0) {
+      fprintf(stderr, "zero write! ending.\n");
+      return 1;
+    }
+    if (wcount != count) {
+      fprintf(stderr, "wrong write size! ending.\n");
+      return 1;
+    }
+  }
+}