FrankKair / polyglot-euler

๐Ÿ“œ Project Euler solutions in various programming languages

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Optimize Elixir solution 145

fredericojordan opened this issue ยท comments

Hey guys, I've just opened #33 which consists of the elixir solution for problem 145.
Unfortunately it is very badly optimized for now, taking 30 min to compute.
If anybody has any suggestion for improvement, I'd be happy to test it out! ๐Ÿ˜„
Thanks!

For reference, these are the first few numbers that are reversible:

[12, 14, 16, 18, 21, 23, 25, 27, 32, 34, 36, 41, 43, 45, 52, 54, 61, 63, 72, 81, 209, 219, 229, 239, 249, 308, 318, 328, 338, 348, 407, 409, 417, 419, 427, 429, 437, 439, 447, 449, 506, 508, 516, 518, 526, 528, 536, 538, 546, 548, ...]

They look like they follow a regular pattern, but I haven't looked too much into it yet.

For starters, the first and last digits must be of different parity, in order for the resulting addition to be an odd number...

We can also identify that in 3-digit numbers, the sum of (first digit + last digit) must result in over than 10, in order for the carry-on digit to make the middle digit odd.

For example:
213 can't be reversible because 213 + 312 = 525
219 is reversible because 219 + 912 = 1131
The 1 carries on from the 9 + 2 sum on the last digit to make the second-to-last digit odd.

For that reason, there are no reversible numbers between 100 and 200.
If we can extrapolate this rule, it will also help to reduce verification cases.

I don't know anything about Elixir but consider the problem as below 100 and consider the following:

  • 12 is reverse of 21
  • 14 is reverse of 41
  • 16 is reverse of 61
  • ...

If you reach 49, the reverse is 94 and you don't need to run from 1 to 100, just from 1 to 50, because when you pass to 50+ numbers, you already counted them. You can try cutting by half just doing this if my assumption is correct.

For tests issues, you can use smaller numbers and see if works. I don't know if that approach is possible considering that Elixir is a functional language.

What do you think?

I think it's possible.
It looks like we just need to separate the cases in where the number of digits is even or odd.
I'll get onto it some time... I hope