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, …

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

function fib {
[cmdletbinding()]
	param ( [int] $x )
	process {
		if ($x -lt 2) {
			$x
		} else {
			$n1 = $x - 1
			$n2 = $x - 2
			$fib2 = fib $n2
			$fib1 = fib $n1
			$result = $fib1 + $fib2	
			#Write-Verbose $result
			$result
		}		
	}
}

#fib 5 -Verbose

Write-Host "___________-------------______________"

$ScriptStartTime =  date

for ($i = 0; $fib -lt 4000000 ; $i+=1) {
	$fib = fib $i

	Write-Host -ForegroundColor Magenta "fib = $fib"

	If ($fib % 2 -eq 0 ) {
		$sum += $fib
		Write-Host -ForegroundColor Green "sum = $sum"
	}
}

$ScriptEndTime = date

$ScriptDuration = $ScriptEndTime - $ScriptStartTime

Write-Host "`n Time Taken:" + $ScriptDuration