Project Euler Lab - Problem 666

#666 - Polymorphic Bacteria

● AdvancedOfficial difficulty: 41%ApproximationTier C - reduced scale in browser; full scale in notebookNot viewed
↖ Euler Lab
Members of a species of bacteria occur in two different types: \(\alpha\) and \(\beta\). Individual bacteria are capable of multiplying and mutating between the types according to the following rules:
  • Every minute, each individual will simultaneously undergo some kind of transformation.
  • Each individual \(A\) of type \(\alpha\) will, independently, do one of the following (at random with equal probability):
    • clone itself, resulting in a new bacterium of type \(\alpha\) (alongside \(A\) who remains)
    • split into 3 new bacteria of type \(\beta\) (replacing \(A\))
  • Each individual \(B\) of type \(\beta\) will, independently, do one of the following (at random with equal probability):
    • spawn a new bacterium of type \(\alpha\) (alongside \(B\) who remains)
    • die

If a population starts with a single bacterium of type \(\alpha\), then it can be shown that there is a 0.07243802 probability that the population will eventually die out, and a 0.92756198 probability that the population will last forever. These probabilities are given rounded to 8 decimal places.

Now consider another species of bacteria, \(S_{k,m}\) (where \(k\) and \(m\) are positive integers), which occurs in \(k\) different types \(\alpha_i\) for \(0\le i< k\). The rules governing this species' lifecycle involve the sequence \(r_n\) defined by:

  • \(r_0 = 306\)
  • \(r_{n+1} = r_n^2 \bmod 10\,007\)

Every minute, for each \(i\), each bacterium \(A\) of type \(\alpha_i\) will independently choose an integer \(j\) uniformly at random in the range \(0\le j<m\). What it then does depends on \(q = r_{im+j} \bmod 5\):

  • If \(q=0\), \(A\) dies.
  • If \(q=1\), \(A\) clones itself, resulting in a new bacterium of type \(\alpha_i\) (alongside \(A\) who remains).
  • If \(q=2\), \(A\) mutates, changing into type \(\alpha_{(2i) \bmod k}\).
  • If \(q=3\), \(A\) splits into 3 new bacteria of type \(\alpha_{(i^2+1) \bmod k}\) (replacing \(A\)).
  • If \(q=4\), \(A\) spawns a new bacterium of type \(\alpha_{(i+1) \bmod k}\) (alongside \(A\) who remains).

In fact, our original species was none other than \(S_{2,2}\), with \(\alpha=\alpha_0\) and \(\beta=\alpha_1\).

Let \(P_{k,m}\) be the probability that a population of species \(S_{k,m}\), starting with a single bacterium of type \(\alpha_0\), will eventually die out. So \(P_{2,2} = 0.07243802\). You are also given that \(P_{4,3} = 0.18554021\) and \(P_{10,5} = 0.53466253\), all rounded to 8 decimal places.

Find \(P_{500,10}\), and give your answer rounded to 8 decimal places.

This problem is taken from Project Euler, Problem 666.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=666. Published Saturday, 20th April 2019, 01:00 pm. Solved by 359 members at time of mirroring.

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 C - reduced scale in browser; full scale in notebook
Browser runs a reduced, clearly-labelled educational scale; the original scale is provided 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.