Geothmetic Meandian
April 13, 2021
These are straight forward:
(define (agm x y) (let loop ((a (max x y)) (g (min x y))) (if (< (/ a g) 1.00000001) (/ (+ a g) 2) (loop (/ (+ a g) 2) (sqrt (* a g))))))
(define (gm . xs) (define (median3 a b c) (cond ((or (<= b a c) (<= c a b)) a) ((or (<= a b c) (<= c b a)) b) (else c))) (let* ((len (length xs)) (a (/ (sum xs) len)) (g (expt (apply * xs) (/ len))) (m (list-ref (sort < xs) (quotient len 2)))) (let loop ((a a) (g g) (m m)) (if (and (< (/ a g) 1.00000001) (< (/ a m) 1.00000001) (< (/ g m) 1.00000001)) (/ (+ a g) 2) (loop (/ (+ a g m) 3) (expt (* a g m) 1/3) (median3 a g m))))))
You can run the programs at http://ideone.com/Z2ZZho.
Fun little drill. I’m not sure about the usefulness of these centrality metrics, but it’s good practice though! Here is my take on this drill using Julia 1.6: https://pastebin.com/sj1X0G0H
You can run it at: https://ideone.com/8s3uh4
Here’s a solution in Python, utilizing the
statistics
module added to the standard library in Python 3.4.Output:
Here’s another Python solution, which doesn’t rely on the
statistics
module.Output:
I have written the code of the following problem in Fortran using subroutine, so that it can be used for finding the solution of other inputted numbers also
<a href=”https://pastebin.com/ZQ7inPa4”>
Here’s a Haskell version. I used this as an excuse to play around with the foldl library.