mute@tiny:~$ time gawk -f ./square1 <<< $'90000000000\n4\n1'
90000000000 is the perfect square of 300000
4 is the perfect square of 2
1 is the perfect square of 1
real 0m0.081s
user 0m0.063s
sys 0m0.016s
Code:
{
number=$1
series=1
square=1
root=1
while (square <= number) {
if (square == number) {
printf("%d is the perfect square of %d\n", number, root)
next
}
root++
series+=2
square+=series
}
print "Not a perfect square"
next
}
---------- Post updated at 10:58 PM ---------- Previous update was at 10:45 PM ----------
using a binary search was idea taken from friend.. they implemented in bash and I again convert to awk. using "one true awk" is a tad faster than gawk
Code:
mute@tiny:~$ time bawk -f ./square2 90000000000
90000000000 is a perfect square of 300000
real 0m0.002s
user 0m0.001s
sys 0m0.001s
Code:
#!/usr/bin/awk -f
BEGIN {
num = ARGV[1]
i = 10
while (i * i < num)
i *= 10
lower = i/10
upper = i
while ( (avg = (upper + lower) / 2) != lower )
if (avg * avg > num)
upper = avg
else
lower = avg
if (avg * avg == num)
print num" is a perfect square of "avg
else
print num" is not a perfect square"
exit
}
Last edited by neutronscott; 08-21-2015 at 01:04 PM..
These 2 Users Gave Thanks to neutronscott For This Post:
Hi All,
I have a file of the following format.
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
<role rolename="tomcat"/>
<role rolename="role1"/>
<role rolename="manager"/>
<role rolename="admin"/>
<user username="tomcat" password="tomcat" roles="tomcat"/>
<user... (5 Replies)
Here's my work of testing whether a number input is perfect or not..
echo Enter a number
read no
i=1
ans=0
while
do
if
then
ans='expr $ans + $i'
fi
i='expr $i + 1'
done
if
then
echo $no is perfect
else
echo $no is NOT perfect
fi (12 Replies)
I've got an aix-box somewhere on the network and a PC on my desk. Nothing fancy so far.
The PC is made dual-boot:
- windowsXP with putty & winSCP
or
- slackware 13 with xfce4 installed.
The aix-box runs DB2 v8.2 and I've installed db2top to monitor the database.
db2top is a character... (0 Replies)
Hi All,
I have a text file which looks like this:
computer programming
systems engineering
I want to get rid of these square brackets and also the text that is inside these brackets. So that my final text file looks like this:
computer programming
systems engineering
I am using... (3 Replies)
I want to print only the lines that meet the criteria : "worde:" and "wordo;"
I got this far:
sed -n '/\(*\)\1e:\1o;/p;'
But it doesn't quite work.
Can someone please perfect it and tell me exactly how its a fixed version/what was wrong with mine?
Thanks heaps, (1 Reply)
I have to find last delimiter in each line of a file and store the value after the last '/' in a variable in ksh script...Pls Pls help me:(The file is as shown below:
/opt/apps/cobqa/apps/abadv/bind/advc0007.bnd
/opt/apps/cobqa/apps/abbrio/bind/naac6115.bnd... (5 Replies)