• Log in or sign up for an account here: https://www.sythe.org/login

Project Euler: Problem 1

  • Thread starter Thread starter verdammt ubel
  • Start date Start date
V

verdammt ubel

Guest
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.
 
This is very good..But Where would I ever use this just wondering??
8/10
 
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
 
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

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?
 
nice guide.... nice detail....9/10
 
You have a bad attitude, but the guide is well written. Try dumbing it down further so the majority of people will understand it.
 
Steam Account Verifier
Sythe's 10th Anniversary
Community Participant
Surprisingly well written guide.

10/10
 
You have a bad attitude, but the guide is well written. Try dumbing it down further so the majority of people will understand it.

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.
 
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
 
Pokémon Trainer
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].
 
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.

Please feel free to criticise me or ask for more details -- I'd be glad to flesh out the sections you specify.
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...)
 
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 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...)


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

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.
 
if((i%3)==0||(i%5)==0) {
It doesn't require the extra brackets, this will work fine:
Code:
if(i%3==0||i%5==0) {
accumulator = accumulator + i;
You should be using the built-in incrementor operator:
Code:
  accumulator += i;
for(i=0;i<1000;++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.
 
1.
if((i%3)==0||(i%5)==0) {
It doesn't require the extra brackets, this will work fine:
Code:
if(i%3==0||i%5==0) {
accumulator = accumulator + i;
You should be using the built-in incrementor operator:
Code:
  accumulator += i;
for(i=0;i<1000;++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.
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.
 
Runescape Gamblingrunescape gambling
Back
Top