Skip to content

The Möbius, divisor and prime-counting functions - #2188

Merged
Rafael-SOWNet merged 1 commit into
masterfrom
the-mobius-divisor-and-prime-counting-functions
Oct 28, 2026
Merged

Rafael-SOWNet merged 1 commit into
masterfrom
the-mobius-divisor-and-prime-counting-functions

Conversation

@Rafael-SOWNet

Copy link
Copy Markdown
Member

mobius(n), divisorsigma(k, n), primenu(n), primeomega(n) and primepi(x) were reserved in #2184 and are now functions. Part of #2182.

mobius(n), also moebiusmu μ(n): 0 where a prime's square divides n, (-1)^k for k distinct primes
divisorsigma(k, n) σ_k(n), the sum of d^k over the divisors d of n; any order
primenu(n), primeomega(n) ω(n) and Ω(n), prime factors without and with multiplicity
primepi(x) π(x), the number of primes at most the real x

The first four are defined on the positive integers and give NaN at any other number. Factorisation is trial division up to 10^6. That settles every number up to 10^12, and any larger one whose cofactor after the small primes is below 10^12; anything else is left as written. primepi uses a sieve up to 10^7, which takes 94 ms there.

Input Was (2.6.0) Now
"mobius(6)".ToEntity().Simplify() 6 * mobius 1
"divisorsigma(1, 6)".ToEntity().Simplify() throws UnhandledParseException 12
"primeomega(12)".ToEntity().Simplify() 12 * primeomega 3
"primepi(10)".ToEntity().Evaled primepi * 10 4
"mobius(0)".ToEntity().Simplify() 0 NaN

LaTeX output is \operatorname{\mu}(n), \operatorname{\sigma}_k(n), \operatorname{\omega}, \operatorname{\Omega} and \operatorname{\pi}. SymPy output is mobius, divisor_sigma(n, k), primenu, primeomega and primepi.

🤖 Generated with Claude Code

https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura

@Rafael-SOWNet Rafael-SOWNet added this to the 2.7.0 milestone Oct 28, 2026
@Rafael-SOWNet Rafael-SOWNet moved this from Todo to In review in AngouriMath Oct 28, 2026
@Rafael-SOWNet Rafael-SOWNet removed this from the 2.7.0 milestone Oct 28, 2026
@codecov

codecov Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.53861% with 29 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.54%. Comparing base (5e7731f) to head (0348dfc).

Files with missing lines Patch % Lines
...ources/AngouriMath/Core/Antlr/AngouriMathParser.cs 90.90% 0 Missing and 6 partials ⚠️
...th/Core/Entity/Discrete/Entity.Discrete.Classes.cs 76.92% 6 Missing ⚠️
...Evaluation.Discrete/Evaluation.Discrete.Classes.cs 86.84% 5 Missing ⚠️
...AngouriMath/Functions/TreeAnalyzer/Sort.Classes.cs 0.00% 5 Missing ⚠️
Sources/AngouriMath/Functions/Substitute.cs 20.00% 0 Missing and 4 partials ⚠️
...urces/AngouriMath/Functions/NumberTheory/Primes.cs 94.87% 1 Missing and 1 partial ⚠️
...nuous/Solvers/EquationSolver/InvertNode.Classes.cs 80.00% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2188      +/-   ##
==========================================
+ Coverage   86.52%   86.54%   +0.02%     
==========================================
  Files         297      297              
  Lines       66161    66324     +163     
  Branches    19237    19267      +30     
==========================================
+ Hits        57245    57402     +157     
  Misses       4851     4851              
- Partials     4065     4071       +6     
Flag Coverage Δ
cpp 37.77% <ø> (ø)
csharp 87.10% <94.53%> (+0.02%) ⬆️
fsharp 32.97% <ø> (ø)
interactive 40.72% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
Sources/AngouriMath/Convenience/MathS.cs 90.22% <100.00%> (+0.15%) ⬆️
Sources/AngouriMath/Core/Antlr/AngouriMathLexer.cs 99.21% <100.00%> (-0.01%) ⬇️
Sources/AngouriMath/Core/CostModel.cs 97.22% <100.00%> (+0.03%) ⬆️
Sources/AngouriMath/Core/Domains.Classes.cs 100.00% <100.00%> (ø)
...Math/Core/Transformations/Matching/MatchPattern.cs 86.21% <100.00%> (+0.15%) ⬆️
...unctions/Output/Latex/Latex.Arithmetics.Classes.cs 97.58% <100.00%> (+0.10%) ⬆️
...ns/Output/ToString/ToString.Arithmetics.Classes.cs 100.00% <100.00%> (ø)
...ions/Output/ToSympy/ToSympy.Arithmetics.Classes.cs 75.00% <100.00%> (+2.27%) ⬆️
...s/AngouriMath/Functions/TreeAnalyzer/RealValued.cs 81.52% <ø> (ø)
...nuous/Solvers/EquationSolver/InvertNode.Classes.cs 60.80% <80.00%> (+0.39%) ⬆️
... and 6 more

... and 5 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

mobius(n), divisorsigma(k, n), primenu(n), primeomega(n) and primepi(x) were reserved by name in
built by MathS.NumberTheory.Mobius, DivisorSigma, PrimeNu, PrimeOmega and PrimePi.

mobius is the Möbius function: 0 where the square of a prime divides n, (-1)^k for a product of k
distinct primes, and 1 at 1. moebiusmu, Mathematica's name for it, reads as the same function.
divisorsigma(k, n) is sigma_k(n), the order first as the notation and Mathematica's
DivisorSigma[k, n] write it. It is the product over the prime powers p^a in n of
1 + p^k + ... + p^(ak), exact for a rational order and an expression for any other number:
divisorsigma(1/2, 4) is 3 + sqrt(2). primenu and primeomega are omega and Omega, the number of
prime factors without and with multiplicity. The four are defined on the positive integers, NaN at
any other number, and left as written for an expression.

The factorisation is trial division up to a million. That settles every number up to 10^12, and a
larger one whose part left after the small primes is below 10^12; anything else is left as written,
and 2^100 is factorised at once. primepi(x) counts the primes up to floor(x) by a sieve over the odd
numbers, for a real x up to ten million (94 ms there), and is NaN off the real line.

They print by name, draw as \operatorname{\mu}(n), \operatorname{\sigma}_k(n),
\operatorname{\omega}(n), \operatorname{\Omega}(n) and \operatorname{\pi}(x), so that a Greek
letter is not read back as a variable times a bracket, and export to SymPy's mobius,
divisor_sigma(n, k), primenu, primeomega and primepi. Their codomains are the integers, and the
non-negative integers for the three counts. The MCP server's syntax document and the syntax page
name them, and the native smoke test evaluates them.

Part of #2182.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura
@Rafael-SOWNet
Rafael-SOWNet force-pushed the the-mobius-divisor-and-prime-counting-functions branch from fa6ad4b to 0348dfc Compare October 28, 2026 20:35
@Rafael-SOWNet
Rafael-SOWNet merged commit 60c1a3f into master Oct 28, 2026
36 of 37 checks passed
@github-project-automation github-project-automation Bot moved this from In review to Done in AngouriMath Oct 28, 2026
Rafael-SOWNet added a commit that referenced this pull request Oct 28, 2026
The Kernel Benchmark gate failed twice today on unchanged library code: on #2187's first run, and
on #2188 after its rebase. Both times it was SimplifyEasy, SolveEasy and EvalTrigPrecise, at
3-12x their time, and SimplifyEasy at 3% more allocation. Their re-runs matched the baseline to the
byte.

BenchmarkDotNet times one call before it decides how to measure, and where that call takes more
than two thirds of its 500 ms iteration, it times one call per iteration from then on. The first
call of those three took 349-362 ms on the failing runs and 252-300 ms on the passing ones. For a
call of tens of microseconds, one call per iteration is another measurement. That first call paid
for parsing every static expression of the class and for the library's own set-up: EvalEasy, a
cache lookup, took 133 ms the first time on one machine.

A [GlobalSetup] now touches the class and runs one simplification, one solve and one evaluation
before the timed call. Measured on one machine, master against this, the first call drops from
172 to 8 ms (SimplifyEasy), 164 to 5 ms (SolveEasy) and 158 to 45 ms (EvalTrigPrecise). What the
gated benchmarks allocate is unchanged: SimplifyEasy is 79,682 B/op on both, and the largest move is
ParseEasy's +0.11% (21 B/op). The two compilation benchmarks, which the gate leaves out for their
own spread, moved -0.5% and +2.0%. SolveHard, a call of 70 ms, is now timed one call per iteration rather than
piloted, and allocates 9,832,912 B/op against 9,831,370.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura
@Rafael-SOWNet
Rafael-SOWNet deleted the the-mobius-divisor-and-prime-counting-functions branch October 29, 2026 00:26
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