summaryrefslogtreecommitdiff
path: root/examples/functions/sqroot
blob: 79715cbf1d708d4df9e082710a7dc02d34c386fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#From: Syamala Rao Tadigadapa <stadigad@us.oracle.com>
#Subject: Re: Division in ksh(getting the exact number) Please HELP
#Date: Mon, 25 Oct 1999 15:05:08 -0700
#Message-ID: <3814D414.7B896084@us.oracle.com>

#Here is how to calculate the (integer part of the) square root
#of a number in a much cleaner way.

sqroot()
{
	let arg=$1
	let root=arg
	while :
	do
		newroot=$(( (root+arg/root)/2 ))
		(( newroot == root )) && { echo $root; return; }
		let root=newroot
	done
}

sqroot "$@"