Project Euler Lab - Problem 483

#483 - Repeated Permutation

● ResearchOfficial difficulty: 91%ApproximationTier D - conceptual / notebook executionNot viewed
↖ Euler Lab

We define a permutation as an operation that rearranges the order of the elements \(\{1, 2, 3, ..., n\}\). There are \(n!\) such permutations, one of which leaves the elements in their initial order. For \(n = 3\) we have \(3! = 6\) permutations:

  • \(P_1 =\) keep the initial order
  • \(P_2 =\) exchange the 1st and 2nd elements
  • \(P_3 =\) exchange the 1st and 3rd elements
  • \(P_4 =\) exchange the 2nd and 3rd elements
  • \(P_5 =\) rotate the elements to the right
  • \(P_6 =\) rotate the elements to the left

If we select one of these permutations, and we re-apply the same permutation repeatedly, we eventually restore the initial order.
For a permutation \(P_i\), let \(f(P_i)\) be the number of steps required to restore the initial order by applying the permutation \(P_i\) repeatedly.
For \(n = 3\), we obtain:

  • \(f(P_1) = 1\) : \((1,2,3) \to (1,2,3)\)
  • \(f(P_2) = 2\) : \((1,2,3) \to (2,1,3) \to (1,2,3)\)
  • \(f(P_3) = 2\) : \((1,2,3) \to (3,2,1) \to (1,2,3)\)
  • \(f(P_4) = 2\) : \((1,2,3) \to (1,3,2) \to (1,2,3)\)
  • \(f(P_5) = 3\) : \((1,2,3) \to (3,1,2) \to (2,3,1) \to (1,2,3)\)
  • \(f(P_6) = 3\) : \((1,2,3) \to (2,3,1) \to (3,1,2) \to (1,2,3)\)

Let \(g(n)\) be the average value of \(f^2(P_i)\) over all permutations \(P_i\) of length \(n\).
\(g(3) = (1^2 + 2^2 + 2^2 + 2^2 + 3^2 + 3^2)/3! = 31/6 \approx 5.166666667\mathrm e0\)
\(g(5) = 2081/120 \approx 1.734166667\mathrm e1\)
\(g(20) = 12422728886023769167301/2432902008176640000 \approx 5.106136147\mathrm e3\)

Find \(g(350)\) and write the answer in scientific notation rounded to \(10\) significant digits, using a lowercase e to separate mantissa and exponent, as in the examples above.

This problem is taken from Project Euler, Problem 483.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=483. Published Sunday, 5th October 2014, 10:00 am. Solved by 343 members at time of mirroring.

Why this is useful

Numerical Computing. Precision, conditioning, and numerical iteration transfer directly to pricing engines and model calibration (Phases 5, 13, 15).

We classify relevance honestly - not every Euler problem is a trading application.

Learning mode

Pick how much scaffolding you want. Your choice is remembered per problem.

Scratchpad

Mathematical notes, formulas, pseudocode, hypotheses, complexity notes. Saved automatically with your progress.

Python workbench

Tier D - conceptual / notebook execution
Too heavy for browser Pyodide at original scale: the browser is used for planning, small cases and reasoning; full scale runs in a local notebook.

Real Python (Pyodide) in a sandboxed Web Worker - no network, no filesystem, no DOM access. Ctrl/Cmd+Enter runs. Escape leaves the editor. Stop terminates the worker.

Python runtime not loaded (it boots on first run - a one-time local load).

Check your answer

Answers are checked against a salted hash held in a separate file - not printed in this page. This prevents accidental spoilers; it is not cryptographic protection (see the build notes).

Progressive hints

Confidence

Low confidence schedules this problem for spaced review, even if you solved it.

Reference solution

Spoiler
The complete original explanation (interpretation, naive approach, insight, proof, complexity, Python implementation, tests, common mistakes, alternatives) is hidden and lazy-loaded. Reveal it only after a meaningful attempt - the struggle is where the learning happens.