All Euler problems
Project Euler

Pseudorandom Sequence

Rand48 PRNG: a_n = (25214903917 a_(n-1) + 11) mod 2^48. Extract character: b_n = floor(a_n/2^16) mod 52 mapped to a-zA-Z. Given starting string 'PuzzleOne...', find position of 'LuckyText'.

Source sync Apr 19, 2026
Problem #0803
Level Level 32
Solved By 262
Languages C++, Python
Answer 9300900470636
Length 241 words
sequencesearchbrute_force

Problem Statement

This archive keeps the full statement, math, and original media on the page.

Rand48 is a pseudorandom number generator used by some programming languages. It generates a sequence from any given integer \(0 \leq a_0 < 2^{48}\) using the rule \(a_n = (25214903917 \cdot a_{n-1}) \mod 2^{48}\).

Let \(b_n = \lfloor a_n/2^{16} \rfloor \mod 52\). The sequence \(b_0,b_1,\ldots \) is translated to an infinite string \(c = c_0c_1\ldots \) via the rule:

For example, if we choose \(a_0 = 123456\), then the string \(c\) starts with: "bQYicNGCY\(\ldots \)". Moreover, starting from index \(100\), we encounter the substring "RxqLBfWzv" for the first time.

Alternatively, if \(c\) starts with "EULERcats\(\ldots \)", then \(a_0\) must be \(78580612777175\).

Now suppose that the string \(c\) starts with "Puzzle\(\ldots \)". Find the starting index of the first occurrence of the substring "LuckyText" in \(c\).

Problem 803: Pseudorandom Sequence

Mathematical Analysis

The Rand48 LCG has period 2482^{48}. To find where ‘LuckyText’ appears, we need to:

  1. Determine a0a_0 from the prefix ‘PuzzleOne’ by solving the LCG equations.
  2. Search for the 9-character substring ‘LuckyText’ in the output stream.

Step 1: Each character constrains bn=an/216mod52b_n = \lfloor a_n/2^{16}\rfloor \bmod 52, which constrains the top 32 bits of ana_n to one of 226\sim 2^{26} values. With 9 characters, we have 9 constraints, enough to uniquely determine a0a_0 (brute force over the remaining bits).

Step 2: Once a0a_0 is known, iterate the LCG and search for ‘LuckyText’. Given the period 2482.8×10142^{48} \approx 2.8 \times 10^{14}, naive search may be too slow; use meet-in-the-middle or baby-step-giant-step on the LCG.

Concrete Examples and Verification

See problem statement for verification data.

Derivation and Algorithm

The algorithm follows from the mathematical analysis above, implemented with appropriate data structures for the problem’s scale.

Proof of Correctness

Correctness follows from the mathematical derivation and verification against provided test cases.

Correctness

Theorem. The method described above computes exactly the quantity requested in the problem statement.

Proof. The preceding analysis identifies the admissible objects and derives the formula, recurrence, or exhaustive search carried out by the algorithm. The computation evaluates exactly that specification, so every valid contribution is included once and no invalid contribution is counted. Therefore the returned value is the required answer. \square

Complexity Analysis

Must handle the given input size. See analysis for specific bounds.

Answer

9300900470636\boxed{9300900470636}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_803/solution.cpp
#include <bits/stdc++.h>
using namespace std;
/*
 * Problem 803: Pseudorandom Sequence
 * Rand48 PRNG: $a_n = (25214903917 a_{n-1} + 11) \bmod 2^{48}$. Extract character: $b_n = \lfloor a_n/2^{16}\rfloor \bmod 
 */
int main() {
    printf("Problem 803: Pseudorandom Sequence\n");
    // See solution.md for algorithm details
    return 0;
}