Finding the sum of multiples of 3 and 5 below 1000 (Matlab)

  • Thread starter miniradman
  • Start date
  • Tags
    Matlab Sum
In summary, the person is trying to find the sum of a list of integers, but is not sure how to do it.
  • #1
miniradman
196
0

Homework Statement


Project Euler Problem 1.

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

Hello, there

I do understand what a multiple is, and how to sum and bunch of numbers... but I have no idea why my algorithm is not delivering the correct result.

When I multiply the array [1:333] by 3, shouldn't I get all the multiples of 3 below 1000? (3, 6, 9... etc), and the same goes for the [1:199] by 5 right? This is a really simple problem, but I don't see where I'm going wrong here :cry:

2. The attempt at a solution
clc
clear

x3 = [1:333]*3
x5 = [1:199]*5

all_multi_array = [x3,x5]

sum(all_multi_array)
 
Physics news on Phys.org
  • #2
What happens with 15?
How many times do you count it in your algorithm?
 
  • Like
Likes 1 person
  • #3
ahhh... I count it twice!

and the same goes for 30, 45, 60, etc I'm guessing...

EDIT:
Yes, it returned a correct result for my following algorithm

clc
clear

x3 = [1:333]*3
x5 = [1:199]*5

all_multi_array = [x3,x5]

sum(all_multi_array) - sum((15)*[1:66])

Thanks for the hint mfb :thumbs:
 
  • #4
Rather than hard-coding those array limits (333, 199, 66), wouldn't it be better to calculate them given some arbitrary N, where in this case N happens to be 1000?

Without forming those arrays, what is the sum of [1:333]? [1:199]? [1:66]?
 
  • #5
I guess it would add some extra scope to the code. I could make N = 999 (because it must be less than 1000) and use the "floor" command in matab to round the numbers when I divide them by 3,5 and 15 respectively.

EDIT:
The only way I could think of finding the sum of those numbers without creating an array, would be with loops. But that's the opposite of modularisation... so I'm not sure :tongue:
 
Last edited:
  • #6
There is a direct formula for the sum of the first N integers, and you can adapt it to your sums.

It is not necessary to solve the original problem, but it is still nice to see how it can be generalized.
 

1. What is the purpose of finding the sum of multiples of 3 and 5 below 1000?

The purpose of this task is to practice using Matlab to perform calculations and to see the relationship between multiples of 3 and 5.

2. How do you find the sum of multiples of 3 and 5 below 1000 in Matlab?

To find the sum of multiples of 3 and 5 below 1000 in Matlab, you can use a for loop to iterate through the numbers from 1 to 999 and check if each number is a multiple of 3 or 5. If it is, then add it to a running sum. This sum will be the total sum of all the multiples of 3 and 5 below 1000.

3. What is the most efficient way to find the sum of multiples of 3 and 5 below 1000?

The most efficient way to find the sum of multiples of 3 and 5 below 1000 is to use the formula for the sum of an arithmetic sequence. This formula is given by Sn = n/2(a1 + an), where Sn is the sum, n is the number of terms, a1 is the first term, and an is the last term. In this case, n can be calculated by dividing 999 (the highest multiple below 1000) by 3 and 5, and then subtracting the overlap (multiples of both 3 and 5). The first term, a1, is 3 and the last term, an, is 999 for multiples of 3, and 5 for multiples of 5. The two sums can then be added together to get the final sum of multiples of 3 and 5 below 1000.

4. Can this task be generalized to find the sum of multiples of any two numbers below a given limit?

Yes, this task can be generalized to find the sum of multiples of any two numbers below a given limit. The same approach can be used, but with the numbers and limit being input by the user or stored in variables.

5. What other applications can this task have in real-world problems?

Finding the sum of multiples of numbers can be useful in a variety of real-world problems, such as calculating the total cost of items that come in multiple units, determining the total distance traveled by a vehicle that stops at multiple points, or finding the total energy consumption of a system that operates at different power levels. It can also be used in financial calculations, such as finding the total profit from multiple investments or determining the total cost of a loan with varying interest rates.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
32
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Precalculus Mathematics Homework Help
Replies
11
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Set Theory, Logic, Probability, Statistics
Replies
12
Views
970
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
Back
Top