Advertisement
pintcat

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

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