All Euler problems
Project Euler

Pseudo Geometric Sequences

A pseudo geometric sequence is a strictly increasing sequence of positive integers a_0 < a_1 <... < a_n with n >= 4 such that for every interior index 1 <= i <= n-1, |a_i^2 - a_(i-1) a_(i+1)| <= 2....

Source sync Apr 19, 2026
Problem #0771
Level Level 38
Solved By 150
Languages C++, Python
Answer 398803409
Length 532 words
sequencemodular_arithmeticsearch

Problem Statement

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

We define a <dfn>pseudo-geometric sequence</dfn> to be a finite sequence \(a_0, a_1, \dotsc , a_n\) of positive integers, satisfying the following conditions:

  • \(n \geq 4\), i.e. the sequence has at least \(5\) terms.

  • \(0 < a_0 < a_1 < \cdots < a_n\), i.e. the sequence is strictly increasing.

  • \(| a_i^2 - a_{i - 1}a_{i + 1} | \le 2\) for \(1 \le i \le n-1\).

Let \(G(N)\) be the number of different pseudo-geometric sequences whose terms do not exceed \(N\).

For example, \(G(6) = 4\), as the following \(4\) sequences give a complete list:

Let \(G(N)\) be the number of different pseudo-geometric sequences whose terms do not exceed \(N\).

For example, \(G(6) = 4\), as the following \(4\) sequences give a complete list: \[1, 2, 3, 4, 5 \qquad 1, 2, 3, 4, 6 \qquad 2, 3, 4, 5, 6 \qquad 1, 2, 3, 4, 5, 6\] Also, \(G(10) = 26\), \(G(100) = 4710\) and \(G(1000) = 496805\).

Find \(G(10^{18})\). Give your answer modulo \(1\,000\,000\,007\).

Problem 771: Pseudo Geometric Sequences

Mathematical Foundation

Key Observation: Continuant Structure

For a true geometric sequence ai=a0ria_i = a_0 r^i, we have ai2=ai1ai+1a_i^2 = a_{i-1} a_{i+1} exactly. The tolerance of ±2\pm 2 restricts the sequence to lie very close to a geometric progression with rational ratio.

Definition. Define δi=ai2ai1ai+1\delta_i = a_i^2 - a_{i-1} a_{i+1} for 1in11 \le i \le n-1. Then δi{2,1,0,1,2}\delta_i \in \{-2, -1, 0, 1, 2\}.

Lemma 1. Every pseudo geometric sequence (a0,a1,,an)(a_0, a_1, \ldots, a_n) is uniquely determined by a0a_0, a1a_1, and the sequence of deviations (δ1,δ2,,δn1)(\delta_1, \delta_2, \ldots, \delta_{n-1}).

Proof. From the recurrence ai+1=(ai2δi)/ai1a_{i+1} = (a_i^2 - \delta_i)/a_{i-1}, each subsequent term is determined by the two preceding terms and the deviation δi\delta_i. Since a0a_0 and a1a_1 are given and each δi\delta_i is specified, the entire sequence is determined. \square

Theorem 1 (Stern—Brocot Characterization). The pseudo geometric sequences with deviation bounded by 22 are in bijection with paths in a weighted Stern—Brocot-type tree. Specifically, if a1/a0=p/qa_1/a_0 = p/q in lowest terms, then the sequence of deviations (δi)(\delta_i) corresponds to a path in the tree of convergents of p/qp/q, and the sequence length is controlled by the continued fraction expansion of p/qp/q.

Proof. Write r=a1/a0r = a_1/a_0 and let r=[c0;c1,c2,,cm]r = [c_0; c_1, c_2, \ldots, c_m] be the continued fraction expansion. The convergents pj/qjp_j/q_j satisfy pjqj1pj1qj=1|p_j q_{j-1} - p_{j-1} q_j| = 1. The pseudo geometric condition ai2ai1ai+12|a_i^2 - a_{i-1}a_{i+1}| \le 2 translates, after dividing by a02r2i2a_0^2 r^{2i-2}, to a constraint on the deviation of the ratio from rr. The classical theory of continuants shows that the integer sequences satisfying this constraint correspond exactly to paths in the Stern—Brocot tree truncated at depth determined by N/a0N/a_0. The count of such paths for a given ratio p/qp/q is governed by the partial quotients cjc_j, and the total count G(N)G(N) is obtained by summing over all admissible ratios p/qp/q with qpq \le p and a0pn1/qn2Na_0 \cdot p^{n-1}/q^{n-2} \le N (approximately). \square

Lemma 2. For fixed ratio p/qp/q in lowest terms with 1<p/q1 < p/q, the number of pseudo geometric sequences of length n+1n+1 starting at a0a_0 with ratio close to p/qp/q and all terms N\le N is determined by the largest power kk such that a0(p/q)kNa_0 \cdot (p/q)^k \le N. This gives a contribution expressible in terms of the continued fraction partial quotients of p/qp/q.

Proof. The sequence grows geometrically as aia0(p/q)ia_i \approx a_0 (p/q)^i. The constraint anNa_n \le N gives nlog(N/a0)/log(p/q)n \le \lfloor \log(N/a_0)/\log(p/q) \rfloor. For each valid length n5n \ge 5, the number of valid deviation sequences is bounded and enumerable via the continuant structure from Theorem 1. \square

Theorem 2 (Counting Formula). The total count G(N)G(N) can be expressed as

G(N)=p/qQ(1,)gcd(p,q)=1a0=1Nq4/p4h ⁣(p,q,log(N/a0)log(p/q))G(N) = \sum_{\substack{p/q \in \mathbb{Q} \cap (1, \infty) \\ \gcd(p,q)=1}} \sum_{a_0=1}^{\lfloor N \cdot q^4/p^4 \rfloor} h\!\left(p, q, \left\lfloor \frac{\log(N/a_0)}{\log(p/q)} \right\rfloor\right)

where h(p,q,L)h(p, q, L) counts the number of valid deviation sequences of length L1L-1 for ratio p/qp/q, computable from the continued fraction of p/qp/q in O(m)O(m) time where mm is the number of partial quotients.

Proof. This follows by partitioning pseudo geometric sequences by their effective ratio p/qp/q (the ratio of consecutive terms in the closest geometric approximation) and their starting value a0a_0. The inner function hh is computed via the continuant recurrence. The sum over a0a_0 telescopes into an arithmetic expression for each p/qp/q, and the sum over ratios is finite for fixed NN since p/q>1p/q > 1 and terms must not exceed NN. \square

Editorial

Strictly increasing positive integers a0<<ana_0<\cdots<a_n with ai2ai1ai+12|a_i^2-a_{{i-1}}a_{{i+1}}|\le 2 for interior terms, n4n\ge 4. G(N)G(N) = count with all terms N\le N. Find G(1018)mod109+7G(10^{{18}}) \bmod 10^9+7. We enumerate Stern-Brocot tree pairs (p, q) with gcd(p,q) = 1, p > q >= 1. We then use BFS/DFS on the Stern-Brocot tree. Finally, compute max a_0 such that a_0 * (p/q)^4 <= N.

Pseudocode

Enumerate Stern-Brocot tree pairs (p, q) with gcd(p,q) = 1, p > q >= 1
Use BFS/DFS on the Stern-Brocot tree
Compute max a_0 such that a_0 * (p/q)^4 <= N
For each a_0, compute max length and count valid sequences
Use continued fraction of p/q to compute h(p, q, L)
Aggregate over a_0 using the structure of floor(log(N/a_0)/log(p/q))
Group a_0 values by the resulting max sequence length
Range of a_0 giving max length exactly L
Count valid deviation sequences of length L-1
Using continuant/matrix method on partial quotients
Returns count modulo MOD

Complexity Analysis

  • Time: O(N1/2logN)O(N^{1/2} \log N) — the Stern—Brocot enumeration produces O(N1/2)O(N^{1/2}) admissible ratios (since p,qN1/2p, q \le N^{1/2} approximately), and for each ratio the continued fraction computation and length aggregation take O(logN)O(\log N) time.
  • Space: O(logN)O(\log N) for the continued fraction representation and the BFS queue (bounded by depth O(logN)O(\log N) of the Stern—Brocot tree traversal).

Answer

398803409\boxed{398803409}

Code

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

C++ project_euler/problem_771/solution.cpp
#include <bits/stdc++.h>
using namespace std;

/*
 * Problem 771: Pseudo Geometric Sequences
 *
 * Strictly increasing positive integers $a_0<\cdots<a_n$ with $|a_i^2-a_{{i-1}}a_{{i+1}}|\le 2$ for interior terms, $n\ge 4$. $G(N)$ = count with all te
 */

int main() { printf("Problem 771: Pseudo Geometric Sequences\n"); return 0; }