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;
As such, our program currently looks like this;
Problem 2: Testing for Multiples
This is where we will test the integer. It's simple C to test a variable for divisibility;
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;
Please feel free to criticise me or ask for more details -- I'd be glad to flesh out the sections you specify.
"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.












