OK. I think I'm caught up on my sleep. And, I think I have a new script that meets all of the requirements (bash script using only integer arithmetic and only using shell built-ins running on Mac OS X on a MacBook Pro (assembled in mid 2014)). I am, however, using OS X version 10.10.5 instead of version 10.7.5 that wisecracker was using.
The bash script wisecracker provided in post #1 in this thread mostly works fine for relatively small values, but takes a LONG time for relatively large values. I was surprised that it said that 0 is not a perfect square instead of saying that the minimum value it would process is 1. I wanted to see what it would do when invoked as:
I wasn't surprised that it came up with the correct answer:
but a ps run shortly before it completed showed that it used more than 595 cpu minutes to compute that result.
While that was consuming one core of one cpu, I played around with Corona688's bash script in post #16 in this thread. It correctly reported that 0 is a perfect square (which surprised me since it always uses 1 as the low end of its binary search range). And, due to the way it calculates the stopping point for the binary search and the way it determines the value it is processing, it says:
even though the correct response would be:
When given invalid numbers and when given valid numbers that do not fit in a signed long integer, bash prints a diagnostic message when processing that value with Corona688's script and sometimes continues processing and sometimes terminates processing at that point.
I made a slight modification to Corona688's script to read values from a file instead of just processing one command line argument.
And, I produced the following bash script which performs a more traditional binary search. It uses a little bit of number theory to reduce the range of the binary search. (An x-1 or x digit integer, where x is even, will have a square root that is x/2 digits if it has an integer square root.) It performs lots of tests to verify that the input is a valid number and is in range for a signed long int.
It reads its input from a file named file and looks for two values on each line. The 1st value is the number to be processed. The 2nd value (if present) indicates the expected results for the 1st value (-1 if it is not a perfect square or the square root if it is a perfect square). If the expected value does not match the computed value, it prints a diagnostic. If the 2nd value is not present, the result is printed, but not verified.
With the following input in file:
it produces the output:
with average times:
for 10 runs. With the same input file, the modified version of Corona688's script produces the output:
with average times:
for 10 runs. Note that it is expected to run faster than the above script because it doesn't perform any error checking and it doesn't process the last two input lines because it dies on any of the last three input lines. I was surprised that the times were so close. (Apparently reducing the binary search range made up for the extra data verification tests.)
Note also that I am not saying that Corona688's script should perform any error checking; that was not a requirement for this project. I machine-generated most of the input file (after I found the anomalies in the results from the ksh script I presented earlier) and added code to verify that the results computed matched known results as a debugging aid.
These 3 Users Gave Thanks to Don Cragun 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)