add a few more tools. most useful: round.c
[tools.git] / math / ordnung.c
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;
+}