generate.py

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

3230 bytes, as of the version from 2026-12-01 10:12 (current). Recorded here, not run.

"""T890: Copeland–Erdős constants in integer bases.

Requires SageMath and numberdb. Install with sage -pip install numberdb.
Run sage -python generate.py for a full read-only comparison with T890;
use sage -python generate.py --publish only to submit authorized draft entries.
Credentials and engine/model/run provenance belong in the caller's environment.

The parameter b is the integer construction radix, b >= 2. The default
enumeration is every b from 2 to 16. Prime blocks start at 2, have no padding,
and are read in the construction radix. A sieve supplies primes; an integer
accumulator forms complete blocks until b^L > 10^(digits+15). The infinite
continuation is in [A/b^L,(A+1)/b^L]. Exact rational endpoints are rounded
outward by MPFI with at least 512 bits, retaining the nonzero tail uncertainty.
No empirical precision or presumed random digit behavior is used.
"""
import argparse
import math
import os
import sys
import numberdb.sage as numberdb
from sage.rings.integer_ring import ZZ
from sage.rings.rational_field import QQ
from sage.rings.real_mpfi import RealIntervalField

def prime_sieve(limit):
    flags = bytearray([1]) * (limit + 1)
    flags[0:2] = b'\0\0'
    for p in range(2, math.isqrt(limit) + 1):
        if flags[p]:
            flags[p*p:limit+1:p] = b'\0' * ((limit-p*p)//p+1)
    return [p for p in range(2, limit+1) if flags[p]]

def exact_prefix(b, digits=100):
    if isinstance(b, bool) or ZZ(b) != b or b < 2:
        raise ValueError('b must be an integer at least 2')
    b = int(b)
    target = 10 ** (int(digits) + 15)
    limit = 1024
    while True:
        A, D, L = 0, 1, 0
        used = []
        for p in prime_sieve(limit):
            width, power = 1, b
            while p >= power:
                width += 1
                power *= b
            A = A * power + p
            D *= power
            L += width
            used.append(p)
            if D > target:
                return {'b':b,'A':A,'D':D,'L':L,'primes':used}
        limit *= 2

def enclosure(prefix, digits=100):
    field = RealIntervalField(max(512, numberdb.bits(digits, losing=100)))
    A, D = ZZ(prefix['A']), ZZ(prefix['D'])
    lower, upper = QQ(A)/D, QQ(A+1)/D
    result = field(lower, upper)
    assert result.lower().exact_rational() <= lower < upper <= result.upper().exact_rational()
    assert result.lower() > 0 and result.upper() < 1
    return result

class CopelandErdos(numberdb.Generator):
    table = 'T890'
    parameters = ('b',)
    type = 'R'
    digits = 100
    rigour = 'proven'

    def enumerate(self, min_base=2, max_base=16):
        for b in range(min_base, max_base+1):
            yield {'b':b}

    def value(self, params, digits):
        return enclosure(exact_prefix(ZZ(params['b']), digits), digits)

if __name__ == '__main__':
    parser=argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--publish', action='store_true')
    args=parser.parse_args()
    if os.environ.get('NUMBERDB_KEY_FROM_STDIN') == '1':
        numberdb.configure(api_key=sys.stdin.read().strip())
    g=CopelandErdos()
    if args.publish or os.environ.get('NUMBERDB_PUBLISH') == '1':
        print(g.publish())
    else:
        print(g.verify(sample=None))