# Fortran code snippets

**URL:** <https://fortran-lang.discourse.group/t/fortran-code-snippets/2150>\
**Category:** Uncategorized\
**Created:** [October 24, 2021, 1:39pm UTC](https://fortran-lang.discourse.group/t/fortran-code-snippets/2150 "2021-10-24T13:39:39Z")\
**Posts on this page:** 1\
**Showing post:** 27

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [August 1, 2023, 1:14am UTC](https://fortran-lang.discourse.group/t/fortran-code-snippets/2150/27 "2023-08-01T01:14:17Z")

</div>

Here is a program that computed 1000 digits of pi in 0.03s on my PC in [14 lines of FORTRAN](http://old.stanleyrabinowitz.com/bibliography/piAbstract.pdf):

```auto
      integer vect(3350), buffer(201)
      data vect/3350*2/, more/0/
      DO 2 n=1, 201
         karray = 0
         DO 3 L=3350, 1, -1
            num = 100000*vect(L) + karry*L
            karry = num/(2*L-1)
3 vect(L) = num - karry*(2*L-1)
         k = karry/100000
         buffer(n) = more+k
2 more = karry - k*100000
      write(*,100) buffer
100 format(1x,I1,'.'/(1x,10I5.5))
      end

```

or Fortran (using a variable `m` instead of `l`)

```auto
implicit none
integer vect(3350), buffer(201), m, n, karry, k, more, num
vect = 2
more = 0
do n=1, 201
   karry = 0
   do m=3350, 1, -1
      num = 100000*vect(m) + karry*m
      karry = num/(2*m-1)
      vect(m) = num - karry*(2*m-1)
   end do
   k = karry/100000
   buffer(n) = more + k
   more = karry - k*100000
end do
write (*,"(1x,I1,'.'/(1x,10I5.5))") buffer
end

```

with the theory explained in

[A Spigot Algorithm for the Digits of π](https://www.maa.org/sites/default/files/pdf/pubs/amm_supplements/Monthly_Reference_12.pdf)  
by Stanley Rabinowitz and Stan Wagon  
The American Mathematical Monthly, Vol. 102, No. 3 (Mar., 1995), pp. 195-203

giving output

```auto
 3.
 14159265358979323846264338327950288419716939937510
 58209749445923078164062862089986280348253421170679
 82148086513282306647093844609550582231725359408128
 48111745028410270193852110555964462294895493038196
 44288109756659334461284756482337867831652712019091
 45648566923460348610454326648213393607260249141273
 72458700660631558817488152092096282925409171536436
 78925903600113305305488204665213841469519415116094
 33057270365759591953092186117381932611793105118548
 07446237996274956735188575272489122793818301194912
 98336733624406566430860213949463952247371907021798
 60943702770539217176293176752384674818467669405132
 00056812714526356082778577134275778960917363717872
 14684409012249534301465495853710507922796892589235
 42019956112129021960864034418159813629774771309960
 51870721134999999837297804995105973173281609631859
 50244594553469083026425223082533446850352619311881
 71010003137838752886587533208381420617177669147303
 59825349042875546873115956286388235378759375195778
 18577805321712268066130019278766111959092164201989

```

---

_[View the full topic](https://fortran-lang.discourse.group/t/fortran-code-snippets/2150)._
