How to resolve the algorithm Sum multiples of 3 and 5 step by step in the Julia programming language
How to resolve the algorithm Sum multiples of 3 and 5 step by step in the Julia 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 Julia programming language
The provided Julia code contains two functions for calculating the sum of multiples of two or three numbers up to a specified limit, while excluding the sum of multiples of their least common multiple (LCM).
multsum(n, m, lim)
This function calculates the sum of multiples of two numbers, n and m, up to a given limit, lim. It does this by first calculating the sum of multiples of n up to the limit, then calculating the sum of multiples of m up to the limit, and finally subtracting the sum of multiples of their LCM.
Explanation:
sum(0:n:lim-1)
: This calculates the sum of multiples of n up to the limit, excluding the limit itself. It creates a range from 0 to lim-1, with a step size of n, and sums the values in that range.sum(0:m:lim-1)
: This calculates the sum of multiples of m up to the limit, excluding the limit itself.sum(0:lcm(n,m):lim-1)
: This calculates the sum of multiples of the least common multiple (LCM) of n and m up to the limit, excluding the limit itself. The LCM is the smallest positive integer that is divisible by both n and m.
The result of these subtractions is the sum of multiples of n and m up to the limit, excluding the sum of multiples of their LCM.
multsum(n, lim)
This function is a simplified version of the previous one, designed to calculate the sum of multiples of a single number, n, up to a given limit, lim. It uses a more efficient formula to calculate the sum directly, without the need for the subtraction step.
Explanation:
occ = div(lim-1, n)
: This calculates the number of times n "fits" into lim-1. It divides lim-1 by n and rounds the result down to an integer.div(n*occ*(occ+1), 2)
: This calculates the sum of multiples of n up to lim-1 using a mathematical formula. The formula is (n * occ * (occ + 1)) / 2, where occ is the number of times n fits into lim-1.
In summary, these functions efficiently calculate the sum of multiples of two or three numbers up to a specified limit, while excluding the sum of multiples of their LCM. The multsum(n, m, lim)
function is designed for general-purpose use, while the multsum(n, lim)
function is optimized for calculating sums of multiples of a single number.
Source code in the julia programming language
multsum(n, m, lim) = sum(0:n:lim-1) + sum(0:m:lim-1) - sum(0:lcm(n,m):lim-1)
multsum(n, lim) = (occ = div(lim-1, n); div(n*occ*(occ+1), 2))
multsum(n, m, lim) = multsum(n, lim) + multsum(m, lim) - multsum(lcm(n,m), lim)
You may also check:How to resolve the algorithm Random number generator (included) step by step in the Inform 7 programming language
You may also check:How to resolve the algorithm Arithmetic/Rational step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Combinations step by step in the Scala programming language
You may also check:How to resolve the algorithm Vector products step by step in the Factor programming language
You may also check:How to resolve the algorithm Hello world/Newbie step by step in the Pixilang programming language