Monday, December 29, 2008

Project Euler and PowerShell - Problem 7

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6^(th) prime is 13.

What is the 10001^(st) prime number?

function isPrime
{
param ($number)
$isPrime = $true
if($number -lt 2) { $isPrime = $False}
if($number -gt 2 -and $number%2 -eq 0) {$isPrime = $False}
for($i=3;$i -le [math]::Sqrt($number);$i+=2)
{
if($number % $i -eq 0) { $isPrime = $False}
}
$isPrime
}

$i = $j = 0
do {
if(isPrime $i){$j++;$i}
$i++
}
until ($j -eq 10001)

Project Euler and PowerShell - Problem 6

This was an easy one!
The sum of the squares of the first ten natural numbers is,

1^(2) + 2^(2) + ... + 10^(2) = 385

The square of the sum of the first ten natural numbers is,

(1 + 2 + ... + 10)^(2) = 55^(2) = 3025

Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

$sumSquares = $squareSums = $difference = 0
for ($i=1;$i -le 100;$i++)
{
$sumSquares += [Math]::Pow($i,2)
$squareSums += $i
}
$difference = [math]::Pow($squareSums,2) - $sumSquares
$difference

Project Euler and PowerShell - Problem 5

Moving on...

Problem 5: 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest number that is divisible with no remainder evenly divisible by all of the numbers from 1 to 20?
function modFunction ($mod)
{
$number = 1
$start = $mod
do {
if($number%$mod -eq 0)
{
$mod--
}
else
{
$number++;
$mod=$start
}
}
until ($mod -eq 1)
$number
}

modFunction 20
While short, this is clearly a brute force approach. Anyone have a more efficient algorithm?

Project Euler and PowerShell - Problem 4

Problem 4: A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91X99. Find the largest palindrome made from the product of two 3-digit numbers.

function Reverse-Integer
{
param([string]$str)
for ($i = $str.ToString().Length - 1; $i -ge 0; $i--)
{
$c = $c + ($str.ToString().Substring($i,1))
}
[
int]$c
}

$big = 0
for($x=100;$x -le 999;$x++)
{
for($y=100;$y -le 999;$y++)
{
$xy = $x*$y
$yx = Reverse-Integer $xy
if ($yx -eq $xy)
{
if ($xy -gt $big) {$big=$xy}
}
}
}
$big

This one takes a long time to run. If anyone has any ideas on how to refactor this, please share!

Project Euler and PowerShell - Problem 3

Problem 3 -The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143?
function isPrime
{
param ($number)
$isPrime = $true
if($number -lt 2) { $isPrime = $False}
if($number -gt 2 -and $number%2 -eq 0) {$isPrime = $False}
for($i=3;$i -le [math]::Sqrt($number);$i+=2)
{
if($number % $i -eq 0) { $isPrime = $False}
}
$isPrime
}

$value=600851475143
$sqrValue =[Math]::Sqrt($value)

for($i=3;$i -le $sqrValue; $i+=2)
{
if($value%$i -eq 0 -and (isPrime $i))
{
$maxPrime = $i
}
}

$maxPrime

Project Euler and PowerShell - Problem 2

This is a continuation of my efforts to use PowerShell to solve some of the problems at Project Euler.

Problem 2: Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

Find the sum of all the even-valued terms in the sequence which do not exceed four million.

function Get-SumFib ($n)
{
$current = $previous = 1
while ($current -lt $n)
{
if($current%2 -eq 0)
{
$sum= $sum + $current
}

$current,$previous = ($current + $previous),$current}
$sum
}
Get-Fib 4000000

Project Euler and PowerShell - Problem 1

Was looking at Pete on Software the other day and saw a reference to Project Euler. What a cool site! Basically, the intent of this site is to use computational/programming skills to solve increasing complex mathematical problems. This seemed like an interesting way to test my PowerShell skills. The next few blog posts will be my attempts at solving a few of these.

- Spoiler Alert -

If you have any interest in solving any of these problems at Project Euler, quit reading this post and immediately pull up your programming editor of choice and have at it.

Problem 1: If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.

$result = 0
for($i = 0; $i -lt 1000;$i++)
{
if ($i % 3 -eq 0 -or $i % 5 -eq 0)
{
$result += $i;
}
}
$result

Saturday, December 20, 2008

PowerShell at HIMSS

I found out on Friday (December 19th, 2008) that my proposal to speak at the Healthcare Information and Management Systems Society (HIMSS) - Microsoft Health Users Group (MSHUG) was accepted! This is two years in a row that I have had the opportunity to speak at this conference. Last year, a colleague of mine (Andrew Klare) and I presented on the implementation of Microsoft Speech with the Ohio State University Medical Center's Patient Transportation application.

This year, the presentation will be titled, “Managing your Healthcare environment with PowerShell”. I am looking forward to evangelizing PowerShell with this group! Given the time constraint (approx. 50 minutes), I will more then likely show examples of various monitoring and scheduled jobs that we currently use. I will also try to demonstrate how the use of PowerShell has impacted productivity and budgets.

Any suggestions from the PowerShell community would be appreciated!

Friday, December 19, 2008

PowerShell Twitterers

Steven Murawski has collected and posted a list of PowerShell related Twitterers. If you are a fan of PowerShell and use Twitter, these are some of the folks that you should follow.

What is Twitter you ask? Good question! A concise definition would be - It is free social networking service. This however doesn't quite do it justice. Check out a few of the following posts for a better idea of how people are using it.

Better yet, go to Twitter and set up your free account.

Enjoy!

Wednesday, December 17, 2008

BlackBerry Disaster Recover Information

Was asked yesterday if I could write something that would periodically generate a CSV file of our BlackBerry users and their pin numbers. This "report" would be used for communication purposes in a Disaster situation. I looked on the BlackBerry server for a few minutes hunting for a way to do it from the server. No such luck. I then remembered that all the data resides in SQL Server diligently waiting to be queried. Following is a small script that I have scheduled to run once a week:

# Parse the DN to get the SAMAccountName
$SQLQuery = "SELECT DisplayName,PIN,CreationTime,MailboxSMTPAddr,
Right(MailboxDN,(LEN(MailboxDN)-CHARINDEX('s/cn=', MailboxDN)-4)) AS SamAccountName
FROM UserConfig
ORDER BY DisplayName
"

#Set up connection
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = YourServer; Database = YourDB; User ID=YourID; Password=YourPWD; Integrated Security = False"
$SqlConnection.Open()

# Setup SQL Command
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SQLQuery
$SqlCmd.Connection = $SqlConnection

# Create the table and export!
$SqlVMDataReader = $SqlCmd.ExecuteReader()
$SqlVMDataTable = New-Object System.Data.DataTable
$SqlVMDataTable.Load($SqlVMDataReader)
$SqlVMDataTable Export-Csv 'L:\DRDocs\BBPins.csv' -noTypeInformation

# Cleanup
$SqlVMDataReader.Close()
$SqlConnection.Close()

Enjoy!

Monday, December 15, 2008

PrimalForms Example (Virtual Center Log)

Recently saw a post on Twitter from alanrenouf. He was asking if there was a log file that could be parsed to search for who deleted a VM. I know that this data was kept in the Virtual Center Database, specifically in the VPX_EVENT table. I mentioned this to Alan and he quickly wrote a PowerShell script to find the data and blogged it.
Concurrently, I was investigating PrimalForms (PowerShell UI builder - FREE). Once I saw Alan's post, I knew it would be a good example to try PrimalForms with.

The UI was simple to create with PrimalForms, here is a screenshot of the UI.

The dropdowns are filled on the form load with the following:
- Distinct Virtual Machine
- Distinct User
- Distinct Event (in Alan's case vim.event.VmRemovedEvent)
- Search Start Date
- Search End Date


Selecting the above variables generates the SQL statement to the VPX_EVENT table. The results are then passed to the datagrid.




Pretty cool!


As soon as I figure out how to attach a text file to Blogger, I will do so. In the short-term, if you are interested in the script(s), send me a note at stahler.2@osu.edu.

Enjoy!