How to resolve the algorithm Sum multiples of 3 and 5 step by step in the Picat 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 Picat 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 Picat programming language
Source code in the picat programming language
sumdiv(N, D) = S =>
M = N div D,
S = (M*(M + 1) div 2) * D.
sum35big(N) = sumdiv(N, 3) + sumdiv(N, 5) - sumdiv(N, 15).
main =>
Upto1K = [N: N in 1..999, (N mod 3 = 0; N mod 5 = 0)].sum,
writef("The sum of all multiples of 3 and 5 below 1000 is %w%n", Upto1K),
writef("The sum of all multiples less than 1e20 is %w%n", sum35big(99999_99999_99999_99999)).
You may also check:How to resolve the algorithm Matrix multiplication step by step in the Nim programming language
You may also check:How to resolve the algorithm Sorting Algorithms/Circle Sort step by step in the Wren programming language
You may also check:How to resolve the algorithm 9 billion names of God the integer step by step in the Stata programming language
You may also check:How to resolve the algorithm Mad Libs step by step in the AppleScript programming language
You may also check:How to resolve the algorithm Variables step by step in the TUSCRIPT programming language