Skip to content

Commit 3691aaf

Browse files
akiezunReemMelamed
authored andcommitted
feat(NumberTheory): add almost prime numbers (leanprover-community#39903)
Adds `Nat.IsAlmostPrime k n` for natural numbers with exactly `k` prime factors counted with multiplicity, plus `Nat.IsAtMostAlmostPrime` and `Nat.IsSemiprime`. The definitions reuse the existing arithmetic function `Ω`, and the initial API proves the basic zero/one cases, prime examples, and closure under multiplication.
1 parent 0673cc9 commit 3691aaf

2 files changed

Lines changed: 91 additions & 0 deletions

File tree

Mathlib.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5650,6 +5650,7 @@ public import Mathlib.ModelTheory.Types
56505650
public import Mathlib.ModelTheory.Ultraproducts
56515651
public import Mathlib.NumberTheory.ADEInequality
56525652
public import Mathlib.NumberTheory.AbelSummation
5653+
public import Mathlib.NumberTheory.AlmostPrime
56535654
public import Mathlib.NumberTheory.ArithmeticFunction.Carmichael
56545655
public import Mathlib.NumberTheory.ArithmeticFunction.Defs
56555656
public import Mathlib.NumberTheory.ArithmeticFunction.LFunction
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/-
2+
Copyright (c) 2026 Adam Kiezun. All rights reserved.
3+
Released under Apache 2.0 license as described in the file LICENSE.
4+
Authors: Adam Kiezun
5+
-/
6+
module
7+
8+
public import Mathlib.NumberTheory.ArithmeticFunction.Misc
9+
10+
/-!
11+
# Almost prime numbers
12+
13+
This file defines `Nat.IsAlmostPrime k n`, the predicate that `n` has exactly `k`
14+
prime factors counted with multiplicity. We also define `Nat.IsAtMostAlmostPrime`,
15+
the corresponding predicate with at most `k` prime factors, and `Nat.IsSemiprime`,
16+
the special case of `2`-almost-prime numbers.
17+
18+
Both definitions use the arithmetic function `ArithmeticFunction.cardFactors`, written `Ω`.
19+
20+
The terminology follows the standard definition of an
21+
[almost prime](https://en.wikipedia.org/wiki/Almost_prime).
22+
23+
## Main statements
24+
25+
* `Nat.IsAlmostPrime.mul`: the product of a `k`-almost-prime number and an
26+
`l`-almost-prime number is `(k + l)`-almost-prime.
27+
* `Nat.IsAtMostAlmostPrime.mul`: the analogous statement for at most `k` prime factors.
28+
29+
-/
30+
31+
@[expose] public section
32+
33+
open scoped ArithmeticFunction.Omega
34+
35+
namespace Nat
36+
37+
/-- `IsAlmostPrime k n` means that `n` is `k`-almost prime: it has exactly `k`
38+
prime factors, counted with multiplicity. The side condition excludes `0`, so `1` is
39+
`0`-almost prime. -/
40+
def IsAlmostPrime (k n : ℕ) : Prop :=
41+
n ≠ 0 ∧ Ω n = k
42+
43+
/-- `IsAtMostAlmostPrime k n` means that `n` has at most `k` prime factors,
44+
counted with multiplicity. -/
45+
def IsAtMostAlmostPrime (k n : ℕ) : Prop :=
46+
n ≠ 0 ∧ Ω n ≤ k
47+
48+
/-- A semiprime is a `2`-almost-prime number. -/
49+
abbrev IsSemiprime (n : ℕ) : Prop :=
50+
IsAlmostPrime 2 n
51+
52+
variable {k l m n p q : ℕ}
53+
54+
@[simp]
55+
theorem isAlmostPrime_zero_iff : IsAlmostPrime 0 n ↔ n = 1 := by
56+
rw [IsAlmostPrime, ArithmeticFunction.cardFactors_eq_zero_iff_eq_zero_or_one]
57+
exact ⟨fun h ↦ h.2.resolve_left h.1, fun h ↦ by simp [h]⟩
58+
59+
@[simp]
60+
theorem isAlmostPrime_one_iff : IsAlmostPrime 1 n ↔ n.Prime := by
61+
constructor
62+
· exact fun h ↦ ArithmeticFunction.cardFactors_eq_one_iff_prime.mp h.2
63+
· exact fun h ↦ ⟨h.ne_zero, ArithmeticFunction.cardFactors_eq_one_iff_prime.mpr h⟩
64+
65+
theorem Prime.isAlmostPrime_one (hp : p.Prime) : IsAlmostPrime 1 p := by
66+
simpa using isAlmostPrime_one_iff.mpr hp
67+
68+
theorem IsAlmostPrime.mul (hm : IsAlmostPrime k m) (hn : IsAlmostPrime l n) :
69+
IsAlmostPrime (k + l) (m * n) := by
70+
refine ⟨mul_ne_zero hm.1 hn.1, ?_⟩
71+
rw [ArithmeticFunction.cardFactors_mul hm.1 hn.1, hm.2, hn.2]
72+
73+
theorem IsAtMostAlmostPrime.mul (hm : IsAtMostAlmostPrime k m) (hn : IsAtMostAlmostPrime l n) :
74+
IsAtMostAlmostPrime (k + l) (m * n) := by
75+
refine ⟨mul_ne_zero hm.1 hn.1, ?_⟩
76+
rw [ArithmeticFunction.cardFactors_mul hm.1 hn.1]
77+
exact add_le_add hm.2 hn.2
78+
79+
theorem IsAlmostPrime.isAtMost (hn : IsAlmostPrime k n) (hkl : k ≤ l) :
80+
IsAtMostAlmostPrime l n :=
81+
⟨hn.1, hn.2 ▸ hkl⟩
82+
83+
theorem Prime.mul_isAlmostPrime_two (hp : p.Prime) (hq : q.Prime) :
84+
IsAlmostPrime 2 (p * q) := by
85+
simpa using hp.isAlmostPrime_one.mul hq.isAlmostPrime_one
86+
87+
theorem Prime.sq_isAlmostPrime_two (hp : p.Prime) : IsAlmostPrime 2 (p ^ 2) := by
88+
simpa [pow_two] using hp.mul_isAlmostPrime_two hp
89+
90+
end Nat

0 commit comments

Comments
 (0)