Problem 1

Problem

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\).

Julia

function p1()
  [x for x  1:999 if (x % 3 == 0 || x % 5 == 0)] |> sum
end;

p1()
233168
using BenchmarkTools;
@benchmark p1()
BenchmarkTools.Trial: 10000 samples with 8 evaluations.
 Range (minmax):  3.540 μs258.099 μs   GC (min … max): 0.00% … 95.67%
 Time  (median):     3.712 μs                GC (median):    0.00%
 Time  (mean ± σ):   4.299 μs ±   5.918 μs   GC (mean ± σ):  4.68% ±  3.41%
  ▅██▄▂▁▁▁▁     ▂▄▅▄▄▃▂▂▃▂▂▁                                 ▂
  ███████████▇▆█████████████████▇▇▇▆▇▇███▆▆▅▅▆▆▅▆▄▆▅▄▄▄▅▄▃▄ █
  3.54 μs      Histogram: log(frequency) by time      7.05 μs <
 Memory estimate: 7.61 KiB, allocs estimate: 6.