Showing posts with label scripting. Show all posts
Showing posts with label scripting. Show all posts

Saturday, September 12, 2009

Euler Problem 6

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

12 + 22 + ... + 102 = 385

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

(1 + 2 + ... + 10)2 = 552 = 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.

Powershell Solution:

  1. $SumOfSq = 1..100|%{[Math]::pow($_,2)}|measure -sum|%{$_.sum} 
  2. $SqOfSum = 1..100|measure -sum|%{[Math]::pow($_.sum,2)} 
  3. echo ($SqOfSum - $SumOfSq

Note: The solution may not be the optimized one… just a quick hack… I’ll revisit later for optimization if time permits… :-)

Friday, September 11, 2009

Euler 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 evenly divisible by all of the numbers from 1 to 20?

Powershell Solution:

This can be done manually (pen/paper) using prime factorization.
However, since I love powershell… We’ll use it with prime factorization in mind…

  1. function Get-Factors($number
  2.     [Array]$result = {1} 
  3.     $factor = 2 
  4.     while($number -gt 1) 
  5.     { 
  6.         if($number%$factor -eq 0) 
  7.         { 
  8.             $result += $factor 
  9.             $number /= $factor 
  10.         }  
  11.         else 
  12.         { 
  13.             $factor += 1 
  14.         } 
  15.     }  
  16.      
  17.     return $result 
  18.  
  19. function Get-NextMultiple($currentMultiple, $currentFactor
  20.     foreach($factor in Get-Factors($currentFactor)) 
  21.     { 
  22.         $currentMultiple = $currentMultiple * $factor.ToString() 
  23.         if($currentMultiple%$currentFactor -eq 0) 
  24.         { 
  25.             return $currentMultiple 
  26.         } 
  27.     } 
  28.  
  29. $currentMultiple = 1 
  30. 1..20|foreach{($currentMultiple = Get-NextMultiple $currentMultiple $_)}|measure -max|%{$_.Maximum} 

Note: The solution may not be the optimized one… just a quick hack… I’ll revisit later for optimization if time permits… :-)

Thursday, September 10, 2009

Euler Problem 4

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.

Find the largest palindrome made from the product of two 3-digit numbers.

Powershell Solution:

  1. function Reverse-Text([string] $text
  2.     if($text.Length -eq 1) 
  3.     { 
  4.         return $text 
  5.     } 
  6.     else 
  7.     { 
  8.         return (Reverse-Text($text.Substring(1, $text.Length-1))) + $text[0] 
  9.     } 
  10.  
  11. $start = 999 
  12. $end = 100 
  13. $max_palindrome = 0 
  14. for($num1 = $start; $num1 -ge $end; $num1--) 
  15.     for($num2 = $num1; $num2 -ge $end; $num2--) 
  16.     { 
  17.         $product = $num1*$num2 
  18.         if($product.ToString() -eq (Reverse-Text($product))) 
  19.         { 
  20.             if($product -gt $max_palindrome
  21.             { 
  22.                 $max_palindrome = $product 
  23.             } 
  24.         } 
  25.     } 
  26.  
  27. echo $max_palindrome 
Note: The solution may not be the optimized one… just a quick hack… I’ll revisit later for optimization if time permits… :-)

Euler Problem 3

The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?

Quick Powershell Solution:

  1. $num = 600851475143  
  2. $max_prime = 1 
  3.  
  4. $end1 = 0 
  5. while($end1 -eq 0) 
  6.     $factor = 2 
  7.     $end2 = 0 
  8.     while($end2 -eq 0) 
  9.     { 
  10.         if($num%$factor -eq 0) 
  11.         { 
  12.             if($max_prime -lt $factor
  13.             { 
  14.                 $max_prime = $factor 
  15.             } 
  16.              
  17.             $num = $num/$factor 
  18.             $end2 = 1 
  19.         } 
  20.         $factor = $factor+1 
  21.     } 
  22.      
  23.     if($num -eq 1) 
  24.     { 
  25.         $end1 = 1 
  26.     } 
  27.  
  28. echo $max_prime 

Note: The solution may not be the optimized one… just a quick hack… I’ll revisit later for optimization if time permits… :-)

Sunday, April 15, 2007

Introduction to Powershell

Greetings! I've been playing with Powershell these past few days and I'd like to share my joy with you.Since, this is my first post. I won't dive deep into the chasm of technicalities and hifalutin terms.There are only three goals at the moment:
  1. Know what is PowerShell
  2. Identify three things one can do with it
  3. What do I need to get started

What is PowerShell?

PowerShell is the next generation command-line shell for Windows. Although, this was designed for system administrators, developers like me noticed very appealing benefits in using this tool. This shell has an interactive prompt and scripting environment that can be used independently or in combination. There are also third party IDEs (Interactive Development Environment) that are available. Anyway, with the basic needs, I think using the command prompt approach is already enough. The best feature of PowerShell I love the most is the .NET Framework leverage. That concept is very attractive to me as a developer who spent most of his time developing applications using .NET. I know almost nothing about WMI, VBScript, and other scripting technologies. But, I know .NET and PowerShell gives me the power to use this to extend my existing knowledge for the command-line shell.

When I think of PowerShell, I think of automation. When I think of automation (when done correctly), I think of productivity. When I think of productivity, I think of efficient use of time. I think time is one of the resources people take for granted. It's one of the resources when lost cannot be recovered anymore. PowerShell is one of the tools that help me be a good steward of time. As of now, I can think of two types of tasks that it helps me automate:
  1. Routine tasks which consists of many steps
  2. Tasks in which I have to perform several mouse clicks just to accomplish

What can I do with it?

These are only some in a huge library of things you can do with PowerShell.

A. Write smaller code to display "Hello World"

One upon a time, my friend and I were talking about computer languages. I asked her why she loves studying the current computer language she's studying. She said that in some other languages, you need to write several lines of code just to display a simple "Hello World". Then, she showed me the syntax in her language. I saw two lines. Well, in other languages, such as Python, one could just type: echo "Hello World". This works similar with the Windows CMD. Then, I remembered PowerShell, it's just "Hello World". That's it. No additional keywords. I believe this is because "Hello World" is already a string object. PowerShell recognizes .NET objects and is intellegent enough to display such result. Maybe, this is not really a compeling reason to use PowerShell. But, on second thought, sometimes simplicity makes a huge difference.

Type the following the PowerShell prompt:
"Hello World"

To get the length of the string type:
"Hello World".length

To know more about other things you can do with "Hello World" type:
"Hello World" | get-member

B. Display files

When I joined my first company three years ago, we were trained in administering a popular operating system called Linux.That's the time when my passion for using the command-line shell began to spark. I love the concept of accompishing computer tasks without the use of the mouse. It's like playing bumblebee on the piano with both hands and never reaching out to the musicsheet for page turns 'cause you got it memorized already. When I was tinkering with Powershell, I discovered that it has aliasing feature. This enabled me to use my hands' existing muscle memory. It's like using Linux shell commands in Windows. Maybe, this would also be a nice feature for system administrators who works on both environments. Taking advantage of legacy knowledge seems like an attractive value for me.

To display the current directory, type:
pwd

To display items(files/directories) within the current directory, type:
ls

C. Search within files

I was working on a task last year which requires me to search all instances of a database table within SQL files in a hierarchy of directories. So, my common sense told me to use the Windows Search. To my surprise, it didn't retrieve any result. But, when I opened a certain file, I saw the table I was looking for. It seems windows search got a problem searching the contents of some files like SQL. I wish I already met PowerShell that time so that i can do something like the one below.

get-childitem -recurse -include *.sql | select-string 'special table' | group-object filename

What do I need to get started?

Here's the link to the PowerShell download:

Here's the link to the documentation pack:

Thank you very much for reading this post. Happy Scripting!!!