Ruby is too slow for programming competitions
blog.clifreeder.com
Ruby is too slow for programming competitions
1–10 of 254 posts
Re: Ruby is too slow for programming competitions
#2Re: Ruby is too slow for programming competitions
#3Re: Ruby is too slow for programming competitions
#4Re: Ruby is too slow for programming competitions
#5Re: Ruby is too slow for programming competitions
#6Did you need to check `&& (start..finish).cover?(square)` on Line 17?
Re: Ruby is too slow for programming competitions
#7I'm sure if the competition was based on speed of implementation there would be different conclusions.
Re: Ruby is too slow for programming competitions
#8I don't want to detract from the article's main point with a cliche "algorithmic optimization beats fine tuning", but I think it's worth mentioning. Unless I'm mistaken, this particular problem can be solved about 10,000,000x more efficiently.
> Given two numbers X and Y, return how many numbers in that range
> (inclusive) are palindromes, and also the square of a palindrome.
I would just count the digits of X and Y, then ranging between those counts, apply the following algorithm: For N digits, enumerate all numbers of N/2 digits satisfying the bounds X and Y. If N is even then simply mirror the first N/2 digits. If N is odd, mirror the first N/2-1. Finally, check the bounds of the resulting number against X and Y to filter out edge cases.If the original algorithm runs in 10^N time, this would be 10^(N/2). For N=14 as mentioned, that's roughly a speedup of 10,000,000.
Edit: It seems I misinterpreted the question (which sounds like you're supposed to enumerate all palindrome numbers X>=N>=Y, and also print out the squares of those numbers). This is actually asking for a particular type of palindromes -- those which are palindromic squares of palindromes. As macobo helpfully points out, you can drastically optimize mathematically from this additional filter.
Re: Ruby is too slow for programming competitions
#9There is a huge difference between Ruby and Go, in terms of performance. I wonder if anyone has any experience with Python, considering that both are interpreted programming languages.
Porting his code to PyPy would be very interesting, though.