Entries in Programming (5)

Tuesday
Sep272011

Back to Basics: Exhaustive Enumeration in Powershell

I'm often overheard at work and customer engagements spreading the gospel about Powershell, encouraging everyone from the Junior SA to the most senior "engineer" to take advantage of this powerful language. Anyone can pick Powershell up and become quite productive in no time. One of the things I noticed however was a lack of fundamentals in the newly initiated. So I figured why not do my part and provide a few posts I'm calling "Back To Basics". These are foundational to any langauge, not just Powershell, so pick your poison. Feel free to post your samples in the comments.

Now lets get started with our first lesson, Exhaustive Enumeration.

So what is it? Put simply, exhaustive enumeration is trying all "possible" values until you find the solution.

I always find a problem useful for learning so lets use something simple we can all understand. Finding the square root of a perfect square.

So whats a square and a square root?

A square is a number multiplied by itself. So 3 x 3 = 9

Ok enough of that, how about some code?

PS C:\> [math]::sqrt(9)            
3

No, no, no. Thats not it. The point of this code is to learn. We all know there are better ways to do things, but remember we are forcing a point here, so the code is representing that. Alright back to the matter at hand:

# Find the square root of a perfect square            
$x = Read-Host "Please enter a positive integer"            
$ans = 0            
if ($x -as [int] -and $x -ge 0) {            
 while ($ans * $ans -lt $x) {            
  $ans = $ans + 1            
  Write-Verbose "Trying [$ans]"            
 }            
 if ($ans*$ans -ne $x) {            
  Write-Host "$x is not a perfect square!"            
 } else {            
  Write-Host $ans            
 }            
} else {            
 Write-Host "$x is a negative number or a non-integer."            
}

Now lets see it in action:

PS H:\Development\Powershell> $VerbosePreference = "Continue"            
PS H:\Development\Powershell> .\ExhaustiveEnumeration.ps1            
Please enter a positive integer: 9            
VERBOSE: Trying [1]            
VERBOSE: Trying [2]            
VERBOSE: Trying [3]            
3

So most of you are thinking man that must be slow! So lets take a larger number, say 1515361 and see how slow it is:

PS H:\Development\Powershell> Measure-Command {.\ExhaustiveEnumeration.ps1}            
1231            

Days              : 0            
Hours             : 0            
Minutes           : 0            
Seconds           : 0            
Milliseconds      : 131            
Ticks             : 1310124            
TotalDays         : 1.51634722222222E-06            
TotalHours        : 3.63923333333333E-05            
TotalMinutes      : 0.00218354            
TotalSeconds      : 0.1310124            
TotalMilliseconds : 131.0124

131 milliseconds, not bad. See computers are pretty good at this type of thing! Yes there are much more efficient ways to solve this, but you'll have to wait for a future post.

Tuesday
Nov102009

Google Delivers New Systems Programming Language - Go

What is the purpose of the project?

No major systems language has emerged in over a decade, but over that time the computing landscape has changed tremendously. There are several trends:

  • Computers are enormously quicker but software development is not faster.
  • Dependency management is a big part of software development today but the “header files” of languages in the C tradition are antithetical to clean dependency analysis—and fast compilation.
  • There is a growing rebellion against cumbersome type systems like those of Java and C++, pushing people towards dynamically typed languages such as Python and JavaScript.
  • Some fundamental concepts such as garbage collection and parallel computation are not well supported by popular systems languages.
  • The emergence of multicore computers has generated worry and confusion.

We believe it's worth trying again with a new language, a concurrent, garbage-collected language with fast compilation. Regarding the points above:

  • It is possible to compile a large Go program in a few seconds on a single computer.
  • Go provides a model for software construction that makes dependency analysis easy and avoids much of the overhead of C-style include files and libraries.
  • Go's type system has no hierarchy, so no time is spent defining the relationships between types. Also, although Go has static types the language attempts to make types feel lighter weight than in typical OO languages.
  • Go is fully garbage-collected and provides fundamental support for concurrent execution and communication.
  • By its design, Go proposes an approach for the construction of system software on multicore machines.

Now before you go running off to write the next big thing, remember Google says:

Go is an experiment. The implementation isn't quite mature enough yet for large-scale production use.

Page 1 2