35
5
Introduction:
A 3x3x3 Rubik's Cube has \$43,252,003,274,489,856,000\$ possible permutations, which is approximately 43 quintillion. You may have heard about this number before, but how is it actually calculated?
A 3x3x3 Rubik's Cube has six sides, each with nine stickers. Looking at the (external) pieces instead of stickers however, we have six center pieces; eight corners pieces; and twelve edge pieces. Since the centers can't be moved, we can ignore those in the calculations. As for the corners and edges:
- There are \$8!\$ (\$40,320\$) ways to arrange the eight corners. Each corner has three possible orientations, although only seven (of the eight) can be oriented independently; the orientation of the eighth/final corner depends on the preceding seven, given \$3^7\$ (\$2,187\$) possibilities.
- There are \$\frac{12!}{2}\$ (\$239,500,800\$) ways to arrange the twelve edges. The halve from \$12!\$ is because edges must always be in an even permutation exactly when the corners are. Eleven edges can be flipped independently, with the flip of the twelfth/final edge depending on the preceding eleven, given \$2^{11}\$ (\$2,048\$) possibilities.
Putting this together, we have the following formula:
$$8!×3^7×\frac{12!}{2}×2^{11} = 43,252,003,274,489,856,000$$
Source: Wikipedia - Rubik's Cube Permutations
Although this may already look pretty complex, it's still rather straight-forward for a 3x3x3 Cube. For even cubes the formula is slightly different; this is the formula for a 4x4x4 Cube for example:
$$\frac{8!×3^7×24!^2}{24^7} = 7,401,196,841,564,901,869,874,093,974,498,574,336,000,000,000$$
Which is approximately 7.40 quattuordecillion on the short scale.
And for larger NxNxN Cubes (i.e. the current World Record 33x33x33) the formula will be extended quite a bit. To not make this introduction too long however, I put these links here instead, where the permutations of the 4x4x4 Cube and some other sized NxNxN Cubes are explained with a resulting formula:
You may be wondering by now: is there a general formula based on \$N\$ for any \$N\$x\$N\$x\$N\$ Cube? There certainly is. Here are three completely different algorithms, all giving the exact same results based on \$N\$:
1: Chris Hardwick's Formula:
$$\frac{(24×2^{10}×12!)^{N\pmod2}×(7!×3^6)×(24!)^{\lfloor{\frac{1}{4}×(N^2-2×N)}\rfloor}}{(4!)^{6×\lfloor{\frac{1}{4}×(N-2)^2}\rfloor}}$$
2: Christopher Mowla's trig Formula:
$$8!×3^7×\left(\frac{24!}{(4!)^6}\right)^{\frac{1}{4}×((N-1)×(N-3)+\cos^2(\frac{N×\pi}{2}))}×(24!)^{\frac{1}{2}×(N-2-\sin^2(\frac{N×\pi}{2}))}×(12!×2^{10})^{\sin^2(\frac{N×\pi}{2})}×\frac{1}{24^{\cos^2(\frac{N×\pi}{2})}}$$
3: Christopher Mowla's primes Formula:
$$2^{\frac{1}{2}×(2×N×(N+7)-17-11×(-1)^N)}×3^{N×(N+1)+2}×5^{\frac{1}{2}×(2×N×(N-2)+1+(-1)^N)}×7^{\frac{1}{8}×(6×N×(N-2)+3+5×(-1)^N)}×11^{\frac{1}{4}×(2×N×(N-2)-1+(-1)^N)}×96577^{\frac{1}{8}×(2×N×(N-2)-3+3×(-1)^N)}$$
where \$96577\$ is \$(13×17×19×23)\$.
Source: Cubers-reddit - Mathematical Counting Formulas of Number of Positions, God's Number, etc.
Challenge:
Choose and implement one of these three formulas (or your own derivative), which given an input-integer \$N\$ in the range \$[2,100]\$, outputs the correct result.
Challenge rules:
- You are free to use another formula besides these three, but keep in mind that these three are proven to be correct. If you use another formula, please add a link of where you got it from (or if you come up with it yourself add an in-depth explanation). And I will check for all integers in the range if the output is correct. Perhaps inspiration could be found in the oeis for this sequence: A075152.
- If your language automatically outputs a scientific output (i.e. \$1.401...×10^{45}\$ instead of the number after the 4x4x4 formula) this is allowed. But please add additional code to your answer to convert this scientific rounding to an exact output so the results can be verified, since rounding errors due to floating point precision during the execution of the formula in your code is not allowed - the actual result should be exact.
- Your program/function should be correct for at least the inputs in the range \$[2,100]\$ (although, since \$N=100\$ already results in a huge-ass number, any larger \$N\$ will probably work as well if you're able to output this one correctly).
- You are not allowed to loop over all possible permutations with a counter, since that would never output anything in a reasonable amount of time. Only the implementation of a formula (either one of the three provided, a derivative of one of those, or a completely new formula), or another method that will give the correct results in a reasonable amount of time (without hard-coding of course) is allowed. I thought about adding a restricted-time to enforce this, but I'm personally against restricted-time in combination with code-golf, so I won't. Still, please make sure your program gives the answers, and if it's too slow for TIO for some reason, add some screenshots with the output from your local machine as verification.
General rules:
- This is code-golf, so shortest answer in bytes wins.
Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language. - Standard rules apply for your answer with default I/O rules, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
- Default Loopholes are forbidden.
- If possible, please add a link with a test for your code (i.e. TIO).
- Also, adding an explanation for your answer is highly recommended.
Test cases:
Here the test cases for \$N\$ in the range \$[2,10]\$ (feel free to use the WolframAlpha links above for larger test cases):
n=2
3674160
n=3
43252003274489856000
n=4
7401196841564901869874093974498574336000000000
n=5
282870942277741856536180333107150328293127731985672134721536000000000000000
n=6
157152858401024063281013959519483771508510790313968742344694684829502629887168573442107637760000000000000000000000000
n=7
19500551183731307835329126754019748794904992692043434567152132912323232706135469180065278712755853360682328551719137311299993600000000000000000000000000000000000
n=8
35173780923109452777509592367006557398539936328978098352427605879843998663990903628634874024098344287402504043608416113016679717941937308041012307368528117622006727311360000000000000000000000000000000000000000000000000
n=9
14170392390542612915246393916889970752732946384514830589276833655387444667609821068034079045039617216635075219765012566330942990302517903971787699783519265329288048603083134861573075573092224082416866010882486829056000000000000000000000000000000000000000000000000000000000000000
n=10
82983598512782362708769381780036344745129162094677382883567691311764021348095163778336143207042993152056079271030423741110902768732457008486832096777758106509177169197894747758859723340177608764906985646389382047319811227549112086753524742719830990076805422479380054016000000000000000000000000000000000000000000000000000000000000000000000000000000000
NOTE: Since this is a code-golf challenge, it basically boils down to: implement one of these three formulas (or a derivative / your own method that still produces the correct results) as short as possible.
Why is the 4x4x4 cube considered the "odd" one instead of the 3x3x3? :P – Quintec – 2019-04-17T12:51:21.227
@Quintec Shhh.. you didn't see anything! ;p Fixed, thanks. :) – Kevin Cruijssen – 2019-04-17T12:56:25.053
2Doing this in x86-64 will be a fun challenge. I'll have to roll my own bigint (likely just a 256-bit or 512-bit int), and make it golfy. – moonheart08 – 2019-04-17T13:52:14.450
Off topic, but are there formula's for "super cubes", where the center pieces are marked (usually by colors on their edges) so that they can be uniquely identified? For example 3x3x3 centers orientation would matter (but there is parity). 4x4x4 centers can be moved and oriented (and there is also parity). – rcgldr – 2019-04-17T15:33:49.807
1
@moonheart08 Note that with N=100, the result takes about 128 Kb (16 KB): https://www.wolframalpha.com/input/?i=for+n+%3D+100,+N%5BLog%5B2,+((24*2%5E10*12%21)%5EMod%5Bn,+2%5D+7%213%5E6+24%21%5EFloor%5B(n%5E2+-+2+n)%2F4%5D)%2F4%21%5E(6+Floor%5B(n+-+2)%5E2%2F4%5D)%5D,3%5D
– Solomon Ucko – 2019-04-17T22:07:40.0534Note that Mowla's "trig" formula just uses $\sin^2$ and $\cos^2$ to obfuscate
floor
s. – attinat – 2019-04-18T03:35:07.573@rcgldr Yes, the reddit link also has a bunch of other formulas for the permutations of a Super NxNxN Cube; N-sized Minxes (dodecahedron-shaped variations of the Cubes, like Kilominx, Megaminx, Master Kilominx, Gigaminx, etc. for 2x2x2, 3x3x3, 4x4x4, 5x5x5, etc. respectively); and N-sized Super Minxes.
– Kevin Cruijssen – 2019-04-18T07:13:33.2334@attinat, I think it's a more useful perspective to say that both the trig and the floors are obfuscating $N \bmod 2$ – Peter Taylor – 2019-04-18T07:38:44.683
+1 for "huge-ass number". Puts me in mind of trolls from the Discworld.
– oliver-clare – 2019-04-18T12:42:18.533@KevinCruijssen No... I falsely analyzed the formula. My mistake, sorry. – peterh - Reinstate Monica – 2019-04-19T16:59:19.743
@attinat and Peter Taylor, I don't appreciate the insult. If you all would have read the rest of the formulas in the list, the purpose of using trig functions was to be able to take the first derivative of the formula. Sounds stupid, yeah? But with that derivative, I found a formula which has given very precise predictions on the world record times for the 4x4x4, 5x5x5, and 6x6x6 cubes for the past 7 years. I guess it was my fault that I didn't provide links to the posts in which I originally posted the formula. But Peter Taylor was spot on with it being N mod 2 instead of floor. – Christopher Mowla – 2019-04-23T15:03:22.123
2@ChristopherMowla Don't take their comments personal. I'm amazed that you were able to find these formulas and made such accurate predictions in the first place, and your formulas were one of the inspirations for this challenge. This is code-golf however, so readability, performance, warnings, best practices, historical significance, and sometimes just common sense are all thrown overboard if it can save a single byte on an answer. ;) attinat and PeterTaylor simply suggested such a golf based on your formulas, since N mod 2 is quite a bit shorter to use in programming languages than trig. – Kevin Cruijssen – 2019-04-23T15:10:58.183
@ChristopherMowla "I guess it was my fault that I didn't provide links to the posts in which I originally posted the formula." You could link them in this comment section, curious to read the reasoning behind it, and how you've used to to predict times. – Kevin Cruijssen – 2019-04-23T15:16:37.727
1I appreciate your comment, and I understand what you mean, but attinat could have used a different verb. He should know that I didn't list the formulas with coders to implement this. But also, thanks for the compliment. Without my trig formula, I would not have been able to find the prime formula (in its current most-condensed form). So even if I didn't mention the prediction formula, that would have been worth it on its own. – Christopher Mowla – 2019-04-23T15:25:06.887
As far as the world record prediction formula, see this post, https://www.speedsolving.com/forum/threads/world-record-formula-for-nxnxn-cubes.36621/#post-739116.
I provide links to the origin of my first 1st derivative formula and to the trig polynomial which appears in the actual world record formula (which accurately approximates the power of 10 of the first derivative formula up to n = 31).
I later used the same "power of 10" idea for the "God's number prediction formula. These prediction formulas just "happened to work out", but you're right. It's interesting that they do.
I didn't have enough space to also mention that the two posts above this post https://www.speedsolving.com/forum/threads/calculating-permutations-on-nxnxn-rubiks-cube.22683/page-2#post-649727 contain the original explanations of the trig formula and that trig polynomial (the one up to n = 31).
– Christopher Mowla – 2019-04-23T15:30:37.000I just realized that the LaTeX in those posts is messed up in the number of positions thread (due to the forum going through upgrades). However, below is a link to a saved PDF which has the original format so that you can read it more easily.
https://drive.google.com/file/d/1yQuQYOIe9AMRR07lDDOuPeIo-9Z0qGwW/view?usp=sharing
(The PDF is perfectly clear if you download it and view from your device.)
I forgot to mention that I made the "Wolfram|Alpha friendly version" of my God's number for the nxnxn (lowerbound,upperbound) formula by using a ratio of the same trig polynomial expression. (I just added this link to my Reddit post, BTW.) (wolframalpha.com/input/?i=Table%5BTable%5BFloor%5B((Pi-3)(62n%5E2-56n-4Cos%5B(nPi)%2F2%5D%2B21Cos%5BnPi%5D-33))%2F(ProductLog%5B1%2Fx((Pi-3)(62n%5E2-56n-4Cos%5B(nPi)%2F2%5D%2B21Cos%5BnPi%5D-33))%5D)-1%5D,%7Bx,2,3%7D%5D,%7Bn,2,20%7D%5D)
(See the following topic to compare the lowerbounds.) http://cubezzz.dyndns.org/drupal/?q=node/view/236