• Advertisement
Sign in to follow this  

Trying to solve mathematical problem using programing skills

This topic is 1878 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello.
The question i was posed is:
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.
The program i made
[CODE]
int main()
{
int sum = 0;

for(int i = 0; i < 1000; i++)
{
if((i % 3) == 0)
{
sum += i;
}
if((i%5) == 0)
{
sum += i;
}
}
std::cout<<sum<<std::endl;
}
[/CODE]
Output is "266333"
The answer is "233168"
what em i doing wrong?

Share this post


Link to post
Share on other sites
Advertisement
You're accumulating multiples of 15 twice since those numbers are divisible by both 3 and 5. I'm not going to provide a solution though, but let you think about it.

Share this post


Link to post
Share on other sites
[quote name='Brother Bob' timestamp='1354128194' post='5005026']
You're accumulating multiples of 15 twice since those numbers are divisible by both 3 and 5. I'm not going to provide a solution though, but let you think about it.
[/quote]
You don't have to, and thanks for the answer.
I feel oddly stupid.

The output now is now correct
Combined the if statement to one
[CODE]
if((i % 3) == 0 || (i%5) == 0)
{
sum += i;
OStream << i << "\n";
}
[/CODE] Edited by BaneTrapper

Share this post


Link to post
Share on other sites
I feel dumb for only now realizing that Project Euler used a FizzBuzz-like situation on their first problem. :) Edited by Nypyren

Share this post


Link to post
Share on other sites
I think you can solve it in constant time on paper (or with one expression in your programming language), no need for loops :)

Share this post


Link to post
Share on other sites
[quote name='IvanK' timestamp='1354140013' post='5005098']
I think you can solve it in constant time on paper (or with one expression in your programming language), no need for loops
[/quote]

For 3 and 5, yeah, you can for any bound [eqn]n[/eqn] (inclusive, use [eqn]n - 1[/eqn] if you don't want to include it), it's just:

[eqn]\displaystyle \left ( \sum_{k = 1}^{\lfloor n / 3 \rfloor} 3k \right ) + \left ( \sum_{k = 1}^{\lfloor n / 5 \rfloor} 5k \right ) - \left ( \sum_{k = 1}^{\lfloor n / 15 \rfloor} 15k \right )[/eqn]

Which simplifies down to (the floor symbols indicate integer division):

[eqn]\displaystyle \frac{3}{2} \left \lfloor \frac{n}{3} \right \rfloor \left (\left \lfloor \frac{n}{3} \right \rfloor + 1 \right ) + \frac{5}{2} \left \lfloor \frac{n}{5} \right \rfloor \left (\left \lfloor \frac{n}{5} \right \rfloor + 1 \right ) - \frac{15}{2} \left \lfloor \frac{n}{15} \right \rfloor \left (\left \lfloor \frac{n}{15} \right \rfloor + 1 \right )[/eqn]

For [eqn]n = 1000[/eqn], this gives [eqn]234168[/eqn]. For [eqn]n = 999[/eqn], we get [eqn]233168[/eqn] as expected.

You can see the general pattern - sum up the multiples of each number required (here 3 and 5), and then subtract any numbers which are multiples of any two of them at the same time. You can generalize this to more than two numbers, but it gets pretty messy as you need [eqn]2^p - 1[/eqn] individual sums to get the correct result for [eqn]p[/eqn] different numbers, so it gets impractical if you need to consider many different divisors.

Share this post


Link to post
Share on other sites
[quote name='AlexB.hpp' timestamp='1354129591' post='5005033']
Read opened thread for this problem to find out better solution. It's really interesting.
[/quote]
I was asking what is wrong with my code, tho i made mistake.
I tried looking for other codes to see how they where made, i keep crossing on PHP code, so i open thread :P.

[quote name='IvanK' timestamp='1354140013' post='5005098']
I think you can solve it in constant time on paper (or with one expression in your programming language), no need for loops :)
[/quote]
I have so low mathematical knowledge. Don't know these formulas at all.
[quote name='Bacterius' timestamp='1354157932' post='5005183']
For 3 and 5, yeah, you can for any bound [eqn]n[/eqn] (inclusive, use [eqn]n - 1[/eqn] if you don't want to include it), it's just:
[eqn]\displaystyle \left ( \sum_{k = 1}^{\lfloor n / 3 \rfloor} 3k \right ) + \left ( \sum_{k = 1}^{\lfloor n / 5 \rfloor} 5k \right ) - \left ( \sum_{k = 1}^{\lfloor n / 15 \rfloor} 15k \right )[/eqn]
Which simplifies down to (the floor symbols indicate integer division):
[eqn]\displaystyle \frac{3}{2} \left \lfloor \frac{n}{3} \right \rfloor \left (\left \lfloor \frac{n}{3} \right \rfloor + 1 \right ) + \frac{5}{2} \left \lfloor \frac{n}{5} \right \rfloor \left (\left \lfloor \frac{n}{5} \right \rfloor + 1 \right ) - \frac{15}{2} \left \lfloor \frac{n}{15} \right \rfloor \left (\left \lfloor \frac{n}{15} \right \rfloor + 1 \right )[/eqn]
For [eqn]n = 1000[/eqn], this gives [eqn]234168[/eqn]. For [eqn]n = 999[/eqn], we get [eqn]233168[/eqn] as expected.
You can see the general pattern - sum up the multiples of each number required (here 3 and 5), and then subtract any numbers which are multiples of any two of them at the same time. You can generalize this to more than two numbers, but it gets pretty messy as you need [eqn]2^p - 1[/eqn] individual sums to get the correct result for [eqn]p[/eqn] different numbers, so it gets impractical if you need to consider many different divisors.
[/quote]
If they have shown me how to do it from 0 - 1000 id know how to do it from 0 - 10000^^.
But yea, its kinda too much for me since you most know good math for those problems. I didn't get far with it, 3/10 problems solved and am pleased since i don't understand 70% of those questions.
Thanks on good mood.

Share this post


Link to post
Share on other sites
[url="http://www.mathsisfun.com/algebra/sigma-notation.html"]Sigma notation[/url] can be difficult to read if you're not used to it. It's actually quite straightforward, but for some reason large Greek letters can stun the brain for several seconds. (This happens to me often and is probably a sign of poor nutrition or a brain injury resulting from sitting too close to the television as a kid.) Edited by Khatharr

Share this post


Link to post
Share on other sites
[quote name='Khatharr' timestamp='1354192623' post='5005277']
[url="http://www.mathsisfun.com/algebra/sigma-notation.html"]Sigma notation[/url] can be difficult to read if you're not used to it. It's actually quite straightforward, but for some reason large Greek letters can stun the brain for several seconds. (This happens to me often and is probably a sign of poor nutrition or a brain injury resulting from sitting too close to the television as a kid.)
[/quote]Quoted for truth. Its my experience in Mathematical Analysis, Algebra and Algorithms courses summed up in a single quote.

Share this post


Link to post
Share on other sites
Python is great for this kind of thing. I just pull up the shell and hack a quick function together. Great for GCD, LCM, co-primality, etc. Quicker than C++ to compile and run.
I just rewrite the function every session I want to use it, which ends up with me learning the definitions of these things very thoroughly (and degrading my mental arithmetic).

[quote name='Khatharr' timestamp='1354192623' post='5005277']
[url="http://www.mathsisfun.com/algebra/sigma-notation.html"]Sigma notation[/url] can be difficult to read if you're not used to it. It's actually quite straightforward, but for some reason large Greek letters can stun the brain for several seconds. (This happens to me often and is probably a sign of poor nutrition or a brain injury resulting from sitting too close to the television as a kid.)
[/quote]

What scares you more, a sigma with a large expression or an integral sign with a large expression?

Share this post


Link to post
Share on other sites
I love math notation for two reasons.
1) Being able to read it feels like translating hieroglyphics, which is kind of fun
2) It is incredibly condensed communication: throw 4 symbols down on paper and you've just described a vector space, or a specific group of values, or an entire relationship between values

Especially things like set notation descriptions, those looked like gibberish to me until I sat through a discrete math course.

Anyhow, Project Euler is interesting because there's groups of people who are so focused on the most elegant or minimalist answer, and there's people who want to use their language of choice to solve the problems in any means necessary (treating it as an art or a learning task, respectively). The bottom line is still the same though, so I never give someone crap for using a brute-force or messy algorithm: programming lets us solve problems and complete tasks in a fraction of the time it would take to obtain the answer in meat-space.

...Except that problem asking for the nth prime, looking up prime numbers on the internet is just way too easy.

Share this post


Link to post
Share on other sites
[quote name='hupsilardee' timestamp='1354200231' post='5005306']
What scares you more, a sigma with a large expression or an integral sign with a large expression?
[/quote]
Depends if you're asking for series simplification or not. I loved calculus because it was algebra on steroids and felt like number alchemy. But I was always crap with series. The one place I loved seeing sigmas was in induction proofs.

Share this post


Link to post
Share on other sites
[quote name='hupsilardee' timestamp='1354200231' post='5005306']
Python is great for this kind of thing. I just pull up the shell and hack a quick function together. Great for GCD, LCM, co-primality, etc. Quicker than C++ to compile and run.
I just rewrite the function every session I want to use it, which ends up with me learning the definitions of these things very thoroughly (and degrading my mental arithmetic).
[/quote]
Python is pretty good, but you'd be amazed at how well Haskell does with Project Euler.

There are tons of problems that it just chews through with no thought. Which is exactly what you'd expect from a purely functional language, really [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement