Sum multiples of 3 and 5: Difference between revisions

Line 2,541:
 
=={{header|PowerShell}}==
{{incorrect|PowerShell|Output should be 233168}}
(I believe this is missing $Upto = $Upto - 1 [twice])
<lang powershell>
function SumMultiples ( [int]$Base, [int]$Upto )
{
$X = ( $Upto - ( $Upto % $Base ) ) / $Base + ( [int] ( $Upto % $Base -ne 0 ) )
$Sum = ( $X * $X +- $X ) * $Base / 2
Return $Sum
}
Line 2,556 ⟶ 2,554:
{{out}}
<pre>
233168
234168
</pre>
SimplyFor arbitrarily large integers, simply change the variable type to handle really, really big number.
<lang powershell>
function SumMultiples ( [bigint]$Base, [bigint]$Upto )
{
$X = ( $Upto - ( $Upto % $Base ) ) / $Base + ( [int] ( $Upto % $Base -ne 0 ) )
$Sum = ( $X * $X +- $X ) * $Base / 2
Return $Sum
}
Line 2,573 ⟶ 2,571:
{{out}}
<pre>
233333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333166666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666668
233333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333334166666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666668
</pre>
Here is a cmdlet that will provide the sum of unique multiples of any group of numbers below a given limit. I haven't attempted the extra credit here as the math is too complex for me at the moment.
Anonymous user