How to resolve the algorithm Sum multiples of 3 and 5 step by step in the Groovy 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 Groovy 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 Groovy programming language
Source code in the groovy programming language
def sumMul = { n, f -> BigInteger n1 = (n - 1) / f; f * n1 * (n1 + 1) / 2 }
def sum35 = { sumMul(it, 3) + sumMul(it, 5) - sumMul(it, 15) }
[(1000): 233168, (10e20): 233333333333333333333166666666666666666668].each { arg, value ->
println "Checking $arg == $value"
assert sum35(arg) == value
}
You may also check:How to resolve the algorithm Filter step by step in the PHP programming language
You may also check:How to resolve the algorithm Quickselect algorithm step by step in the Scala programming language
You may also check:How to resolve the algorithm MD5 step by step in the C++ programming language
You may also check:How to resolve the algorithm SEDOLs step by step in the Action! programming language
You may also check:How to resolve the algorithm Play recorded sounds step by step in the Java programming language