Advertisement
pintcat

make-pi - Experimental approach to calculate Pi with up to 15 decimal places using Euler method

Aug 28th, 2022 (edited)
1,473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.74 KB | Science | 0 0
  1. #!/bin/sh
  2.  
  3. BASE=4
  4. RES=0
  5. RES_PREV=0
  6. ROUND=3    # number of digits used for rounding
  7. PREC=1     # holds number of iterations with identical results bofore exiting
  8. X=2
  9.  
  10. if [ -n "${1##*[!0-9]*}" ] && [ $1 -le 15 ] && [ $1 -gt 0 ]; then
  11.     DIGIT=$(($1+$ROUND))
  12. else
  13.     DIGIT=18
  14. fi
  15.  
  16. while [ $((DIGIT=$DIGIT-1)) -ge 0 ]; do
  17.     BASE=$(($BASE*10))
  18. done
  19. ROUND=$(printf 1'%'$ROUND's' | tr ' ' 0)
  20. while [ $PREC -ge 0 ]; do
  21.     RES=$(($RES+($BASE/($X*$((X=$X+1))*$((X=$X+1))))-($BASE/($X*$((X=$X+1))*$((X=$X+1))))))
  22.     if [ $RES_PREV -eq $RES ]; then
  23.         PREC=$(($PREC-1))
  24.     else
  25.         ITERATION=$(($ITERATION+1))
  26.         RES_PREV=$RES
  27.         PREC=$(($ITERATION/10+10))
  28.         printf "\r3."$((($RES+$ROUND*10/18)/$ROUND))
  29.     fi
  30. done
  31. printf "\nUsed iterations: $ITERATION\n"
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement