Advertisement
Guest User

Untitled

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