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 :-)
- function Count-Divisors($num)
- {
- $totalDivisors = 2
- $subTotal = 0
- $localDivisor = 2
- $localNum = $num
- while($localNum -gt 1)
- {
- if(($localNum%$localDivisor) -eq 0)
- {
- $subTotal++
- $localNum/=$localDivisor
- }
- else
- {
- if($subTotal -gt 0)
- {
- $totalDivisors*=($subTotal+1)
- $subTotal = 0
- }
- $localDivisor++
- }
- }
- return $totalDivisors
- }
- function Get-FTNumber($start, $divisors)
- {
- $i=$start
- while ((Count-Divisors ($i * ($i+1)/2)) -le $divisors)
- {
- $i++
- }
- return ($i * ($i+1)/2)
- }
- echo (Get-FTNumber 2 500)