Question:
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
F#
1 2 |
let answer = [1..999] |> Seq.where(fun x -> x % 5 = 0 || x % 3 = 0) |> Seq.sum printfn "%i" answer |
or
1 2 3 4 5 6 7 |
let value = ref 0 for i in [1..999] do if (i % 3 = 0 || i % 5 = 0) then value := value.Value + i printfn "%i" value.Value |
C#
1 2 |
var value = Enumerable.Range(1,999).Where(x=>x % 3 == 0 || x % 5 == 0).Sum(); Console.WriteLine(value); |
or
1 2 3 4 5 6 7 8 9 10 |
var value = 0; for(var ii = 1; ii < 1000; ii++) { if(ii % 3 == 0 || ii % 5 == 0) { value += ii; } } Console.WriteLine(value); |
JavaScript
1 2 3 4 5 6 7 8 9 |
var ii, val = 0; for(ii = 0; ii < 1000; ii++){ if(ii % 3 === 0 || ii % 5 === 0){ val += ii } } alert (val); |
SQL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
DECLARE @i INT DECLARE @total INT SET @i = 0 SET @total = 0 WHILE @i < 1000 BEGIN IF @i % 3 = 0 OR @i % 5 = 0 BEGIN SET @total = @total + @i END SET @i = @i + 1 END PRINT @Total |
or
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
IF EXISTS(SELECT * FROM sys.objects WHERE NAME = 'MakeRangeTable') BEGIN DROP FUNCTION MakeRangeTable END GO CREATE FUNCTION MakeRangeTable (@maxNumber INT) RETURNS @Numbers TABLE ( Number INT NOT NULL ) AS BEGIN DECLARE @i INT SET @i = 1 WHILE @i < 1000 BEGIN INSERT INTO @Numbers(Number) VALUES(@i) SET @i = @i + 1 END RETURN END GO SELECT SUM(Number) FROM MakeRangeTable(999) WHERE Number % 3 = 0 OR Number % 5 = 0 |
PowerShell
1 |
1..999 | Where {($_ % 3 -eq 0) -or ($_ % 5 -eq 0)} | Measure -sum |
or
1 2 3 4 5 6 7 8 9 |
$value = 0 foreach ($i in 1..999) { if($i % 3 -eq 0 -or $i % 5 -eq 0){ $value += $i } } Write-Output $value |