Sponsored Content
Operating Systems OS X (Apple) A Fun Perfect Square Checker Using Integer Arithmetic Only... ;o) Post 302952975 by Don Cragun on Monday 24th of August 2015 04:50:14 PM
Old 08-24-2015
Quote:
Originally Posted by wisecracker
Hi Don...

Phew, that took some reading...

As 0^2 is 0, I never even considered it to be a perfect square so I stand corrected.
My error; however my code can very easily be changed to cater for it.

As you say it is slow, but it was coded from an observation not from any mathematical set of algorithms, that is, every _line_no_ is the perfect square of the odd number series...
Yes, it was a long post. I hope a few people found the narrative informative.

My code rejected 0 since your code didn't process it correctly. If you'd like to modify my script to accept 0, here is a diff showing the changes needed:
Code:
48,49c48,49
< 	    [[ num -lt 1 ]]
< 	then	printf "Out of range: 0 < x <= $LONG_MAX: x=$num\n"
---
> 	    [[ num -lt 0 ]]
> 	then	printf "Out of range: 0 <= x <= $LONG_MAX: x=$num\n"
58c58
< 		low=$((10 ** ((${#num} - 1) / 2)))
---
> 		low=$((10 ** ((${#num} - 1) / 2) - 1))

Your code used the observation that the sum of all of the odd numbers from 0 to x is a perfect square. My code used the observation that the square root of any positive x digit number has a square root between 10^((x-1)/2) and 10^(((x-1)/2)+1)-1 inclusive (except for the number 0 [since 10^0 is 1; not 0]).

Your code starts with the lowest perfect square and calculates successive perfect squares until it has a perfect square that is greater than or equal to the number being checked. My code performs a binary search of the possible square roots that could match the target number until it determines that there is a square root in range that is the square root of the target or determines that one does not exist.

For a 63-bit integer value, your code may need to loop through millions of possible perfect squares. A binary search like my code uses needs to loop through no more than 32 possible square roots. My code needs less than that since it restricts the high and low possible square roots based on the number of digits in the target number.
 

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

extraction of perfect text from file.

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)
Discussion started by: nua7
5 Replies

2. UNIX for Dummies Questions & Answers

A perfect number shell program

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)
Discussion started by: Cyansnow
12 Replies

3. AIX

I want the perfect user-interface

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)
Discussion started by: dr_te_z
0 Replies

4. Shell Programming and Scripting

Delete text between square brackets and also delete those square brackets using sed or awk

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)
Discussion started by: shoaibjameel123
3 Replies

5. Shell Programming and Scripting

how to compare string integer with an integer?

hi, how to I do this? i="4.000" if ; then echo "smaller" fi how do I convert the "4.000" to 4? Thanks! (4 Replies)
Discussion started by: h0ujun
4 Replies

6. UNIX for Dummies Questions & Answers

Can you perfect my sed ?

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)
Discussion started by: maximus73
1 Replies

7. Shell Programming and Scripting

egrep line with perfect mach

Hi Input File A L006 AL01 0 (OCK) L006 A006 0 (OCK) L011 AR11 1 (NLOCK) Input File B L006 AL01 0 (OCK) L006 A006 0 (OCK) Need Egrep Command for perfect Match Thanks (4 Replies)
Discussion started by: asavaliya
4 Replies

8. Shell Programming and Scripting

Not able to find the perfect code...Geting confused in between

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)
Discussion started by: bhavanabahety
5 Replies
set_color(1)							       fish							      set_color(1)

NAME
set_color - set_color - set the terminal color set_color - set the terminal color Synopsis set_color [-v --version] [-h --help] [-b --background COLOR] [COLOR] Description Change the foreground and/or background color of the terminal. COLOR is one of black, red, green, brown, yellow, blue, magenta, purple, cyan, white and normal. o -b, --background Set the background color o -c, --print-colors Prints a list of all valid color names o -h, --help Display help message and exit o -o, --bold Set bold or extra bright mode o -u, --underline Set underlined mode o -v, --version Display version and exit Calling set_color normal will set the terminal color to whatever is the default color of the terminal. Some terminals use the --bold escape sequence to switch to a brighter color set. On such terminals, set_color white will result in a grey font color, while set_color --bold white will result in a white font color. Not all terminal emulators support all these features. This is not a bug in set_color but a missing feature in the terminal emulator. set_color uses the terminfo database to look up how to change terminal colors on whatever terminal is in use. Some systems have old and incomplete terminfo databases, and may lack color information for terminals that support it. Download and install the latest version of ncurses and recompile fish against it in order to fix this issue. Version 1.23.1 Sun Jan 8 2012 set_color(1)
All times are GMT -4. The time now is 05:13 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy