lisp-random / theoros.lisp

;;; An experimental test to see if the solutions are correct
;;; Recurrences from THEOROS

(defun test (m n x0 y0)
  (labels (
           ;; Recurrences
           (x (k)
             (if (zerop k) x0
                 (+ (* (x (1- k))
                       (- 1 m))
                    (* n (y (1- k))))))
           (y (k)
             (if (zerop k) y0
                 (+ (* (y (1- k))
                       (- 1 n))
                    (* m (x (1- k))))))
           ;; Solutions
           (x-sol (k)
             (/ (+ (* n (+ x0 y0))
                   (* (expt (- 1 m n) k)
                      (- (* m x0)
                         (* n y0))))
                (+ m n)))
           (y-sol (k)
             (/ (+ (* m (+ x0 y0))
                   (* (expt (- 1 m n) k)
                      (- (* n y0)
                         (* m x0))))
                (+ m n))))
    (dotimes (k 10 t)
      (let* ((delta-x (- (x k) (x-sol k)))
             (delta-y (- (y k) (y-sol k))))
        (assert (and (zerop delta-x)
                     (zerop delta-y))
                "Failed with k=~A, dX=~A, dY=~A" k delta-x delta-y)))))

(defun do-test (&optional (trials 1000))
  (dotimes (i trials t)
    (test (1+ (random most-positive-fixnum))
          (1+ (random most-positive-fixnum))
          (1+ (random most-positive-fixnum))
          (1+ (random most-positive-fixnum)))))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.