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

  • Thread starter Thread starter miniradman
  • Start date Start date
  • Tags Tags
    Matlab Sum
Click For Summary

Discussion Overview

The discussion revolves around solving Project Euler Problem 1, which involves finding the sum of all natural numbers below 1000 that are multiples of 3 or 5. Participants explore various approaches to implement the solution in MATLAB, including algorithmic strategies and potential pitfalls in counting multiples.

Discussion Character

  • Homework-related
  • Mathematical reasoning
  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant describes their initial approach using arrays to generate multiples of 3 and 5, expressing confusion over an incorrect result.
  • Another participant points out the issue of double-counting multiples of 15, which leads to a correction in the participant's approach.
  • A suggestion is made to avoid hard-coding array limits by calculating them based on an arbitrary upper limit, N.
  • Another participant proposes using the "floor" command in MATLAB to determine limits dynamically, while also considering the implications of using loops versus modularization.
  • A later reply introduces the idea of using a direct formula for the sum of the first N integers to generalize the solution, although it is noted that this is not necessary for solving the original problem.

Areas of Agreement / Disagreement

Participants generally agree on the need to address the issue of double-counting multiples of 15. However, there is no consensus on the best approach to implement the solution, as multiple strategies are proposed and discussed.

Contextual Notes

Limitations include the reliance on specific array limits and the potential complexity introduced by different methods of summation. The discussion does not resolve the best approach to avoid creating arrays or the implications of using loops.

miniradman
Messages
191
Reaction score
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
What happens with 15?
How many times do you count it in your algorithm?
 
  • Like
Likes   Reactions: 1 person
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:
 
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]?
 
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 :-p
 
Last edited:
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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
Replies
4
Views
2K
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 32 ·
2
Replies
32
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 21 ·
Replies
21
Views
3K