Skip to content

Instantly share code, notes, and snippets.

@realeroberto
Last active August 29, 2015 14:15
Show Gist options
  • Save realeroberto/9b3c601dcfe7acef5f0c to your computer and use it in GitHub Desktop.
Save realeroberto/9b3c601dcfe7acef5f0c to your computer and use it in GitHub Desktop.
The Trabb Pardo–Knuth algorithm.
#
# Trabb Pardo–Knuth algorithm
#
# see http://rosettacode.org/wiki/Trabb_Pardo%E2%80%93Knuth_algorithm
#
function TPK_function($x)
{
return [math]::pow([math]::abs($x), .5) + 5 * [math]::pow($x, 3)
}
function TPK([string] $f)
{
[double[]] $values
# ask for 11 numbers to be read into a sequence S
Write-Host "Please enter 11 numbers"
1..11 |% { $values += @(Read-Host $_) }
# for each item in sequence S
1..11 |% {
# reverse sequence S
$v = $values[11-$_]
# result := call a function to do an operation
$result = Invoke-Expression "$f $v"
if ($result -gt 400) {
# if result overflows alert user!
Write-Error "Result too large!"
} else {
# print result
Write-Host "f($v) = $result"
}
}
}
TPK "TPK_function"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment