Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
$range = 1, 2, 4, 8, 16, 32, 62, 64, 65, 127
$numbers = New-Object "System.Collections.Generic.List[Int]"
$numbers = $range
$target = 65
Function Get-SumUp {
[CmdletBinding()]
param
(
[System.Collections.Generic.List[Int]]
$numbers,
[int16]
$target
)
$part = New-Object "System.Collections.Generic.List[Int]"
Get-SumUpRecursive -numbers $numbers -target $target -part $part
}
Function Get-SumUpRecursive {
param
(
[System.Collections.Generic.List[Int]]
$numbers,
[int16]
$target,
[System.Collections.Generic.List[Int]]
$part
)
[int16]$result = 0
foreach ($p in $part) {
$result += $p
}
if ($result -eq $target) {
$output = New-Object -TypeName PSObject
$output | Add-Member -MemberType NoteProperty -Name part -Value "$($part)"
$output
}
if ($result -ge $target) {
return
}
for ($i = 0; $i -lt $numbers.Count; $i++) {
$leftovers = New-Object "System.Collections.Generic.List[Int]"
[int16]$number = $numbers[$i]
for ($j = $i + 1; $j -lt $numbers.Count; $j++) {
$leftovers.Add($numbers[$j])
}
$partList = New-Object "System.Collections.Generic.List[Int]"
foreach ($l in $part) {
$partList.Add($l)
}
$partList.Add($number)
Get-SumUpRecursive -numbers $leftovers -target $target -part $partList
}
}
$combi = @{}
Measure-Command {$combi = Get-SumUp -numbers $numbers -target $target}
#here we can do whatever we need to do, this is just splittingthe results back into arrays.
if ($combi.part.count -gt 1) {
[string[]]$combo = $combi.part[-1].Split()
}
else {
[string[]]$combo = $combi.part.Split()
}
[int[]]$combee = [int[]]$combo
foreach ($c in $combee) {
write-Host $c
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment