Monday, July 2, 2012

Euler Problem 12

I skipped a number of Euler problems since my last post. Anyway, here’s my powershell solution to Problem 12.

http://projecteuler.net/problem=12

It’s not that elegant but it solved the problem in less than 4mins :-)

  1. function Count-Divisors($num
  2.     $totalDivisors = 2 
  3.     $subTotal = 0 
  4.     $localDivisor = 2 
  5.     $localNum = $num 
  6.      
  7.     while($localNum -gt 1) 
  8.     { 
  9.         if(($localNum%$localDivisor) -eq 0) 
  10.         { 
  11.             $subTotal++ 
  12.             $localNum/=$localDivisor 
  13.         } 
  14.         else 
  15.         { 
  16.             if($subTotal -gt 0) 
  17.             { 
  18.                 $totalDivisors*=($subTotal+1) 
  19.                 $subTotal = 0 
  20.             } 
  21.             $localDivisor++ 
  22.         } 
  23.     } 
  24.      
  25.     return $totalDivisors 
  26.  
  27. function Get-FTNumber($start, $divisors
  28.     $i=$start 
  29.     while ((Count-Divisors ($i * ($i+1)/2)) -le $divisors
  30.     { 
  31.         $i++ 
  32.     } 
  33.      
  34.     return ($i * ($i+1)/2) 
  35.  
  36. echo (Get-FTNumber 2 500)