generate.py

back to table · edit · history · where entries came from · files · download

2773 bytes, as of the version from 2026-10-29 05:38 (current). Recorded here, not run.

"""Landau constants G_n -- numberdb.org/T601.

Run with SageMath (or passagemath) and the numberdb client installed:
    sage -pip install numberdb
    sage -python generate.py              # verify every stored entry
    sage -python checks.py                # independent checks, no API access
    sage -python generate.py --fill       # write entries to the existing draft

The --fill option requires NUMBERDB_API_KEY and writes entries only; it does
not offer, review, or publish a draft. Private verification also needs a key.
For campaign wrappers, NUMBERDB_KEY_FROM_STDIN=1 reads the key from stdin.

The sole parameter n is the largest retained Taylor degree. Default coverage
is all 0 <= n <= 100. All arithmetic is in QQ, with no rounding or precision
choice. The recurrence advances the squared odd/even product and its partial
sum. The independent checker counts square-lattice walks by dynamic programming
and also evaluates the central-binomial closed form with Python integers.
See argument-selection.md for the range decision and source conventions.
"""

import os
import sys

import numberdb.sage as numberdb
from sage.rings.integer_ring import ZZ
from sage.rings.rational_field import QQ


class LandauConstants(numberdb.Generator):
    table = 'T601'
    parameters = ('n',)
    type = 'Q'
    rigour = 'exact'
    digits = 100  # Exact rational output is not rounded to this many digits.
    files = ('generate.py', 'checks.py', 'check.py', 'argument-selection.md',
             'numerical-evidence.md', 'skill-evidence.json', 'skill-manifest.json')

    def enumerate(self, maximum=100):
        maximum = ZZ(maximum)
        if maximum < 0:
            raise ValueError('maximum must be a nonnegative integer')
        for n in range(int(maximum) + 1):
            yield {'n': n}

    def value(self, params, digits):
        n = ZZ(params['n'])
        if n < 0:
            raise ValueError('n must be a nonnegative integer')
        term = QQ(1)
        total = QQ(1)
        for k in range(1, int(n) + 1):
            term *= (QQ(2 * k - 1) / QQ(2 * k)) ** 2
            total += term
        return total


def authenticate_stdin():
    if os.environ.get('NUMBERDB_KEY_FROM_STDIN') == '1':
        key = sys.stdin.read().strip()
        if key.startswith('NUMBERDB_') and '=' in key:
            key = key.split('=', 1)[1].strip().strip('\"\'')
        os.environ['NUMBERDB_API_KEY'] = key


if __name__ == '__main__':
    authenticate_stdin()
    generator = LandauConstants()
    if '--fill' in sys.argv or os.environ.get('NUMBERDB_PUBLISH') == '1':
        print(generator.publish(message='Compute exact Landau constants'))
    else:
        report = generator.verify(sample=None)
        print(report)
        sys.exit(0 if report.ok else 1)