Euler#5

[b]Spoiler[/b]

Don't read if you're actually working on these.

Anyway, wow this was easy. My first attempt solved it in .43 seconds. It's a little crude, but hell I don't even think optimising this is going to make much of a difference. If you see any spots that look to be slowing this down for some reason, let me know.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const unsigned int max = 429496720;
    unsigned int result(0);
    int ctr = 0;
    for(unsigned int i = 20; i < max; i += 20)
    {
        for(unsigned int j = 20; j > 0; j--)
        {
            if(i % j == 0)
            {
                ctr += 1;
            }
            else
            {
                ctr = 0;
                break;
            }
        }
        if(ctr == 20)
        {
            result = i;
        }
    }
    std::cout << "Answer is: " << result;
For #5 I simply had this...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main() {
    for (int i = 1; i > 0; i ++) {
        bool found = true;
        for (int j = 11; j <= 20; j ++) {
            if (i % j != 0)
                found = false;
        }
        if (found) {
            std::cout << i;
            i = -1;
        }
    }
    return 0;
}


Edit: I knew it was too good to be true, mine takes an extremely long time to run. Oops. I need to start saving the run times in my comment header so I know what the estimated time on it is. =/
Last edited on
Haha, that's how mine tend to go. I usually just throw something together, and then rework it. At least for these PE problems. They seem too short to actually plan out.

P.S. I found #6 to be super easy as well. I had never heard of a pyramidal number, but plugging in the formula for that made it quite easy. Finds the answer in 0.015 seconds with no optimising yet
Last edited on
Topic archived. No new replies allowed.