Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- BASE=4
- RES=0
- RES_PREV=0
- ROUND=3 # number of digits used for rounding
- PREC=1 # holds number of iterations with identical results bofore exiting
- X=2
- if [ -n "${1##*[!0-9]*}" ] && [ $1 -le 15 ] && [ $1 -gt 0 ]; then
- DIGIT=$(($1+$ROUND))
- else
- DIGIT=18
- fi
- while [ $((DIGIT=$DIGIT-1)) -ge 0 ]; do
- BASE=$(($BASE*10))
- done
- ROUND=$(printf 1'%'$ROUND's' | tr ' ' 0)
- while [ $PREC -ge 0 ]; do
- RES=$(($RES+($BASE/($X*$((X=$X+1))*$((X=$X+1))))-($BASE/($X*$((X=$X+1))*$((X=$X+1))))))
- if [ $RES_PREV -eq $RES ]; then
- PREC=$(($PREC-1))
- else
- ITERATION=$(($ITERATION+1))
- RES_PREV=$RES
- PREC=$(($ITERATION/10+10))
- printf "\r3."$((($RES+$ROUND*10/18)/$ROUND))
- fi
- done
- printf "\nUsed iterations: $ITERATION\n"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement