Project Euler / programming problems

Let’s see where this goes. Either spoiler your solutions or link to them from off-site.

We could play at one problem a day, or two problems if that better fits the pace we want.

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.

Feel free to also see this list Exercises In Chronological Order | Programming Praxis

Discourse has the code tag.

5 Likes

I think if you wrap stuff in ``` it turns it into pre-formatted text:

No whitespace problems
    This is indented
        More indents yay
4 Likes

You can even do syntax highlighting by putting the language after the first ```

for i in range(10):
    print(i)
4 Likes
Javascript oneliner
Array.from({length: 1000}, (v, i) => i).filter(i => (i%3==0 || i%5==0) && i).reduce((i,j) => i+j) 
2 Likes

So do we think below means that it excludes the number 1000 itself?

I don’t think I’ll sign up to Project Euler just to check :slight_smile:

Oh shit, it was 1000, not 100. Reading comprehension failed. I’ll have to redo the Python, lol.

I think below 1000 does not include 1000.

The best way to phrase including 1000 would be with up to and including 1000, though up to 1000 would also be fairly clear.

2 Likes
Python
def problem_1():
    sumN = 0;

    for i in range(3, 1000, 3):
        sumN += i;

    for i in range (5, 1000, 5):
        if i % 3 != 0:
            sumN += i;

    return sumN;

I know it’s pretty rustic, but I haven’t done much coding for years, so I’m really at the “shit shower shave” phase of things.

3 Likes
C++
const int N = 1000;
int sum = 0;
for (int i = 0; i < N; ++i) {
    if (i % 3 == 0 || i % 5 == 0) {
        sum += i;
    }
}
cout << "Sum: " << sum << endl;

GDB online Debugger | Code, Compile, Run, Debug online C, C++

Output: 499500 233168

Edit: need to modulo i, not N

nothing fancy :slight_smile:

1 Like

The given example for numbers below 10 makes it clear that “below N” does not include N :slight_smile:

2 Likes

Not sure why, but the result is wrong.

1 Like

because im a dummy haha i was comparing to N

3 Likes

I guess reading does help.

Ah well :stuck_out_tongue: I find the thread interesting anyway :slight_smile:

BTW: there is a way to solve this without any iteration. (Not that I have done it as clever as that though.)

2 Likes

I wonder whether we should have separate threads for solutions and discussion.

I worry the discussion will overwhelm the solutions.

oh right triangle numbers are a thing

Just for fun, Python, O(1)
N = 1000

def triangle(n):
    return n * (n+1) // 2

def sum_divisible_under_n(divisor):
    return triangle((N-1)//divisor) * divisor

solution = (
    sum_divisible_under_n(3)
    + sum_divisible_under_n(5)
    - sum_divisible_under_n(15))

print("solution:", solution)
4 Likes

I’m having a lot of trouble installing the Ruby interpreter correctly. The Windows installer with devkit won’t start and the one without it won’t even install.

Any tips? I’ve tried a few times now, and been knocked back by an error about a misplaced shortcut.

I’ve also downloaded the zipped version, but I can’t find the program file.

Lazy R one-liner
sum(seq(3,999,3),seq(5,999,5),-seq(15,999,15))
3 Likes

Failed once again to install the Ruby interpreter. I get this:

image

When I found a file called “irb” for it, it asked me how I wanted to open it.

I’ve tried about five times now. Might just have to use Ideone.

I installed rubyinstaller-devkit-3.1.1-1-x64.exe successfully just a day or two ago. Never had any trouble running the installers themselves. I have had trouble with gems/bundler after installing, but that eventually gets fixed if I carefully remove everything, reinstall, and reboot.

That said, if you are in this for the educational experience and not so excited about the Windows sysadmin side, I’d recommend running a Linux VM or doing it within a cheap cloud server. It’s a lot more fun when you have an isolated system that “just works”.

1 Like

What did you do when it gets to the command line prompt stage, and asks you for your opinions on what to install, and offers “enter, 1, 3”?

Did you do “enter for unsure”?