←back to thread

165 points fzliu | 7 comments | | HN request time: 0.972s | source | bottom
Show context
Sohcahtoa82 ◴[] No.41843245[source]
During the solving of a problem on Project Euler, I learned that compilers are smarter than me.

I don't remember the problem number or its title, but it involved starting from the top-left corner of a 2D grid and finding how many possible paths there are to get to the bottom-right corner while only moving either down or right.

My naive solution was a brute-force depth-first recursive search. On my CPU at the time, it took about 3 minutes to solve. I thought, the logic of this is incredibly simple, why not do it in assembly?

My assembly solution took 5 minutes.

I decompiled the compiled C code to see what it had done, but I couldn't understand it. My assembly knowledge was too basic.

Thinking on it now, I wonder if a modern compiler would solve the entire problem at compile-time and just hard-code the answer to be output at runtime.

replies(5): >>41843296 #>>41843301 #>>41843529 #>>41843878 #>>41844013 #
guessmyname ◴[] No.41843301[source]
Lattice Paths — https://projecteuler.net/problem=15

> Starting in the top left corner of a 2x2 grid, and only being able to move to the right and down, there are exactly 6 routes to the bottom right corner.

   ─────────┐  ────┐       ────┐    
   ┌───┬───┐│  ┌───│───┐   ┌───│───┐
   │   │   ││  │   │   │   │   │   │
   ├───┼───┤│  ├───└────┐  ├───│───┤
   │   │   ││  │   │   ││  │   │   │
   └───┴───┘│  └───┴───┘│  └───│───┘
            ▼           ▼      └────▶
                                     
  │┌───┬───┐  │┌───┬───┐  │┌───┬───┐
  ││   │   │  ││   │   │  ││   │   │
  └─────────┐ └────┐───┤  │├───┼───┤
   │   │   ││  │   │   │  ││   │   │
   └───┴───┘│  └───│───┘  │└───┴───┘
            ▼      └────▶ └─────────▶
> How many such routes are there through a 20x20 grid?
replies(4): >>41843464 #>>41843557 #>>41843591 #>>41844288 #
1. dotancohen ◴[] No.41843557[source]
> How many such routes are there through a 20x20 grid?

Is it 21! ?

replies(2): >>41843587 #>>41843596 #
2. Jtsummers ◴[] No.41843587[source]
Nope, and only off by a few orders of magnitude.
replies(1): >>41846184 #
3. mdp2021 ◴[] No.41843596[source]
No. It's 20!/(10!x10!)

You have 20 slots to be filled with 10 items vs other 10 items.

replies(1): >>41843664 #
4. noapologies ◴[] No.41843664[source]
Close, it's 40!/(20!*20!)

20 Rs, 20 Ds in a 20x20 grid.

Example pattern: RRDDDR...D (40 letters)

Basically the number of permutations, with repetition, of 20 Rs and 20 Ds.

replies(2): >>41843698 #>>41846175 #
5. mdp2021 ◴[] No.41843698{3}[source]
Oops! Thanks, just distraction... It's that first I wanted to see the general solution to those kind of problems (and I gave the solution for a wrong one in the class), then I wanted to verify the C implementation of the solution with POPCNT like the OP seems to have done (I am writing the code)...

Edit: ...and yes, it seems that brute-forcing (counting to one trillion) takes more time than I expected.

6. dotancohen ◴[] No.41846175{3}[source]
Now I see it. Twenty items (R operations) in 40 slots: 40!/(20!) and the order of the items does not matter, so again divide by 20!. The remaining slots are D operations.

The answer actually came to me in the shower this morning.

7. dotancohen ◴[] No.41846184[source]
If it is any comfort, my answer produced a non-negative integer - just like the true answer ))