Advertisement
pintcat

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

Aug 28th, 2022 (edited)
1,459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.77 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_DEF=200000    # number of iterations with identical results bofore exiting
  8. X=2
  9.  
  10. if [ -n "${1##*[!0-9]*}" ] && [ $1 -le 15 ]; 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. PREC=$PREC_DEF
  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.         COUNT=0
  26.         ITERATION=$(($ITERATION+1))
  27.         RES_PREV=$RES
  28.         RES_FIN=$RES
  29.         PREC=$PREC_DEF
  30.         while [ $((COUNT=$COUNT+1)) -le $ROUND ]; do RES_FIN=$((($RES_FIN+5)/10)); done
  31.         printf "\r3.$RES_FIN"
  32.     fi
  33. done
  34. printf "\nUsed iterations: $ITERATION\n"
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement