• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
BaneTrapper

Trying to solve mathematical problem using programing skills

13 posts in this topic

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?
0

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
0

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
2

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 :)
2

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.
0

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
1

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.
0

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?
0

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.
0

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.
0

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]
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0