A part of being a programmer involves designing and solving mathematical and/or logical problems.
Project Euler can be a fun way of refining those skills. The website describes itself as...
"...a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems."
The problems themselves are tricky, but a high school student with the mathematical and programming skills should be able to solve many of the earlier problems. Lets take problem 1 for example..
"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."
In many cases we can use simply a brute force attack to solve problems like above. However in later problems code needs to be designed to be efficient or else it might take days to compute. The answer in c++ of the above problem would be...
#include <iostream>
using namespace std;
int main() {
int result = 0;
for (int i = 3; i < 1000; i++)
if (i % 3 == 0 || i % 5 == 0)
result += i;
cout << "The result is: " << result;
cin.get();
return 0;
}
No comments:
Post a Comment