MOSS MVP

I've moved my blog to http://blog.falchionconsulting.com!. Please update your links. This blog is no longer in use--you can find all posts and comments at my new blog; I will no longer be posting to this site and comments have been disabled.

Tuesday, February 23, 2010

4th Grade Math and PowerShell

My 10 year old daughter came home from school today with a simple math problem that she had to solve and she asked if there was an easier way to solve the problem. Basically the teacher explained that the number six was a perfect number because the sum of it's factors (excluding itself) is equal to the number and she wanted to know what the next perfect number was. The kids basically had to go through the numbers and manually add up the factors until they found a match. So naturally, the programmer in me said, sure we can write a quick algorithm to find the next few perfect numbers.

After writing the code, using PowerShell of course, I realized that this little math algorithm demonstrated a few key PowerShell concepts that would be good for anyone starting out with PowerShell to know. Here's the code I wrote for her along with the output:

image

So in this really simple math example you can see how to use a basic for loop construct, dynamic arrays, dynamic typing, static method calls, and variable replacement within strings.

As you can see, for loops in PowerShell are identical to those in C# so there's nothing new there (unless you're new to C# that is). Dynamic arrays on the other hand are kind of cool - you can declare an empty array by simply using @(). Don't confuse this with declaring empty hash tables which use curly braces instead of parenthesis, @{}. Adding elements to these arrays is as easy as using the += operator: $factors += $i.

You can see the dynamic typing where I'm dividing $i by $j - if it divides evenly then the type returned would be an integer, otherwise it would be a float. So a really easy way to check if it divided evenly is to see if the returned type is equal to [int] (we can work with a type by wrapping a type name in brackets).

Calling static methods is a little different than what you may be used to in C#. As mentioned above types are defined by wrapping the type name in brackets - if we want to call a static method (or access a static property) of a type then we simply separate the method or property name and the type name with double colons. So in this example I'm finding the square root of the number by using the static Sqrt method on the System.Math class: [Math]::Sqrt($i).

The last little bit, dynamic variable replacement, just demonstrates how we can use $() to force the contents of the parentheses to be evaluated before they are used within the string for the Write-Host command. In this case I wanted to show the number of factors but if I did not wrap the $factors.Length bit in parentheses my output would look like this: 6 (1 2 3.Length factors)= 1 2 3.

So, as you can see, PowerShell is a really great tool and can be used for things other than SharePoint, even helping your 4th grader with her math homework :)

3 comments:

Perry said...

I think your inner loop should use "less than or equal to", not "less than".

Actually, the fact that you tested for $i/$j = $j indicates that you thought so too, at some point.

(After trying to post this about ten times, I've downgraded my web browser as a new attempt to post a comment. I'm not sure why it is so difficult to post comments, or why I have so much trouble anyway, here. Now I've progressed to actually getting an error -- my request could not be processed -- but I don't currently see any explanation as to why not.)

Anonymous said...

Aha. Apparently if I try enough times, eventually a captcha will appear, and then it will accept the comment after that. Odd.

Plus it just crashed IE8 once, so that was fun.

Gary Lapointe said...

You could be right but it solved the problem we were seeking to solve and I only spent a few minutes on it - it's not like it's going into production :)

Not sure what's up with the comments - I might go back to the off page comment form as I've had a couple of people complain about this one (it's controlled by blogspot so not much I can do to fix it).