Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 2.64 KB | None | 0 0
  1. #-sbcl (error "This only works using SBCL")
  2. (declaim (optimize (speed 3)
  3.                    (debug 0)
  4.                    (safety 0)))
  5.  
  6. (defmacro gen-arccot (sign opposite)
  7.   `(,(intern (format nil "~a~a" 'arccot sign)) (x-rest n x-elevate product)
  8.       "Arccotangent calculator, uses"
  9.       (declare (type integer x-elevate x-rest n product))
  10.       (let ((term (floor x-elevate n)))
  11.         (if (zerop term)
  12.             product
  13.           (,(intern (format nil "~a~a" 'arccot opposite)) x-rest (+ n 2)
  14.              (floor x-elevate x-rest)
  15.              (,(intern (format nil "~a" sign)) product term ))))))
  16.  
  17.  
  18. (defun pidigits (x-digits)
  19.   "Calculate x-digits of pi using Machin's formula"
  20.   (labels ((gen-arccot + -)
  21.            (gen-arccot - +))
  22.     (let* ((limit (expt 10 (+ 10 x-digits)))
  23.            (first-thread (sb-thread:make-thread
  24.                           (lambda () (* 44 (the bignum (arccot+ 3249 1 (the bignum (floor limit 57)) 1))))))
  25.            (second-thread (sb-thread:make-thread
  26.                            (lambda () (* 7 (the bignum (arccot+ 57121 1 (the bignum (floor limit 239)) 1))))))
  27.            (third-thread (sb-thread:make-thread
  28.                           (lambda () (* -12 (the bignum (arccot+ 465124 1 (the bignum (floor limit 682)) 1))))))
  29.            (fourth-thread (sb-thread:make-thread
  30.                            (lambda () (* 24 (the bignum (arccot+ 167521249 1 (the bignum (floor limit 12943)) 1)))))))
  31.       (declare (type bignum limit))
  32.       (sb-thread:thread-yield)
  33.       (format nil "3.~a" (the bignum (rem
  34.                                       (the bignum (floor
  35.                                                    (the bignum (* 4  
  36.                                                                   (the bignum (+
  37.                                                                                (the bignum (sb-thread:join-thread fourth-thread))
  38.                                                                                (the bignum (sb-thread:join-thread third-thread))
  39.                                                                                (the bignum (sb-thread:join-thread second-thread))
  40.                                                                                (the bignum (sb-thread:join-thread first-thread))))))
  41.                                                    10000000000))
  42.                                       (/ limit 10000000000)))))))
  43.  
  44.  
  45. (if (not (cadr *posix-argv*))
  46.   (format t "Error: you should pass an argument~%")
  47.   (let ((n (parse-integer (cadr *posix-argv*))))
  48.     (if (not (typep n 'integer))
  49.       (print "The argument should be a integer~%")
  50.       (print (pidigits n)))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement