Sha256: 3620452f4220babe80bd0f82e740a88bf29106c37e2a43fd6b37a49def23b3ce
Contents?: true
Size: 914 Bytes
Versions: 11
Compression:
Stored size: 914 Bytes
Contents
Function Get-SumOfMultiples { <# .SYNOPSIS Given a number, find the sum of all the unique multiples of particular numbers up to but not including that number. .DESCRIPTION If we list all the natural numbers below 20 that are multiples of 3 or 5, we get 3, 5, 6, 9, 10, 12, 15, and 18. .PARAMETER Multiples An array of the factors .PARAMETER Limit The value BELOW which we test for .EXAMPLE Get-SumOfMultiples -Multiples @(3, 5) -Limit 10 Returns 23 #> [CmdletBinding()] Param( [int[]]$Multiples, [int]$Limit ) $result = 0; for($i = 1; $i -lt $Limit; $i ++) { foreach($multiple in $Multiples) { if ($i % $multiple -eq 0) { $result += $i break # Break out of the foreach look because we've found a factor } } } Return $result; }
Version data entries
11 entries across 11 versions & 1 rubygems