How to resolve the algorithm Sum multiples of 3 and 5 step by step in the jq 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 jq 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 jq programming language
Source code in the jq programming language
def sum_multiples(d):
((./d) | floor) | (d * . * (.+1))/2 ;
# Sum of multiples of a or b that are less than . (the input)
def task(a;b):
. - 1
| sum_multiples(a) + sum_multiples(b) - sum_multiples(a*b);
1000 | task(3;5) # => 233168
10e20 | task(3;5) # => 2.333333333333333e+41
You may also check:How to resolve the algorithm Generate lower case ASCII alphabet step by step in the ALGOL W programming language
You may also check:How to resolve the algorithm Giuga numbers step by step in the Perl programming language
You may also check:How to resolve the algorithm Real constants and functions step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Roots of unity step by step in the BASIC programming language
You may also check:How to resolve the algorithm Dice game probabilities step by step in the D programming language