How to resolve the algorithm Sum multiples of 3 and 5 step by step in the F# programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sum multiples of 3 and 5 step by step in the F# programming language
Table of Contents
Problem Statement
The objective is to write a function that finds the sum of all positive multiples of 3 or 5 below n. Show output for n = 1000. This is is the same as Project Euler problem 1. Extra credit: do this efficiently for n = 1e20 or higher.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sum multiples of 3 and 5 step by step in the F# programming language
Source code in the fsharp programming language
let sum35 n = Seq.init n (id) |> Seq.reduce (fun sum i -> if i % 3 = 0 || i % 5 = 0 then sum + i else sum)
printfn "%d" (sum35 1000)
printfn "----------"
let sumUpTo (n : bigint) = n * (n + 1I) / 2I
let sumMultsBelow k n = k * (sumUpTo ((n-1I)/k))
let sum35fast n = (sumMultsBelow 3I n) + (sumMultsBelow 5I n) - (sumMultsBelow 15I n)
[for i = 0 to 30 do yield i]
|> List.iter (fun i -> printfn "%A" (sum35fast (bigint.Pow(10I, i))))
You may also check:How to resolve the algorithm Mandelbrot set step by step in the Python programming language
You may also check:How to resolve the algorithm Loops/Downward for step by step in the Verilog programming language
You may also check:How to resolve the algorithm Display an outline as a nested table step by step in the J programming language
You may also check:How to resolve the algorithm Read a configuration file step by step in the Raku programming language
You may also check:How to resolve the algorithm Van der Corput sequence step by step in the Modula-2 programming language