about summary refs log tree commit diff
path: root/tht/C/TP-2018/func.scm
diff options
context:
space:
mode:
Diffstat (limited to 'tht/C/TP-2018/func.scm')
-rw-r--r--tht/C/TP-2018/func.scm35
1 files changed, 35 insertions, 0 deletions
diff --git a/tht/C/TP-2018/func.scm b/tht/C/TP-2018/func.scm
new file mode 100644
index 0000000..a7d0af5
--- /dev/null
+++ b/tht/C/TP-2018/func.scm
@@ -0,0 +1,35 @@
+(define (func k r p q m)
+  (define (func-iter a b c d count)
+    (display (cons a b))
+    (newline)
+    (cond ((= count 0) (cons (remainder b m)
+                             (remainder a m)))
+          ((even? count) (func-iter (remainder a m)
+                                    (remainder b m)
+                                    (remainder (+ (* c c) (* d d)) m)
+                                    (remainder (+ (* 2 c d) (* d d r)) m)
+                                    (/ count 2)))
+          (else (func-iter (remainder (+ (* b d) (* a d r) (* a c)) m)
+                           (remainder (+ (* b c) (* a d)) m)
+                           (remainder c m)
+                           (remainder d m)
+                           (- count 1)))))
+  (let ((g (gcd p q)))
+    (func-iter (/ q g) (/ p g) 0 1 (- k 1))))
+
+(define (iter)
+  (let* ((k (read))
+         (r (read))
+         (p (read))
+         (q (read))
+         (m (read)))
+    (if (not (eof-object? m))
+        (let ((c (func k r p q m)))
+          (display (car c))
+          (display " ")
+          (display (cdr c))
+          (newline)
+          (iter)))))
+
+(with-input-from-file "FUNC.INP"
+  (lambda () (with-output-to-file "FUNC.OUT" iter)))