Sponsored Content
Operating Systems OS X (Apple) A Fun Perfect Square Checker Using Integer Arithmetic Only... ;o) Post 302952687 by neutronscott on Thursday 20th of August 2015 10:58:29 PM
Old 08-20-2015
Don cheated!!

Using same method, different interpreter:

Code:
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:
 

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
rootv(1)						      General Commands Manual							  rootv(1)

NAME
rootv - display TV using the Xvideo extension SYNOPSIS
rootv [ options ] DESCRIPTION
rootv puts a video to some window. By default it creates its own window, but you can also ask it to use the root window (thus the name) or any other window. rootv needs the Xvideo extension. rootv itself provides no way to control video/audio/tuner. You can use v4lctl to do this. OPTIONS
rootv knows a number of command line options to modify the behaviour. -help Print a short description of all command line options. -verbose Be verbose. That will dump some debug messages to stderr. -root Put the video onto the root window instead of creating a new window. -id <win> Put the video into the window <win> instead of creating a new window. -station <st> Tune station <st>. This just calls v4lctl, which in turn will look up the station in your ~/.xawtv config file. -no-mute Don't toggle mute. By default rootv will unmute the TV card when it starts and mute it on exit. It does this using the XV_MUTE attribute (if available). -port <n> Use Xvideo port <n>. Try the -verbose switch if you want to know which ones are available. -bg Fork into background. -wm Print WindowMaker menu (set stations using v4lctl). SEE ALSO
xawtv(1), fbtv(1), v4lctl(1) AUTHOR
Gerd Knorr <kraxel@bytesex.org> COPYRIGHT
Copyright (C) 2000-2002 Gerd Knorr <kraxel@bytesex.org> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MER- CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. (c) 2000-2002 Gerd Knorr rootv(1)
All times are GMT -4. The time now is 10:48 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy