stbrumme / euler Goto Github PK
View Code? Open in Web Editor NEWC++ solutions for more than 300 Project Euler problems
Home Page: https://euler.stephan-brumme.com
C++ solutions for more than 300 Project Euler problems
Home Page: https://euler.stephan-brumme.com
I tried this solution for Hackerrank Project Euler Problem 218. But it fails for hidden test cases.
Hello, I like to read your solutions (in 99% of cases after I finish some Euler problem myself , although I used it once to get me a bit unstuck on general idea), to compare and learn new ways.
With #313 I see I can share the other way, hope you don't mind opening github issue for this, it's easiest way for me to contact you and open the discussion.
My approach was opening spreadsheet and drawing few sliding game boards first to find:
These should be reasonably easy to verify by "eyeballing" small area of board in spreadsheet and trying to do the moves, and the rules 4. and 5. can be applied after each other due to preserving free square position, and 2. and 3. can be applied after either 4. or 5. once the bottom right corner of board is in reach.
Now I did a bit of leap of faith and claim that these rules can be applied to get optimal amount of moves (I think it's reasonably visible why this works intuitively, but I don't think I would be capable to produce math-quality proof of it).
At this point I got next idea, to reverse the formulas, ie. calculate all [m,n] (or amount of them) for particular moves value, and after a while of playing with those formulas I got inverse formulas:
for NxN square board the only N = (moves + 11) / 8, if divisible (moves % 8 == 5), otherwise no N exists
(after reading through problem forum I was reminded the prime^2 will never hit this case, because that would require (p % 8)(p % 8) == 5 and there's no such (p % 8) to produce 5 after square, so actually there's never square board in the set of boards for p^2 moves.
for MxN board I calculate first value X = (moves + 13) / 2 -> if not divisible by 2, there's no [M,N]
min_m = ceil(X / 4) => int min_m = (x + 3) / 4;
- to ensure n < m
max_m = floor((X - 2) / 3) => int max_m = (x - 2) / 3;
to ensure 2 <= n
Then for each min_m ... max_m the n can be calculated as n = x - 3m, but that's not the point of the task, it is enough to count solutions: return 2 * (max_m - min_m + 1);
... 2x for both MxN and NxM boards.
Then the final code uses my primes class to generate all primes <= 1e6, and goes through them summing amounts of boards for each p^2, which takes about 0.03s on my machine to produce the final answer.
(I hope this finds you well and you could enjoy the read - at least as much as I enjoyed your blog so far. If you want to adjust your article to describe this approach, you are free to use/adjust this as you see fit, have a good time :) )
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.