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
interp(n)						       Interpreter utilities							 interp(n)

__________________________________________________________________________________________________________________________________________________

NAME
interp - Interp creation and aliasing SYNOPSIS
package require Tcl 8.3 package require interp ?0.1.1? ::interp::createEmpty ?path? ::interp::snitLink path methodlist ::interp::snitDictLink path methoddict _________________________________________________________________ DESCRIPTION
This package provides a number of commands for the convenient creation of Tcl interpreters for highly restricted execution. API
::interp::createEmpty ?path? This commands creates an empty Tcl interpreter and returns it name. Empty means that the new interpreter has neither namespaces, nor any commands. It is useful only for the creation of aliases. If a path is specified then it is taken as the name of the new interpreter. ::interp::snitLink path methodlist This command assumes that it was called from within a method of a snit object, and that the command mymethod is available. It extends the interpreter specified by path with aliases for all methods found in the methodlist, with the alias directing execu- tion to the same-named method of the snit object invoking this command. Each element of methodlist is actually interpreted as a command prefix, with the first word of each prefix the name of the method to link to. The result of the command is the empty string. ::interp::snitDictLink path methoddict This command behaves like ::interp::snitLink, except that it takes a dictionary mapping from commands to methods as its input, and not a list of methods. Like for ::interp::snitLink the method references are actually command prefixes. This command allows the creation of more complex command-method mappings than ::interp::snitLink. The result of the command is the empty string. BUGS, IDEAS, FEEDBACK This document, and the package it describes, will undoubtedly contain bugs and other problems. Please report such in the category interp of the Tcllib SF Trackers [http://sourceforge.net/tracker/?group_id=12883]. Please also report any ideas for enhancements you may have for either package and/or documentation. KEYWORDS
alias, empty interpreter, interpreter, method, snit COPYRIGHT
Copyright (c) 2006 Andreas Kupries <andreas_kupries@users.sourceforge.net> interp 0.1.1 interp(n)
All times are GMT -4. The time now is 05:03 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy