Towards the end of last week I started to look at how I could get a PowerShell script to generate a list a list of Perfect Numbers. It's not that I wanted to know what they are (that's what Google is there for), I just wanted to see if I could code it up in PS. Given the nature of Perfect Numbers, the first thing is to identify those numbers that are proper positive divisors of the starting number eg: 6 is the first Perfect Number as the sum of the proper positive divisors (1, 2, & 3) equal the starting number.
The basic premise was to code the division (easy), then check to see if the result was a whole number, cast all the results into an array and then see if the sum of the numbers in the array equalled the starting number, then start the process again incrementing the starting number by one.
First hiccup was to how to check whether the result of the division was a whole number. A post to the Scripting Guys Forum on TechNet on Saturday night prompted a number of suggestions as to how I could do this (thanks to all who replied!). Being lazy I've gone for the (hopefully!) least typing option and that is to perform a check on the result of the division with something like this:
$result = 6/2
if ($result -is [int]){write-host "It's a whole number"}
That's as far as I've got in what is probably the next installment of my series of 'Preventing boredom with numbers (and PowerShell)' postings, but fear not dear avid reader, I will keep you updated as to how my latest sleep-inducing PS project is progressing...
No comments:
Post a Comment