Download code

Jump to: navigation, search

Back to Pi_with_Machin's_formula_(Lisp)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

pi.lisp

 1 ; The authors of this work have released all rights to it and placed it
 2 ; in the public domain under the Creative Commons CC0 1.0 waiver
 3 ; (http://creativecommons.org/publicdomain/zero/1.0/).
 4 ; 
 5 ; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 ; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 ; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 ; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 ; CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 ; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 ; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 ; 
13 ; Retrieved from: http://en.literateprograms.org/Pi_with_Machin's_formula_(Lisp)?oldid=2756
14 
15 (defun arccot (x unity)
16   (let ((xpower (floor (/ unity x))))
17     (arccot-plus-helper (* x x) 1 xpower)))
18 
19 (defun arccot-plus-helper (xsq n xpower)
20   (let ((term (floor (/ xpower n))))
21     (if (= term 0)
22         0
23       (+ (arccot-minus-helper xsq (+ n 2) (floor (/ xpower xsq)))
24          term))))
25 
26 (defun arccot-minus-helper (xsq n xpower)
27   (let ((term (floor (/ xpower n))))
28     (if (= term 0)
29         0
30       (- (arccot-plus-helper xsq (+ n 2) (floor (/ xpower xsq)))
31          term))))
32 
33 
34 (defun pidigits (digits)
35     (let* ((unity (expt 10 (+ digits 10)))
36            (pi (* 4 (- (* 4 (arccot 5 unity)) (arccot 239 unity)))))
37       (floor (/ pi (expt 10 10)))))


hijacker
hijacker
hijacker
hijacker