Skip to content

feat(NumberTheory): decide, #eval and norm_num for Carmichael numbers - #43965

Open
YaelDillies wants to merge 10 commits into
leanprover-community:masterfrom
YaelDillies:norm_num_carmichael
Open

YaelDillies wants to merge 10 commits into
leanprover-community:masterfrom
YaelDillies:norm_num_carmichael

Conversation

@YaelDillies

@YaelDillies YaelDillies commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

Provide both an efficient decidability instance and a norm_num extension to decide whether a given literal is a Carmichael number.

This lets us prove that 561 is the smallest Carmichael number "in a computationally efficient manner". Deprecate the version of Korselt's criterion that was supposed to be used for this (but was less efficient than these new instance/extension).

Generated by Claude Opus in several rounds of review to optimise various parts of the algorithms and make sure that the decidability instance works for both #eval and decide. I haven't made much of an effort to clean the proofs (existing proofs internal to norm_num read like garbage to me anyway).

From FormalConjectures

Assisted-by: Claude Opus 5.1


Open in Gitpod

…umbers

Provide both an efficient decidability instance and a `norm_num` extension to decide whether a given literal is a Carmichael number.

This lets us prove that 561 is the smallest Carmichael number "in a computationally efficient manner".

Generated by Claude Opus in several rounds of review to optimise various parts of the algorithms and make sure that the decidability instance works for both `#eval` and `decide`. I haven't made much of an effort to clean the proofs (existing proofs internal to `norm_num` read like garbage to me anyway).

From FormalConjectures

Assisted-by: Claude Opus 5.1
@YaelDillies YaelDillies added t-meta Tactics, attributes or user commands LLM-generated PRs with substantial input from LLMs - review accordingly labels Sep 19, 2026
@github-actions

github-actions Bot commented Sep 19, 2026 •

Copy link
Copy Markdown

PR summary e3971fcc0e

Import changes for modified files

No significant changes to the import graph

Import changes for all files
Files Import difference

Declarations diff (regex)

+ KorseltInvt
+ KorseltInvt.mul
+ KorseltInvt.one
+ add_two_le_of_forall_prime_dvd
+ instance : DecidablePred IsCarmichael := fun n ↦
+ isCarmichael_of_korseltInvt
+ isLeast_isCarmichael_561
+ isNat_isCarmichael
+ isNat_not_isCarmichael
+ korseltAux
+ korseltAux_iff
+ not_isCarmichael_of_ble_two
+ not_isCarmichael_of_mod_two_eq_zero
+ not_isCarmichael_of_mul_eq
+ not_isCarmichael_of_mul_mul_eq

You can run this locally as follows
## from your `mathlib4` directory:
git clone https://github.com/leanprover-community/mathlib-ci.git ../mathlib-ci

## summary with just the declaration names:
../mathlib-ci/scripts/pr_summary/declarations_diff.sh <optional_commit>

## more verbose report:
../mathlib-ci/scripts/pr_summary/declarations_diff.sh long <optional_commit>

The doc-module for scripts/pr_summary/declarations_diff.sh in the mathlib-ci repository contains some details about this script.

Declarations diff (Lean)

✅ Lean-aware diff — post-build, computed from the Lean environment (commit e3971fc).

  • +16 new declarations
  • −0 removed declarations
+Mathlib.Meta.NormNum.KorseltInvt
+Mathlib.Meta.NormNum.KorseltInvt.mul
+Mathlib.Meta.NormNum.KorseltInvt.one
+Mathlib.Meta.NormNum.deriveNatPrime
+Mathlib.Meta.NormNum.evalNatIsCarmichael
+Mathlib.Meta.NormNum.isCarmichael_of_korseltInvt
+Mathlib.Meta.NormNum.isNat_isCarmichael
+Mathlib.Meta.NormNum.isNat_not_isCarmichael
+Mathlib.Meta.NormNum.not_isCarmichael_of_ble_two
+Mathlib.Meta.NormNum.not_isCarmichael_of_mod_two_eq_zero
+Mathlib.Meta.NormNum.not_isCarmichael_of_mul_eq
+Mathlib.Meta.NormNum.not_isCarmichael_of_mul_mul_eq
+Nat.instDecidablePredIsCarmichael
+Nat.isLeast_isCarmichael_561
+Nat.korseltAux
+Nat.korseltAux_iff

No changes to strong technical debt.
No changes to weak technical debt.

Current commit e3971fcc0e
Reference commit c47f9ad906

This script lives in the mathlib-ci repository. To run it locally, from your mathlib4 directory:

git clone https://github.com/leanprover-community/mathlib-ci.git ../mathlib-ci
../mathlib-ci/scripts/reporting/technical-debt-metrics.py pr_summary
  • The relative value is the weighted sum of the differences with weight given by the inverse of the current value of the statistic.
  • The absolute value is the relative value divided by the total sum of the inverses of the current values (i.e. the weighted average of the differences).

@github-actions github-actions Bot added the t-number-theory Number theory (also use t-algebra or t-analysis to specialize) label Sep 19, 2026
@SnirBroshi

Copy link
Copy Markdown
Collaborator

!bench

@leanprover-radar

leanprover-radar commented Sep 19, 2026 •

Copy link
Copy Markdown

Benchmark results for a12253a against f61f3ed are in. No significant results found. @SnirBroshi

  • 🟥 build//instructions: +93.8G (+0.07%)

Small changes (2🟥)

  • 🟥 build/module/Batteries.Data.Array.Pairwise//instructions: +25.1M (+1.23%)
  • 🟥 build/module/Mathlib.NumberTheory.CarmichaelNumber//instructions: +20.9G (+207.57%) (reduced significance based on *//lines)

@mathlib-merge-conflicts mathlib-merge-conflicts Bot added the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Oct 9, 2026
@mathlib-merge-conflicts

Copy link
Copy Markdown

This pull request has conflicts, please merge master and resolve them.

@github-actions github-actions Bot removed the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Oct 13, 2026

@joneugster joneugster left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR is on the larger side for reviewing, but overall it looks good to me, thanks!

Comment on lines +184 to +186
if m % d = 0 then
(n - 1) % (d - 1) == 0 && m / d % d != 0 && korseltAux n fuel (m / d) (d + 2) true
else korseltAux n fuel m (d + 2) c

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit confused by the incosistent usage of = 0 and == 0. It also seems that the proof breaks very easily if you change any of these into the other form.

Could you make this more consistent and robust?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I'll unresolve this for context, no action needed)

Thanks for making this consistent. I wasn't quite aware how ugly the boolean version would look like, but that is a separate issue...

@joneugster joneugster added the awaiting-author Reply -awaiting-author to remove the label on your PR once you have addressed all comments. label Nov 6, 2026
@YaelDillies YaelDillies removed the awaiting-author Reply -awaiting-author to remove the label on your PR once you have addressed all comments. label Nov 6, 2026
@joneugster

Copy link
Copy Markdown
Contributor

Thanks!

maintainer merge

@github-actions

Copy link
Copy Markdown

🚀 Pull request has been placed on the maintainer queue by joneugster.

@mathlib-triage mathlib-triage Bot added the maintainer-merge A reviewer has approved the changed; awaiting maintainer approval. label Nov 20, 2026

@Vierkantor Vierkantor left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is very nice to have decision procedures for this! Unfortunately I find this PR rather hard to read... A thorough cleanup and documentation (references!) pass would be good before merging.


It is defined by structural recursion on `fuel`, so that it can be evaluated by `decide`. See
`Nat.korseltAux_eq_true_iff` for the conditions under which it is correct. -/
def korseltAux (n fuel m d : ℕ) (c : Bool) : Bool :=

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this some known algorithm (by Korselt, I assume)? Could you add a citation?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I came up with the algorithm, but it is the obvious one if you think of how to use Korselt's criterion to quickly check carmichaelness

have : p ≠ d + 1 := by rintro rfl; have := hp.eq_two_or_odd; lia
lia

theorem korseltAux_iff {n fuel m d : ℕ} {c : Bool} (hd : 3 ≤ d) (hdo : d % 2 = 1)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a hard time trying to read this theorem statement, a docstring could help here (but even better would be explaining what korseltAux is trying to do).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this now?

simp +contextual [← Nat.dvd_iff_mod_eq_zero, hmp, hmp.prime, prime_dvd_prime_iff_eq,
forall_comm]
-- when `fuel = 0`, the remaining goal contradicts `hfuel`
all_goals try exact absurd hfuel (by lia [d.le_mul_self])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why all_goals try can't be replaced with any_goals but it does show how the proof state is very hard to control here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because at this point we are both in the zero and succ branches of the induction on fuel, and exact absurd hfuel (by lia [d.le_mul_self]) fails on all goals in the succ branch. I could make the branches disjoint by duplicating the 10 lines that come before, but the current situation felt better.

@Vierkantor Vierkantor added the awaiting-author Reply -awaiting-author to remove the label on your PR once you have addressed all comments. label Nov 20, 2026
@YaelDillies YaelDillies removed the awaiting-author Reply -awaiting-author to remove the label on your PR once you have addressed all comments. label Nov 21, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

LLM-generated PRs with substantial input from LLMs - review accordingly maintainer-merge A reviewer has approved the changed; awaiting maintainer approval. t-meta Tactics, attributes or user commands t-number-theory Number theory (also use t-algebra or t-analysis to specialize)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants