I’m stuck on this one.
I can’t find a simple formula and my python attempt with loops and common divisors is not efficient enough for testcases 5-7.
I’m also thinking about integer factorization and primes.
Can somebody give my a hint?
First it was “piece of cake”, and then shortly after the “aha, not as easy as it seems …”
Then some thinking which I liked a lot, and the hidden structure was revealed. This allowed me to write a naive solution (I guess similar to @RoDr1g0 ) which solved nicely cases 1…4.
Then started the deep dive, and in this particular puzzle, the deep dive involves college-level arithmetic (as hinted above), number theory, some code optimizations etc.
I found it somewhat interesting to go to this level of in-depth math, and did some google searches, which allowed me to find an optimized solution for cases 5,6.
Mind you, this requires very good understanding of math. Not sure I saw such things in my BA (and I did learn math etc…)
The “ultimate” required too much deep-dive, so I left it alone.
I’m new to this site, and I like it much.
I would prefer in such challenges that the “required knowledge” is listed up-front. Had I known that this requires such a level of math I may have chosen to steer elsewhere (“coders strike back”, gold league right now. Yami … )
Edit - I jumped the gun too soon. Check @irmo322 comment below. Indeed there is a (very) elegant solution to this puzzle.
I managed to work out the mathematics behind the problem description all by myself. Now, so I thought, I just need to implement the mathematics in code.
My naive implementation was not fast enough for the bigger numbers. That was expected. But my code optimizations were still not fast enough.
I searched around and found some stackexchange codegolf challenge which described this exact problem. Sadly even the presumably highly optimized code from the stackexchange codegolf was not fast enough for the bigger numbers. So I gave up on this puzzle.
I had fun working out the mathematics. I had fun implementing the mathematics. I had fun trying to optimize my naive implementations. I gave up after realizing that I probably need advanced mathematics to solve the big numbers.
In other words: the big numbers are a tad too big. Make them big enough that naive implementations fail. But not so big that it requires advanced mathematics. Or mark the puzzles as requiring advanced mathematics.
[yep, a moderator removed the link]
It probably spoils too much of the puzzle so mods will probably remove the link.
For the last test, I have two hints for you:
1- You don’t necessary need to dive into hard mathematics. There is a solution which can be explained to a (smart) 12 years old teenager.
1 - Use a fast language as C. For example, my solution wasn’t fast enough if written in lua (a script langage) but passes the last test when written in C.
My hat’s off to you , @irmo322. Beautiful solution.
Not sure about the 12 years old, but indeed a simple and elegant approach.
Thank you for providing the inspiration.
Your point about the difference between scripting and C is something I’ve also thought about. I wasn’t sure that the site can factor the difference between the execution speed of different languages… Following your experience it seems that there are indeed gaps, and compiled languages are probably faster. Interesting.
Oh my god, this image is so helpful! I tried creating this in ASCII, but it gave me a headache to try to read it. You can actually analyze the factors in this! Notice how in row 6, each square is red if the corresponding square in row 3 or in row 2 is red? And it just continues! Thank you!
I have it solved in native Python, all testcases.
With some head scratching, google skills and Wikipedia-grade math.
So, doable in scripting languages, just have to use more elegant approach than bit-bashing.
Then again, there are other, very concise solutions in Python, that don’t involve hard math whatsoever. or so it seems. maybe it’s hidden behind the lines.
an update to my wailing from years ago. i kept returning to this puzzle once or twice a year and tried a bit of this and that. i found some mathematical shortcuts. which might seem “simple” once understood. but some of it really is not easy to understand. at least for me. i tried some programmatic optimization. some worked some not. most just marginal.
eventually i got a python solution which was able to pass the last test some of the time. i could have tried submitting that a couple of times until it passed but i did not want to do that. so i coded the same algorithm in c and it passed consistently.
this was some years ago. i forgot whether i still needed to optimize some on the c code. perhaps yes.
well. just wanted to share. maybe encourage some frustrated codingamer trying this puzzle.