The problem at hand is to find the sum of every numbers less than a offered number N which room divisible by 3 and/ or 5. Us will explore this problem and show multiple options with a constant time O(1) equipment as well.

You are watching: Sum of all multiples of 3 and 5 below 1000

In this problem, you gain the idea of how to gain the amount of consecutive integers in consistent time without actually including them. Through the idea the the problem, you can solve the Problem 1 the Project Euler together well.

Example:

If we take into consideration N=20, then the number that space divisible by 3 and/ or 5 are:

3, 5, 6, 9, 10, 12, 15, 18, 20

The sum of the numbers is 98. Hence, 98 is ours answer for N = 20.

Basic idea to fix this problem efficiently is:

Sum the integers native 1 come N = N * (N-1) / 2

Simple solution O(N)

The an easy solution is come traverse with all aspects from 1 to N and check because that each aspect if that is divisible through 3 or 5. If that is divisible, us shall include it to a typical variable.

Pseudocode:

sum = 0;for i from 1 come N if ns % 3 == 0 OR i % 5 == 0 amount += i;If we assume that modulas operation takes consistent time, the in its entirety complexity of the algorithm is O(N).

In reality, operations choose multiplication and modulas bring away O(log N) time and also hence, the time intricacy of this method will it is in O(N log N).

Implementation:

class lungemine.com { static int find_sum(int N) { int sum = 0; for(int ns = 1; ns Output:

23331668This strategy works rapid for smaller numbers and even, us go to greater numbers choose 10^9, this i do not care slow. To handle this, we will construct our constant time algorithm.

Efficient approach

We will solve this problem in continuous time.

Let us settle the minimal version of the problem where we need to uncover the sum of numbers that room divisible by 3. For this, we can take the previous technlungemine.comue of checking each variety of take benefit of a fundamental mathematical property.

The residential property is:

Sum of numbers from 1 to N is N * (N-1) / 2.

This way the amount of number from 1 to 1000 is just 1000 * 999/2 = 499500.

You need to understand just how this will help us.

Consider the very first 5 multiples the 3 that is:

3, 6, 9, 12, 15 which is exact same as 3 multiplied by the an initial 5 integers (1, 2, 3, 4, 5).

Sum of an initial 5 multiples that 3== 3 + 6 + 9 + 12 + 15= 3 * (1 + 2 + 3 + 4 + 5)= 3 * (sum of very first 5 integers)

We recognize the amount of very first 5 integers together 5 * 4/2 = 10. So, the amount of very first 5 multiples of 3 is 3 * 10 that is 30.

To uncover the amount of number M * 3 NThis have the right to be easily determined by separating N through 3 and taking the floor that the number. Different approach is to find the modulus the N through 3 and also subtract the from N which we deserve to then division by 3.

Upper border of lot of of 3 series = M

M = floor (N / 3) orM = (N - N%3) / 3 Hence, for any kind of given N, to uncover the amount of numbers divisible through 3 and less 보다 N:

We need to discover MSum = 3 * M * (M-1) / 2

Similarly, for 5:

M = floor (N / 5) orM = (N - N % 5) / 5 amount = 5 * M * (M-1) / 2We can find the sum of multiples of 3 and also 5 separately. At this point, if we add both, we acquire a number which has the multiples of 15 (3 * 5) added twice. To acquire our ultimate answer, we must subtract multiples that 15 native the sum.

This is simple as we can use our insights to get the amount of multiples the 15.

See more: Have A Wonderful Day In Italian, Hope You Have A Wonderful Day

Hence, the last solution is:

Sum the numbers much less than N which room multiples that 3 = amount of multiples the 3 + amount of multiples that 5 - amount of multiples of 15Find the sum of multiples the 3 or any given number takes constant time O(1) if girlfriend consider simple operations like multiplication take away O(1) time.

Pseudocode:

find_sum(N, multiple) M = (N - N % multiple) / multiple; sum = lot of * M * (M-1) / 2; return sum;sum = find_sum(N, 3) + find_sum(N, 5) - find_sum(N, 15)Implementation:

class lungemine.com revolution int find_sum(int N, int multiple) int M = (int)((N - N % multiple) / multiple); int amount = (int)(multiple * M * (M-1) / 2); return sum; public revolution void key (String<> args) int N = 10000;int sum = find_sum(N, 3) + find_sum(N, 5) - find_sum(N, 15);System.out.println(sum);Output:

23331659

*
lungemine.com structure The official account the lungemine.com lungemine.com backed by GitHub, DigitalOcean and Discourse