Arithmetic-geometric mean/Calculate Pi: Difference between revisions

m
Automated syntax highlighting fixup (second round - minor fixes)
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 1:
[[Category:Geometry]]
{{task}}
[http://www.maa.org/sites/default/files/pdf/upload_library/22/Ford/Almkvist-Berndt585-608.pdf Almkvist Berndt 1988] begins with an investigation of why the agm is such an efficient algorithm, and proves that it converges quadratically. This is an efficient method to calculate <math>\pi</math>.
Line 22 ⟶ 23:
<!-- Example by Nigel Galloway, Feb 8, 2012 -->
{{libheader|GMP}}
<syntaxhighlight lang="c">#include "gmp.h"
 
void agm (const mpf_t in1, const mpf_t in2, mpf_t out1, mpf_t out2) {
Line 74 ⟶ 75:
 
=={{header|C sharp|C#}}==
{{libheader|System.Numerics}}Can specify the number of desired digits on the command line, default is 25000, which takes a few seconds (depending on your system's performance).<syntaxhighlight lang="csharp">using System;
using System.Numerics;
 
Line 140 ⟶ 141:
{{trans|C}}
{{libheader|GMP}}
<syntaxhighlight lang="cpp">#include <gmpxx.h>
#include <chrono>
 
Line 190 ⟶ 191:
=={{header|Clojure}}==
{{trans|Ruby}}
<syntaxhighlight lang="lisp">(ns async-example.core
(:use [criterium.core])
(:gen-class))
Line 266 ⟶ 267:
{{libheader|MMA}}
<p>This is an example that uses the Common Lisp Bigfloat Package (http://www.cs.berkeley.edu/~fateman/lisp/mma4max/more/bf.lisp)</p>
<syntaxhighlight lang="lisp">(load "bf.fasl")
 
;;(setf mma::bigfloat-bin-prec 1000)
Line 289 ⟶ 290:
=={{header|D}}==
{{trans|C#}}
<syntaxhighlight lang="d">import std.bigint;
import std.conv;
import std.math;
Line 372 ⟶ 373:
Thanks for Velthuis BigIntegers library[https://github.com/rvelthuis/DelphiBigNumbers].
{{Trans|C#}}
<syntaxhighlight lang=Delphi"delphi">
program Calculate_Pi;
 
Line 490 ⟶ 491:
=={{header|Erlang}}==
{{trans|python}}
<syntaxhighlight lang="erlang">
-module(pi).
-export([agmPi/1, agmPiBody/5]).
Line 522 ⟶ 523:
{{libheader|iso_fortran_env}}
{{trans|Julia}}
<syntaxhighlight lang="fortran">program CalcPi
! Use real128 numbers: (append '_rf')
use iso_fortran_env, only: rf => real128
Line 571 ⟶ 572:
 
=={{header|Go}}==
<syntaxhighlight lang="go">package main
 
import (
Line 618 ⟶ 619:
=={{header|Groovy}}==
{{trans|Java}}
<syntaxhighlight lang="groovy">import java.math.MathContext
 
class CalculatePi {
Line 658 ⟶ 659:
{{libheader|MPFR}}
{{libheader|hmpfr}}
<syntaxhighlight lang=Haskell"haskell">import Prelude hiding (pi)
import Data.Number.MPFR hiding (sqrt, pi, div)
import Data.Number.MPFR.Instances.Near ()
Line 698 ⟶ 699:
[https://code.jsoftware.com/wiki/Essays/Extended%20Precision%20Functions Extended precision functions] and [https://code.jsoftware.com/wiki/Essays/Chudnovsky_Algorithm Pi].
Translated from python:
<syntaxhighlight lang=J"j">DP=: 100
 
round=: DP&$: : (4 : 0)
Line 745 ⟶ 746:
1583455951826865080542496790424338362837978447536228171662934224565463064033895909488933268392567279887495006936541219489670405121434573776487989539520749180843985094860051126840117004097133550161882511486508109869673199973040182062140382647367514024790194...</pre>
 
That said, note that J offers a more direct approach here:<syntaxhighlight lang=J"j"> 102j100":<.@o.&.(*&(10^100x))1
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679</syntaxhighlight>
 
However, a limitation of this approach is that the floor function -- which we use here to signal the desire for an exact approximation of pi -- does not round. But we can ask for extra digits, for example:<syntaxhighlight lang=J"j"> 113j111":<.@o.&.(*&(10^111x))1
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513</syntaxhighlight>
 
Line 756 ⟶ 757:
{{trans|Kotlin}}
Used features of Java 8
<syntaxhighlight lang=Java"java">import java.math.BigDecimal;
import java.math.MathContext;
import java.util.Objects;
Line 803 ⟶ 804:
`rsqrt` function
for taking square roots of rationals; such a module can be found at [[Arithmetic/Rational]].
<syntaxhighlight lang="jq"># include "rational"; # a reminder
 
def pi(precision):
Line 830 ⟶ 831:
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">using Printf
 
agm1step(x, y) = (x + y) / 2, sqrt(x * y)
Line 879 ⟶ 880:
 
=={{header|Kotlin}}==
<syntaxhighlight lang="scala">import java.math.BigDecimal
import java.math.MathContext
 
Line 919 ⟶ 920:
With 16 steps we have 89415 correct digits:
 
<syntaxhighlight lang="maple">agm:=proc(n)
local a:=1,g:=evalf(sqrt(1/2)),s:=0,p:=4,i;
for i to n do
Line 935 ⟶ 936:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">pi[n_, prec_] :=
Module[{a = 1, g = N[1/Sqrt[2], prec], k, s = 0, p = 4},
For[k = 1, k < n, k++,
Line 950 ⟶ 951:
=={{header|МК-61/52}}==
{{Output?}}
<syntaxhighlight lang="text">3 П0 1 П1 П4 2 КвКор 1/x П2 1
^ 4 / П3 ИП3 ИП1 ИП2 + 2 /
П5 ИП1 - x^2 ИП4 * - П3 ИП1 ИП2
Line 959 ⟶ 960:
{{libheader|bignum}}
{{Trans|Delphy}}
<syntaxhighlight lang=Nim"nim">from math import sqrt
import times
 
Line 1,057 ⟶ 1,058:
=={{header|OCaml}}==
program for calculating digits of pi
<syntaxhighlight lang=OCaml"ocaml">let limit = 10000 and n = 2800
let x = Array.make (n+1) 2000
 
Line 1,080 ⟶ 1,081:
 
=={{header|PARI/GP}}==
<syntaxhighlight lang="parigp">pi(n)=my(a=1,g=2^-.5);(1-2*sum(k=1,n,[a,g]=[(a+g)/2,sqrt(a*g)];(a^2-g^2)<<k))^-1*4*a^2
pi(6)</syntaxhighlight>
{{out}}
Line 1,090 ⟶ 1,091:
The number of steps used is based on the desired accuracy rather than being hard coded, as this is intended to work for 1M digits as well as for 100.
 
<syntaxhighlight lang="perl">use Math::BigFloat try => "GMP,Pari";
 
my $digits = shift || 100; # Get number of digits from command line
Line 1,117 ⟶ 1,118:
The following is a translation, almost line-for-line, of the Ruby code. It is slower than the above and the last digit or two may not be correct.
{{trans|Ruby}}
<syntaxhighlight lang="perl">use strict;
use warnings;
use Math::BigFloat;
Line 1,140 ⟶ 1,141:
{{libheader|Phix/mpfr}}
{{trans|Python}}
<!--<syntaxhighlight lang=Phix"phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1.0.0"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (mpfr_set_default_prec[ision] has been renamed)</span>
Line 1,204 ⟶ 1,205:
=={{header|PicoLisp}}==
{{trans|Python}}
<syntaxhighlight lang=PicoLisp"picolisp">(scl 40)
 
(de pi ()
Line 1,230 ⟶ 1,231:
=={{header|Python}}==
{{trans|Ruby}}
<syntaxhighlight lang="python">from decimal import *
 
D = Decimal
Line 1,251 ⟶ 1,252:
{{libheader|Rmpfr}}
 
<syntaxhighlight lang="rsplus">library(Rmpfr)
 
agm <- function(n, prec) {
Line 1,287 ⟶ 1,288:
=={{header|Racket}}==
{{trans|Ruby}}
<syntaxhighlight lang=Racket"racket">#lang racket
(require math/bigfloat)
 
Line 1,334 ⟶ 1,335:
 
Notice that we don't get the exact number of decimals required : the last two decimals or so can be wrong. This is because we don't need <math>a_n</math>, but rather <math>a_n^2</math>. Elevating to the square makes us lose a bit of precision. It could be compensated by choosing a slightly higher value of N (in a way that could be precisely calculated), but that would probably be overkill.
<syntaxhighlight lang=perl6"raku" line>constant number-of-decimals = 100;
 
multi sqrt(Int $n) {
Line 1,381 ⟶ 1,382:
Whatever number of digits used, the actual number of digits is five larger than specified, and then the result is rounded to the requested number of digits.
===version 1===
<syntaxhighlight lang="rexx">/*REXX program calculates the value of pi using the AGM algorithm. */
parse arg d .; if d=='' | d=="," then d= 500 /*D not specified? Then use default. */
numeric digits d+5 /*set the numeric decimal digits to D+5*/
Line 1,413 ⟶ 1,414:
 
For 1,005 decimal digits, &nbsp; it is over &nbsp; '''68''' &nbsp; times faster than version 3.
<syntaxhighlight lang="rexx">/*REXX program calculates the AGM (arithmetic─geometric mean) of two (real) numbers. */
parse arg a b digs . /*obtain optional numbers from the C.L.*/
if digs=='' | digs=="," then digs= 100 /*No DIGS specified? Then use default.*/
Line 1,480 ⟶ 1,481:
 
===version 3===
<syntaxhighlight lang="rexx">
/*REXX*/
Line 1,546 ⟶ 1,547:
Using agm.
See [[Talk:Arithmetic-geometric mean]]
<syntaxhighlight lang="ruby"># Calculate Pi using the Arithmetic Geometric Mean of 1 and 1/sqrt(2)
#
#
Line 1,602 ⟶ 1,603:
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">/// calculate pi with algebraic/geometric mean
pub fn pi(n: usize) -> f64 {
let mut a : f64 = 1.0;
Line 1,625 ⟶ 1,626:
</syntaxhighlight>
Can be invoked like:
<syntaxhighlight lang="rust">
fn main() {
println!("pi(7): {}", pi(7));
Line 1,637 ⟶ 1,638:
=={{header|Scala}}==
===Completely (tail) recursive===
<syntaxhighlight lang=Scala"scala">import java.math.MathContext
 
import scala.annotation.tailrec
Line 1,676 ⟶ 1,677:
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func agm_pi(digits) {
var acc = (digits + 8);
 
Line 1,705 ⟶ 1,706:
{{trans|Ruby}}
{{tcllib|math::bigfloat}}
<syntaxhighlight lang="tcl">package require math::bigfloat
namespace import math::bigfloat::*
 
Line 1,735 ⟶ 1,736:
{{trans|C#}}
{{Libheader|System.Numerics}}
<syntaxhighlight lang="vbnet">Imports System, System.Numerics
 
Module Program
Line 1,797 ⟶ 1,798:
{{trans|Sidef}}
{{libheader|Wren-big}}
<syntaxhighlight lang="ecmascript">import "/big" for BigRat
 
var digits = 500
Line 1,819 ⟶ 1,820:
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912
</pre>
 
[[Category:Geometry]]
10,327

edits