;;; portal-prime.lsp — portal a primality test between machines
;;;
;;; Machine A: python3 lumbda.py --fast examples/portal-prime.lsp
;;;   (starts computing, saves checkpoint to prime-state.portal)
;;;
;;; Machine B: python3 lumbda.py --portal-resume prime-state.portal
;;;   (resumes from checkpoint, finishes the computation)

(define (prime? n)
  (display "Testing if ") (display n) (display " is prime...") (newline)
  (let loop ((i 2) (checks 0))
    (cond
      ((> (* i i) n)
       (display "  checked ") (display checks) (display " divisors") (newline)
       #t)
      ((= (remainder n i) 0)
       (display "  found divisor: ") (display i) (newline)
       #f)
      (else
       (when (= (remainder i 10000) 0)
         (display "  checkpoint at i=") (display i) (newline)
         (portal-checkpoint! "prime-state.portal"))
       (loop (+ i 1) (+ checks 1))))))

(define result (prime? 1000000007))
(display "Result: ") (display result) (newline)
