Project Euler: Problem 1

Discussion in 'Archives' started by verdammt ubel, Dec 2, 2008.

Project Euler: Problem 1
  1. Unread #1 - Dec 2, 2008 at 3:15 PM
  2. verdammt ubel
    Referrals:
    0

    verdammt ubel Guest

    Project Euler: Problem 1

    The first problem listed in Project Euler is this;

    "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 problem is split into several parts, they will be tackled one after the other.

    Problem 1: Counting Natural Numbers

    The only real barrier to entry to this part of the problem is the vocabulary. A natural number is any non-negative whole integer. To us, this means that 0, 1, 2 and 3, and all the whole numbers after, are natural numbers. We can, therefore, count natural numbers with a simple loop, and since we have a fixed range of numbers to iterate through, we will use a "for" loop. The non-specific syntax for a for loop is;

    Code:
    for(initialisation;end-condition;increment);
    As such, our program currently looks like this;

    Code:
    #include <stdio.h> // This include lets us write to the console, among other things
    
    // int i will be our "counter" variable, to test for the rules of the problem
    
    int main() {
      for(int i=0;i<1000;++i) { // i is set to 0, and i must be incremented until it is no longer less than 1000
      }
    }
    Problem 2: Testing for Multiples

    This is where we will test the integer. It's simple C to test a variable for divisibility;

    Code:
    #include <stdio.h> // This include lets us write to the console, among other things
    
    // int i will be our "counter" variable, to test for the rules of the problem
    
    int main() {
      for(int i=0;i<1000;++i) { // i is set to 0, and i must be incremented until it is no longer less than 1000
        if (i%3==0||i%5==0) // This conditional statement tests if i is divisible first by 3, and then by 5. Being divisible by either or both will trigger the conditional.
      }
    }
    Problem 3: Accumulating our Total

    So now we have code to test all the natural numbers up to 1000, but we still don't know the answer to the problem, so let's finish up and find out. We can use an if statement to check if i fits the rules, and then add it to another variable, an integer, to produce the sum of the natural numbers fitting the rules, and so answer the problem. We will increment the accumulator variable by the value of i each time our conditional is triggered. Then, following the for loop we discussed earlier, we will output the accumulator variable to the command line, and return 0 (The return code for successful execution).

    Do not read further unless you've attempted to write the code for this problem yourself first. The code to complete the problem follows;









    Code:
    #include <stdio.h>
    
    int accumulator = 0;
    
    int main() {
        for(int i=0;i<1000;++i) {
            if(i%3==0||i%5==0) {
                accumulator += i;
            }
        }
        printf("%i\n",accumulator);
        getchar();
        return 0;
    }
    Please feel free to criticise me or ask for more details -- I'd be glad to flesh out the sections you specify.
     
  3. Unread #2 - Dec 2, 2008 at 3:36 PM
  4. Xploit!
    Joined:
    Jun 29, 2008
    Posts:
    662
    Referrals:
    2
    Sythe Gold:
    0

    Xploit! Apprentice
    Banned

    Project Euler: Problem 1

    This is very good..But Where would I ever use this just wondering??
    8/10
     
  5. Unread #3 - Dec 2, 2008 at 3:43 PM
  6. verdammt ubel
    Referrals:
    0

    verdammt ubel Guest

    Project Euler: Problem 1

    Where would you ever use C, or where would you ever use math?
     
  7. Unread #4 - Dec 2, 2008 at 3:52 PM
  8. OldFinn
    Joined:
    Jul 23, 2007
    Posts:
    7,094
    Referrals:
    60
    Sythe Gold:
    1

    OldFinn Hero
    Banned

    Project Euler: Problem 1

    Excellent guide, 10/10.
     
  9. Unread #5 - Dec 2, 2008 at 4:00 PM
  10. antchak
    Joined:
    Oct 20, 2008
    Posts:
    559
    Referrals:
    1
    Sythe Gold:
    0

    antchak Forum Addict
    Banned

    Project Euler: Problem 1

    I like Rawr Im Phat's guides better. But it is good all the same. (For all those that don't know this was made because he insulted Rawr and said he could make better guides). 7/10
     
  11. Unread #6 - Dec 2, 2008 at 4:13 PM
  12. verdammt ubel
    Referrals:
    0

    verdammt ubel Guest

    Project Euler: Problem 1

    Was it the superior detail, the better writing quality, the lack of grammar, spelling, and proof-reading mistakes, or the harder subject matter that bothered you?
     
  13. Unread #7 - Dec 2, 2008 at 4:29 PM
  14. Tru3 Mast3r
    Joined:
    Nov 12, 2008
    Posts:
    401
    Referrals:
    0
    Sythe Gold:
    0

    Tru3 Mast3r Forum Addict
    Do Not Trade

    Project Euler: Problem 1

    nice guide.... nice detail....9/10
     
  15. Unread #8 - Dec 2, 2008 at 4:39 PM
  16. The Fat Controller
    Joined:
    Aug 16, 2007
    Posts:
    1,003
    Referrals:
    0
    Sythe Gold:
    1

    The Fat Controller Guru

    Project Euler: Problem 1

    You have a bad attitude, but the guide is well written. Try dumbing it down further so the majority of people will understand it.
     
  17. Unread #9 - Dec 2, 2008 at 4:54 PM
  18. Farcast
    Joined:
    Nov 3, 2008
    Posts:
    11,754
    Referrals:
    75
    Sythe Gold:
    499
    Steam Account Verifier Sythe's 10th Anniversary Community Participant

    Farcast Lord
    Retired Global Moderator Zombie $50 USD Donor

    Project Euler: Problem 1

    Surprisingly well written guide.

    10/10
     
  19. Unread #10 - Dec 2, 2008 at 5:02 PM
  20. verdammt ubel
    Referrals:
    0

    verdammt ubel Guest

    Project Euler: Problem 1

    I should describe the syntax of C, first, but I wanted to make a guide on how to solve a math problem, not a glorified hello world.
     
  21. Unread #11 - Dec 2, 2008 at 5:40 PM
  22. Inventor
    Referrals:
    0

    Inventor Guest

    Project Euler: Problem 1

    It's a justified attitude. Not a bad one..

    People take sides with user educators because they "have a rank" for the sake of doing so.

    Frankly, It's sad.

    Nice guide but certainly not one for me since I hate math. Haha
     
  23. Unread #12 - Dec 2, 2008 at 7:30 PM
  24. Rawr
    Joined:
    Jul 18, 2008
    Posts:
    2,442
    Referrals:
    10
    Sythe Gold:
    13
    Pokémon Trainer

    Rawr Addict.
    Retired Sectional Moderator $100 USD Donor

    Project Euler: Problem 1

    The guide is very good, but you don't get the point. People don't want a supremely intelligent guide so they can say "What the hell?" They want it somewhat simple enough for them to understand, easily. You just don't get it and think that you're smarter than everyone. Intelligence isn't the whole idea based in user education... It's about wanting to educate others, and having fun while doing so. This guide is good, but you just still don't understand the point. If you're looking for supreme intelligence, and enjoy making others looks stupid. I say you should just go to the [url="http://www.sythe.org/forumdisplay.php?f=244]SFA Forums[/url].
     
  25. Unread #13 - Dec 3, 2008 at 1:16 AM
  26. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    Project Euler: Problem 1

    To the person above me, "dumbing down" guides will only result in "dumbing down" your target audience. When you deliver information and knowledge on a topic to people, you want to do it in a way that they can understand but also so that it can benefit them. Guides that are too simple don't benefit anyone.

    Add a getchar() before the main function returns so that the program doesn't close before the user sees the output. (kind of obvious but you know noobs...)
     
  27. Unread #14 - Dec 3, 2008 at 11:51 AM
  28. verdammt ubel
    Referrals:
    0

    verdammt ubel Guest

    Project Euler: Problem 1



    I could. I could also open a file and write to that, or any number of ways. It didn't occur to me that some people might be using an operating system with such a broken shell as Windows and still be programming.
     
  29. Unread #15 - Dec 3, 2008 at 12:03 PM
  30. The Fat Controller
    Joined:
    Aug 16, 2007
    Posts:
    1,003
    Referrals:
    0
    Sythe Gold:
    1

    The Fat Controller Guru

    Project Euler: Problem 1

    This guide is useful only to a small subset of Sythe members. You need to have knowledge of programming to appreciate it, and people who already know enough C will be able to solve this one anyway. Making something more understandable to other users does not have to compromise it's integrity.
     
  31. Unread #16 - Dec 3, 2008 at 12:04 PM
  32. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    Project Euler: Problem 1

    It doesn't require the extra brackets, this will work fine:
    Code:
    if(i%3==0||i%5==0) {
    You should be using the built-in incrementor operator:
    Code:
      accumulator += i;
    Why are you using the pre-incrementor and starting at zero? Since, the lowest number that can affect the sum is 1 you should start from 1 and use the post-incrementor. Also, you might as well initialize your variable inside the for loop since it is permitted and means you don't have to do it on its own line.
    Code:
    for(int i=1;i<1000;i++) {
    Cheers.
     
  33. Unread #17 - Dec 3, 2008 at 12:47 PM
  34. verdammt ubel
    Referrals:
    0

    verdammt ubel Guest

    Project Euler: Problem 1

    The brackets will be removed.

    I should. I wrote this program a while back and didn't know about it.

    Pre-incrementation is sometimes faster and never slower, and since the first number to matter is 3, that section seemed worth the possible speed boost (However small).

    Initialising your variable within the for loop is not standard C99, I thought. I may be wrong. Thanks.
     
< [Swap]Selling level 3 skiller with 8mill,70Mining exc. | Selling awesome account with 80 mining and other stats! >

Users viewing this thread
1 guest


 
 
Adblock breaks this site