generate.py

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

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

"""Generate and verify T892: prime-indicator reals in integer bases.

Requirements: SageMath (or the corresponding passagemath rings) and numberdb.
  sage -pip install numberdb
  sage -python generate.py             # verify every stored entry; no writes
  sage -python generate.py --publish   # explicitly fill the authorized table

Configure your own NumberDB key for access to this private draft. No credential
or agent/model declaration is embedded here. Future writes also require the
session's NUMBERDB_ENGINE and NUMBERDB_MODEL declarations.

The sole parameter b is an integer construction radix >= 2; the default
selection is every b=2,...,16 in increasing order. Position 1 is the first
fractional digit; the primes begin at 2. The quantity is sum_p b^(-p), without
rescaling. OEIS A051006 defines the binary member and the prime-position rule.

A sieve finds primes <= M. All summation is exact QQ arithmetic. Choose M by
(b-1)*b^M > 10^(digits+20). The omitted positive terms are bounded by the sum
of ALL integer positions after M, namely 1/((b-1)*b^M). RealIntervalField
converts both exact endpoints outward at max(512,bits(digits+40)) bits. Thus
the interval encloses the infinite real, rather than only its rational prefix.
Default values use 512 bits; serialization at 100 digits is independently
checked in the attached evidence. More precision is available via value().
"""
import argparse
import os
import sys
from math import isqrt
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_positions(cutoff):
    flags = [True] * (cutoff + 1)
    flags[0:2] = [False, False]
    for divisor in range(2, isqrt(cutoff) + 1):
        if flags[divisor]:
            for composite in range(divisor * divisor, cutoff + 1, divisor):
                flags[composite] = False
    return [n for n in range(2, cutoff + 1) if flags[n]]


def exact_bracket(base, digits):
    b = ZZ(base)
    if b < 2:
        raise ValueError('b must be an integer >= 2')
    threshold = ZZ(10) ** (int(digits) + 20)
    cutoff = 1
    while (b - 1) * b ** cutoff <= threshold:
        cutoff += 1
    primes = prime_positions(cutoff)
    partial = sum((QQ(1) / (b ** p) for p in primes), QQ(0))
    tail = QQ(1) / ((b - 1) * b ** cutoff)
    assert type(partial).__name__ == 'Rational'
    assert type(tail).__name__ == 'Rational'
    return cutoff, primes, partial, partial + tail


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

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

    def value(self, params, digits):
        _, _, lower, upper = exact_bracket(params['b'], digits)
        field = RealIntervalField(max(512, numberdb.bits(int(digits) + 40)))
        result = field(lower, upper)
        assert not result.is_NaN()
        assert result.lower() < result.upper()
        return result


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':
        os.environ['NUMBERDB_API_KEY'] = sys.stdin.read().strip()
    generator = PrimeIndicator()
    if args.publish:
        print(generator.publish(removing=False))
    else:
        print(generator.verify(sample=None))