Open Source · 7 Languages · 900+ Problems

Project Euler Solutions

Complete solutions in C++, Python & Java — with step-by-step mathematical explanations

All Problems

Problem 978: Random Walk Skewness

View on Project Euler

Project Euler Problem 978 Solution

EulerSolve provides an optimized solution for Project Euler Problem 978, Random Walk Skewness, with C++, Python, Java, and a step-by-step mathematical explanation.

Problem Summary The quantity of interest is the skewness of a random walk defined by the signed Fibonacci-type recurrence $X_0=0,\qquad X_1=1,\qquad X_n=X_{n-1}+\varepsilon_n X_{n-2}\quad(n\ge 2),$ where each \(\varepsilon_n\) is independently chosen from \(\{-1,+1\}\) with probability \(1/2\). The task is to evaluate the skewness of \(X_{50}\). A brute-force distribution build would branch over \(2^{49}\) sign histories, which is unnecessary. The implementations avoid enumeration entirely by tracking only the moments that the skewness formula needs. Mathematical Approach The key simplification is that skewness depends on the mean, variance, and third central moment. For this walk, those quantities can be extracted from two short linear recurrences. The Signed Fibonacci Walk At every step \(n\ge 2\), the process adds or subtracts the previous-but-one value. This is why the walk is Fibonacci-shaped but random: the deterministic recurrence \(x_n=x_{n-1}+x_{n-2}\) is replaced by a random sign in front of \(x_{n-2}\). Define the raw moments $a_n=\mathbb{E}[X_n^2],\qquad c_n=\mathbb{E}[X_n^3].$ Once these are known, the skewness follows from standard moment identities....

Detailed mathematical approach

Problem Summary

The quantity of interest is the skewness of a random walk defined by the signed Fibonacci-type recurrence

$X_0=0,\qquad X_1=1,\qquad X_n=X_{n-1}+\varepsilon_n X_{n-2}\quad(n\ge 2),$

where each \(\varepsilon_n\) is independently chosen from \(\{-1,+1\}\) with probability \(1/2\). The task is to evaluate the skewness of \(X_{50}\).

A brute-force distribution build would branch over \(2^{49}\) sign histories, which is unnecessary. The implementations avoid enumeration entirely by tracking only the moments that the skewness formula needs.

Mathematical Approach

The key simplification is that skewness depends on the mean, variance, and third central moment. For this walk, those quantities can be extracted from two short linear recurrences.

The Signed Fibonacci Walk

At every step \(n\ge 2\), the process adds or subtracts the previous-but-one value. This is why the walk is Fibonacci-shaped but random: the deterministic recurrence \(x_n=x_{n-1}+x_{n-2}\) is replaced by a random sign in front of \(x_{n-2}\).

Define the raw moments

$a_n=\mathbb{E}[X_n^2],\qquad c_n=\mathbb{E}[X_n^3].$

Once these are known, the skewness follows from standard moment identities.

Why the Mean Stays Equal to \(1\)

Because \(\varepsilon_n\) is independent of the past and has mean \(0\),

$\mathbb{E}[X_n]=\mathbb{E}[X_{n-1}]+\mathbb{E}[\varepsilon_n]\mathbb{E}[X_{n-2}]=\mathbb{E}[X_{n-1}].$

Starting from \(\mathbb{E}[X_1]=1\), this gives

$\mathbb{E}[X_n]=1\qquad\text{for all }n\ge 1.$

This invariant is what turns the variance and the third central moment into the simple expressions

$\operatorname{Var}(X_n)=a_n-1,\qquad \mu_{3,n}=c_n-3a_n+2.$

Recurrence for the Second Moment

Expand the square:

$X_n^2=X_{n-1}^2+2\varepsilon_n X_{n-1}X_{n-2}+X_{n-2}^2.$

Taking expectations kills the mixed term, again because \(\mathbb{E}[\varepsilon_n]=0\). Therefore

$a_n=a_{n-1}+a_{n-2}\qquad(n\ge 2),$

with initial values

$a_0=0,\qquad a_1=1.$

So the second moments are exactly the Fibonacci numbers in the convention \(F_0=0\), \(F_1=1\):

$a_n=F_n.$

In particular, the variance is simply \(F_n-1\).

Recurrence for the Third Moment

Cubing the step relation gives

$X_n^3=X_{n-1}^3+3\varepsilon_n X_{n-1}^2X_{n-2}+3X_{n-1}X_{n-2}^2+\varepsilon_n X_{n-2}^3.$

After taking expectations, the terms with an odd factor of \(\varepsilon_n\) vanish, so

$c_n=c_{n-1}+3\,\mathbb{E}[X_{n-1}X_{n-2}^2].$

The remaining mixed moment collapses one step further. Since

$X_{n-1}=X_{n-2}+\varepsilon_{n-1}X_{n-3},$

we have

$\mathbb{E}[X_{n-1}X_{n-2}^2]=\mathbb{E}[X_{n-2}^3]+\mathbb{E}[\varepsilon_{n-1}X_{n-3}X_{n-2}^2]=c_{n-2}.$

Hence the third raw moment satisfies

$c_n=c_{n-1}+3c_{n-2}\qquad(n\ge 2),$

with

$c_0=0,\qquad c_1=1.$

The first values are

$0,1,1,4,7,19,\dots,$

which already show that the third moment grows faster than the second.

Worked Example: \(n=5\)

For \(n=5\) there are \(2^3=8\) equally likely sign patterns, because the random choices start at \(n=3\). Enumerating those eight paths gives

$X_5\in\{5,3,1,1,1,-1,-1,-1\}.$

From this list,

$\mathbb{E}[X_5]=1,\qquad \mathbb{E}[X_5^2]=\frac{25+9+1+1+1+1+1+1}{8}=5,$

and

$\mathbb{E}[X_5^3]=\frac{125+27+1+1+1-1-1-1}{8}=19.$

Therefore

$\operatorname{Var}(X_5)=5-1=4,\qquad \mu_{3,5}=19-3\cdot 5+2=6,$

so the skewness is

$\gamma_5=\frac{6}{4^{3/2}}=\frac{6}{8}=0.75.$

This matches the validation values embedded in the implementations and is a concrete check that the recurrence derivation is correct.

From Moments to Skewness

Once \(a_n\) and \(c_n\) are known, the skewness is

$\gamma_n=\frac{\mu_{3,n}}{\operatorname{Var}(X_n)^{3/2}}=\frac{c_n-3a_n+2}{(a_n-1)^{3/2}}.$

So the whole problem is reduced to iterating two scalar recurrences up to \(n=50\). No probability table, no path enumeration, and no symbolic distribution are required.

How the Code Works

The C++, Python, and Java implementations store only the last two values of the second-moment recurrence and the last two values of the third-moment recurrence. Starting from \((a_0,a_1)=(0,1)\) and \((c_0,c_1)=(0,1)\), each loop step advances both sequences by

$a_n=a_{n-1}+a_{n-2},\qquad c_n=c_{n-1}+3c_{n-2}.$

After the loop reaches \(n=50\), the implementation converts the final moment values into the variance \(a_{50}-1\) and the third central moment \(c_{50}-3a_{50}+2\). It then computes \(\sigma=\sqrt{\operatorname{Var}(X_{50})}\) and returns

$\gamma_{50}=\frac{\mu_{3,50}}{\sigma^3}.$

There is also a small safety guard: if the variance is non-positive, the result is reported as \(0\). For the actual target \(n=50\), the variance is positive, so the normal skewness formula is used.

Complexity Analysis

The running time is \(O(n)\), because the implementation performs one constant-cost recurrence update for each \(n\) from 2 through 50. The memory usage is \(O(1)\), because only a fixed number of scalar values are stored.

This is dramatically smaller than building the full distribution, which would branch into \(2^{n-2}\) equally likely sign histories. The moment method succeeds because skewness depends on only a few aggregated statistics, and those statistics obey closed recurrences.

Footnotes and References

  1. Project Euler problem page: https://projecteuler.net/problem=978
  2. Skewness: Wikipedia - Skewness
  3. Central moment: Wikipedia - Central moment
  4. Fibonacci number: Wikipedia - Fibonacci number
  5. Random Fibonacci sequence: Wikipedia - Random Fibonacci sequence
  6. Linear recurrence with constant coefficients: Wikipedia - Linear recurrence with constant coefficients

Mathematical approach · C++ solution · Python solution · Java solution

Previous: Problem 977 · All Project Euler solutions · Next: Problem 979

Need help with a problem? Ask me! 💡
e
✦ Euler GLM 5.2
Hi! I'm Euler. Ask me anything about Project Euler problems, math concepts, or solution approaches! 🧮