PI day.

 
Thread Tools Search this Thread
Operating Systems OS X (Apple) PI day.
# 1  
Old 03-18-2018
PI day.

Hi guys and gals...
OSX 10.13.3, default bash terminal calling ksh in the code.
I decided to have a go at creating PI using ksh only as a bit of fun.
I also decided on a Taylor series to do such a task.
This was the fun bit of code that generates PI to 6 decimal places although it could easily be set to continuous running and calculate within the limits of ksh's floating point precision.
It takes around 35 seconds to calculate to six decimal places...
Code:
#!/bin/ksh
# !/usr/local/bin/dash
# PI using the Taylor series.
# PI/4 = 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11......

clear
OP="-"
BASE=3.0
VAL=1.0
N=1
MYPI=0.0
while true
do
	if [ $(( $N % 2 )) -eq 0 ]
	then
		OP="+"
	fi
	if [ $(( $N % 2 )) -gt 0 ]
	then
		OP="-"
	fi
	VAL=$(( ($VAL * 1.0) $OP (1.0 / $BASE) ))
	BASE=$(( $BASE + 2.0 ))
	N=$(( $N + 1 ))
	MYPI=$(( $VAL * 4.0 ))
	# Hard coded known value to exit loop without interaction.
	if [ "${MYPI:0:8}" == "3.141592" ]
	then
		break
	fi
done
printf "PI to six decimal places = %.6f...\nIterations = $N...\n" "$MYPI"

Result:
Code:
PI to six decimal places = 3.141592...
Iterations = 1530012...
AMIGA:amiga~/Desktop/Code/Shell> _

Enjoy and hope to see other solutions on this thread...

Last edited by wisecracker; 03-18-2018 at 07:54 AM..
# 2  
Old 03-18-2018
I had to do this in awk as I don't have ksh installed, but I think you get the gist... Usually, an iteration is exited if changes get below a defined bound which I here chose to be 10^-7...
Code:
awk -vOFMT="%.8f" '
BEGIN   {SGN = BS = 1

         do     {OLD  = PI4
                 PI4 += SGN/BS
                 SGN *= -1
                 BS  += 2
                 DLT  = OLD - PI4
                }
         while (DLT>0?DLT:-DLT > 1E-7)

         print PI4*4, 4*atan2(1,1), 22/7
        }
'
3.14159285 3.14159265 3.14285714

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Julian day to dates in YEAR-MONTH-DAY

hello, I have many files called day001, day002, day003 and I want to rename them by day20070101, day20070102, etc. I need to do it for several years and leap years as well. What is the best way to do it ? Thank you. (1 Reply)
Discussion started by: Ggg
1 Replies

2. UNIX for Dummies Questions & Answers

Move the files between Current day & a previous day

Hi All, I have a requirement where I need to first capture the current day & move all the files from a particular directory based on a previous day. i.e move all the files from one directory to another based on current day & a previous day. Here is what I am trying, but it gives me errors.... (2 Replies)
Discussion started by: dsfreddie
2 Replies

3. Shell Programming and Scripting

Script to check if last modified day is previous day

Hi, I would like to write a script that checks if a file ('counter') was modified the previous day, if so erase its contents and write 00000000 into it. For e.g. if the file 'counter' was last modified at 11.30pm on 24th May and the script runs at 12.15am of 25th May, it should erase it's... (1 Reply)
Discussion started by: hegdepras
1 Replies

4. Shell Programming and Scripting

Code creates day 32 instead of 1st day of next month.

I am using the code below modified from a post I saw here regarding having the script write out future dates. The problem is that instead of making 8/1 it makes 7/32! Please help! yy=`date +%Y` mm=`date +%m` dd=`date +%d` echo "Today is : $yy $mm $dd" #!/usr/bin/ksh date '+%m... (5 Replies)
Discussion started by: libertyforall
5 Replies

5. Solaris

May i know the day to day activities of a Solaris system administrator?

Recently i have attended a telephonic interview. As i dont have work experience in solaris i was not able to deliver correct answer for this question. Your answer will help for the people like me who is looking to become Solaris System administrator. (4 Replies)
Discussion started by: Sesha
4 Replies

6. Shell Programming and Scripting

Script to find previous month last day minus one day timestamp

Hi All, I need to find the previous month last day minus one day, using shell script. Can you guys help me to do this. My Requirment is as below: Input for me will be 2000909(YYYYMM) I need the previous months last day minus 1 day timestamp. That is i need 2000908 months last day minus ... (3 Replies)
Discussion started by: girish.raos
3 Replies

7. Solaris

/var size is increasing day by day

Hi experts, I am facing a big problem. i use solaris 9. i found size of /var is increasing day by. snapshot of a yester moring- /dev/vx/dsk/var 15G 14G 1.1G 94% /var snapshot of a yesterday everning- /dev/vx/dsk/var 15G 14G 824M 95% /var I am... (11 Replies)
Discussion started by: thepurple
11 Replies

8. Shell Programming and Scripting

Write a shell script to find whether the first day of the month is a working day

Hi , I am relatively new to unix... Can u pls help me out to find out if the first day of the month is a working day ie from (Monday to Friday)...using Date and If clause in Korn shell.. This is very urgent. Thanks for ur help... (7 Replies)
Discussion started by: phani
7 Replies

9. Shell Programming and Scripting

How to compare prev day file to current day file

Hi all: I am new to this board and UNIX programming so please forgive if I don't explain something correctly. I am trying to write a script to keep track of our links, we link one program written for Client A to Client B's directory. What we want to do is to keep track of our linked programs... (1 Reply)
Discussion started by: Smurtzy
1 Replies
Login or Register to Ask a Question