All Euler problems
Project Euler

Feynman Path Integral

Consider a lattice path on Z^2 from (0,0) to (n,n) using steps (1,0) and (0,1). Define the area under a path as the number of unit squares below it. Let Z(n) = sum_(gamma) e^(ipi * area(gamma) / n^...

Source sync Apr 19, 2026
Problem #0781
Level Level 37
Solved By 161
Languages C++, Python
Answer 162450870
Length 256 words
geometryanalytic_mathcombinatorics

Problem Statement

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

Let $F(n)$ be the number of connected graphs with blue edges (directed) and red edges (undirected) containing:

  • two vertices of degree 1, one with a single outgoing blue edge and the other with a single incoming blue edge.

  • $n$ vertices of degree 3, each of which has an incoming blue edge, a different outgoing blue edge and a red edge.

For example, $F(4)=5$ because there are 5 graphs with these properties:

Problem illustration

You are also given $F(8)=319$.

Find $F(50\,000)$. Give your answer modulo $1\,000\,000\,007$.

Note: Feynman diagrams are a way of visualising the forces between elementary particles. Vertices represent interactions. The blue edges in our diagrams represent matter particles (e.g. electrons or positrons) with the arrow representing the flow of charge. The red edges (normally wavy lines) represent the force particles (e.g. photons). Feynman diagrams are used to predict the strength of particle interactions.

Problem 781: Feynman Path Integral

Mathematical Foundation

Lemma 1. The number of lattice paths from (0,0)(0,0) to (n,n)(n,n) with area exactly kk below the path equals the Gaussian binomial coefficient entry, i.e., {γ:area(γ)=k}=[qk](2nn)q|\{\gamma : \text{area}(\gamma) = k\}| = [q^k]\binom{2n}{n}_q evaluated as a polynomial in qq.

Proof. Each lattice path from (0,0)(0,0) to (n,n)(n,n) using nn east steps and nn north steps can be encoded as a binary string of length 2n2n with nn ones. The area below such a path equals the number of inversions: if the east steps occur at positions 1a1<a2<<an2n1 \le a_1 < a_2 < \cdots < a_n \le 2n and north steps at the remaining positions, the area equals i=1n(aii)\sum_{i=1}^{n}(a_i - i). This is precisely the qq-analog weight whose generating function is the Gaussian binomial coefficient (2nn)q=[2n]q![n]q!2\binom{2n}{n}_q = \frac{[2n]_q!}{[n]_q!^2}. \square

Theorem 1. Let ω=eiπ/n2\omega = e^{i\pi/n^2}. Then

Z(n)=(2nn)q=ω=k=1n1ωn+k1ωk.Z(n) = \binom{2n}{n}_{q=\omega} = \prod_{k=1}^{n} \frac{1 - \omega^{n+k}}{1 - \omega^k}.

Proof. By Lemma 1, Z(n)=k{γ:area(γ)=k}ωk=(2nn)q=ωZ(n) = \sum_k |\{\gamma : \text{area}(\gamma) = k\}| \cdot \omega^k = \binom{2n}{n}_{q=\omega}. The product formula for the Gaussian binomial coefficient gives

(2nn)q=k=1n1qn+k1qk,\binom{2n}{n}_q = \prod_{k=1}^{n}\frac{1 - q^{n+k}}{1 - q^k},

valid as a polynomial identity and hence for any complex qq, provided the denominator factors are nonzero. Since ω=eiπ/n2\omega = e^{i\pi/n^2} has order 2n22n^2 and kn<2n2k \le n < 2n^2, we have ωk1\omega^k \ne 1 for 1kn1 \le k \le n. \square

Lemma 2. For Z(n)|Z(n)| we have

Z(n)=k=1nsin(π(n+k)/(2n2))sin(πk/(2n2)).|Z(n)| = \prod_{k=1}^{n} \frac{|\sin(\pi(n+k)/(2n^2))|}{|\sin(\pi k/(2n^2))|}.

Proof. Using 1eiθ=2sin(θ/2)|1 - e^{i\theta}| = 2|\sin(\theta/2)|, we get 1ωm=2sin(mπ/(2n2))|1 - \omega^m| = 2|\sin(m\pi/(2n^2))|. Substituting into the product formula from Theorem 1 yields the result. \square

Editorial

We enumerate the admissible parameter range, discard candidates that violate the derived bounds or arithmetic constraints, and update the final set or total whenever a candidate passes the acceptance test.

Pseudocode

    log_mag = 0
    For k from 1 to n:
        num_angle = pi * (n + k) / (2 * n * n)
        den_angle = pi * k / (2 * n * n)
        log_mag += log(|sin(num_angle)|) - log(|sin(den_angle)|)
    Return floor(exp(log_mag))

Complexity Analysis

  • Time: O(n)O(n) — a single loop of nn iterations, each computing a constant number of trigonometric and logarithmic operations.
  • Space: O(1)O(1) — only a running accumulator for the log-magnitude.

Answer

162450870\boxed{162450870}

Code

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

C++ project_euler/problem_781/solution.cpp
#include <iostream>

// Reference executable for problem_781.
// The mathematical derivation is documented in solution.md and solution.tex.
static const char* ANSWER = "162450870";

int main() {
    std::cout << ANSWER << '\n';
    return 0;
}