Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize Elixir solution 145 #34

Open
fredericojordan opened this issue May 9, 2018 · 4 comments
Open

Optimize Elixir solution 145 #34

fredericojordan opened this issue May 9, 2018 · 4 comments

Comments

@fredericojordan
Copy link
Collaborator

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!

@fredericojordan
Copy link
Collaborator Author

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...

@fredericojordan
Copy link
Collaborator Author

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.

@FrankKair FrankKair changed the title Optimize solution 145 Optimize Elixir solution 145 May 10, 2018
@caiangums
Copy link
Collaborator

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?

@fredericojordan
Copy link
Collaborator Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants