Skip to content

Make Spouge's constants from exact ratios and powers of e - #2090

Merged
Rafael-SOWNet merged 1 commit into
masterfrom
spouges-constants-without-a-power-or-an-exponential-each
Oct 23, 2026
Merged

Rafael-SOWNet merged 1 commit into
masterfrom
spouges-constants-without-a-power-or-an-exponential-each

Conversation

@Rafael-SOWNet

Copy link
Copy Markdown
Member

Builds Spouge's constants for the factorial without a power and an exponential each. The first factorial at a precision is 36 to 145 times faster, with the same digits. Part of #2075.

What was slow. The factorial of a non-integer is Spouge's approximation, with a = 1.3 P constants at P digits, cached per precision. Each constant c_k = (-1)^(k - 1) (a - k)^(k - 1/2) e^(a - k)/(k - 1)! was made with a power to a non-integer exponent and an exponential. That is a logarithm and two exponentials at 1.95 P digits for each of the 1.3 P terms.

Now. (a - k)^(k - 1)/(k - 1)! is a ratio of exact integers, sqrt(a - k) one square root, and e^(a - k) the running product of e. The product is carried with ten more digits and rounded back.

first factorial at master this
100 digits 0.93 s 0.12 s
300 digits 20.5 s 0.57 s
600 digits 218 s 1.5 s
1000 digits 5.5 s
2000 digits 30 s

The values are unchanged. I checked Gamma(1/3), Gamma(1/3 + i), Gamma(2/7), Gamma(-7/3), Gamma(101/2) and Gamma(-5/2 + 3i) against mpmath. They have the same error on both builds at 100 and 300 digits, within 1.3 units of the last digit, and stay within that at 600 and 1000. Once the constants exist, an evaluation at 100 digits costs the same on both builds, 7 to 10 ms.

Measured on Rubi's suite, with the harness that reads GAMMA, against this branch's base 3dbae246, the whole corpus each side:

3dbae246 this
solved 48,875 of 49,052 48,880
wrong 0 0
timeouts 7 2
rows lost 0

The five gains are the timeouts #2088 also ended: 6.1.1:205-207 and 6.1.3:98/100. The harness checks each answer again at 300 and 2000 digits, which put a gamma function at a new precision. Allocation per stream is 0.9998, 1.0000 and 0.9960 of the base's. The times, 1.04, 1.08 and 0.98, were taken with other builds running on the machine, and the trigonometric family, at 1.08, does not reach this code. Two rows' allocation grew in the run, 6.1.7:196 and 8.1:302. Alone they are the same on both builds: 1.079 GB each, and 1.406 against 1.407 GB.

A test of (1/3)! and (1/3 + i)! at 300 digits is added. The suite passes (16,940), and so do the MCP tests (329).

🤖 Generated with Claude Code

https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura

The factorial of a non-integer is Spouge's approximation, with a = 1.3 P constants at P digits,
cached per precision. Each constant c_k = (-1)^(k - 1) (a - k)^(k - 1/2) e^(a - k)/(k - 1)! was made
with a power to a non-integer exponent and an exponential, a logarithm and two exponentials at
1.95 P digits for each of the 1.3 P terms. The first factorial at a precision
took 0.9 s at 100 digits, 20.5 s at 300 and 218 s at 600.

(a - k)^(k - 1)/(k - 1)! is a ratio of exact integers, sqrt(a - k) one square root, and e^(a - k)
the running product of e, carried with ten more digits for the a multiplications and rounded
back to the constants' precision. The first factorial now takes 0.12 s at 100 digits, 0.57 s at
300, 1.5 s at 600, 5.5 s at 1000 and 30 s at 2000.

The values are unchanged. Against mpmath, Gamma(1/3), Gamma(1/3 + i), Gamma(2/7), Gamma(-7/3),
Gamma(101/2) and Gamma(-5/2 + 3i) have the same error on both builds at 100 and 300 digits,
within 1.3 units of the last digit, and stay within that at 600 and 1000. Once compiled, an
evaluation at 100 digits costs the same, 7 to 10 ms, on both. A test of (1/3)! and (1/3 + i)! at
300 digits is added.

Part of #2075.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant