Welcome, guest | Sign In | My Account | Store | Cart

A procedure from the bag of utilities used by Jeff Hobbs. Given an integer number it finds the smallest square greater than the input. A possible application for this is when a discrete number of items have to be displayed in a table. Using this returns the number of columns/rows for the table to fit all items.

Tcl, 12 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# get_square_size --
#   gets the minimum square size for an input
# Arguments:
#   num		number
# Returns:
#   returns smallest square size that would fit number
#
proc get_square_size num {
    set i 1
    while {($i*$i) < $num} { incr i }
    return $i
}

1 comment

Keith Vetter 20 years, 4 months ago  # | flag

Simpler version. Why not just:

proc get_square_size {num} {
  return [expr {int(sqrt($num))}]
}
Created by andreas kupries on Wed, 21 Aug 2002 (MIT)
Tcl recipes (162)
andreas kupries's recipes (20)

Required Modules

  • (none specified)

Other Information and Tasks