| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Prime factorisation of natural numbers and its properties |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.Nat.Primality.Factorisation where |
| 10 |
|
| 11 |
open import Data.Nat.Base |
| 12 |
open import Data.Nat.Divisibility |
| 13 |
using (_∣?_; quotient; quotient>1; quotient-<; quotient-∣; m∣n⇒n≡m*quotient; _∣_; ∣1⇒≡1 |
| 14 |
; divides) |
| 15 |
open import Data.Nat.Properties |
| 16 |
open import Data.Nat.Induction using (<-Rec; <-rec; <-recBuilder) |
| 17 |
open import Data.Nat.ListAction using (product) |
| 18 |
open import Data.Nat.ListAction.Properties using (product-↭) |
| 19 |
open import Data.Nat.Primality |
| 20 |
using (Prime; _Rough_; rough∧square>⇒prime; ∤⇒rough-suc; rough∧∣⇒rough; rough∧∣⇒prime |
| 21 |
; 2-rough; euclidsLemma; prime⇒irreducible; ¬prime[1]; productOfPrimes≥1; prime⇒nonZero) |
| 22 |
open import Data.Product.Base using (∃-syntax; _×_; _,_; proj₁; proj₂) |
| 23 |
open import Data.List.Base using (List; []; _∷_; _++_) |
| 24 |
open import Data.List.Membership.Propositional using (_∈_) |
| 25 |
open import Data.List.Membership.Propositional.Properties using (∈-∃++) |
| 26 |
open import Data.List.Relation.Unary.All as All using (All; []; _∷_) |
| 27 |
open import Data.List.Relation.Unary.Any using (here; there) |
| 28 |
open import Data.List.Relation.Binary.Permutation.Propositional |
| 29 |
using (_↭_; ↭-reflexive; ↭-refl; ↭-trans; module PermutationReasoning) |
| 30 |
open import Data.List.Relation.Binary.Permutation.Propositional.Properties |
| 31 |
using (All-resp-↭; shift) |
| 32 |
open import Data.Sum.Base using (inj₁; inj₂) |
| 33 |
open import Function.Base using (_$_; _∘_; _|>_; flip) |
| 34 |
open import Induction using (build) |
| 35 |
open import Induction.Lexicographic using (_⊗_; [_⊗_]) |
| 36 |
open import Relation.Nullary.Decidable using (yes; no) |
| 37 |
open import Relation.Nullary.Negation using (contradiction) |
| 38 |
open import Relation.Binary.PropositionalEquality.Core |
| 39 |
using (_≡_; refl; sym; trans; cong) |
| 40 |
open import Relation.Binary.PropositionalEquality.Properties |
| 41 |
using (module ≡-Reasoning) |
| 42 |
|
| 43 |
private |
| 44 |
variable |
| 45 |
n : ℕ |
| 46 |
|
| 47 |
------------------------------------------------------------------------ |
| 48 |
-- Core definition |
| 49 |
|
| 50 |
record PrimeFactorisation (n : ℕ) : Set where |
| 51 |
field |
| 52 |
factors : List ℕ |
| 53 |
isFactorisation : n ≡ product factors |
| 54 |
factorsPrime : All Prime factors |
| 55 |
|
| 56 |
open PrimeFactorisation public using (factors) |
| 57 |
open PrimeFactorisation |
| 58 |
|
| 59 |
------------------------------------------------------------------------ |
| 60 |
-- Finding a factorisation |
| 61 |
|
| 62 |
primeFactorisation[1] : PrimeFactorisation 1 |
| 63 |
primeFactorisation[1] = record |
| 64 |
{ factors = [] |
| 65 |
; isFactorisation = refl |
| 66 |
; factorsPrime = [] |
| 67 |
} |
| 68 |
|
| 69 |
primeFactorisation[p] : Prime n → PrimeFactorisation n |
| 70 |
primeFactorisation[p] {n} pr = record |
| 71 |
{ factors = n ∷ [] |
| 72 |
; isFactorisation = sym (*-identityʳ n) |
| 73 |
; factorsPrime = pr ∷ [] |
| 74 |
} |
| 75 |
|
| 76 |
-- This builds up three important things: |
| 77 |
-- * a proof that every number we've gotten to so far has increasingly higher |
| 78 |
-- possible least prime factor, so we don't have to repeat smaller factors |
| 79 |
-- over and over (this is the "m" and "rough" parameters) |
| 80 |
-- * a witness that this limit is getting closer to the number of interest, in a |
| 81 |
-- way that helps the termination checker (the "k" and "eq" parameters) |
| 82 |
-- * a proof that we can factorise any smaller number, which is useful when we |
| 83 |
-- encounter a factor, as we can then divide by that factor and continue from |
| 84 |
-- there without termination issues |
| 85 |
factorise : ∀ n → .{{NonZero n}} → PrimeFactorisation n |
| 86 |
factorise 1 = primeFactorisation[1] |
| 87 |
factorise n₀@(2+ _) = build [ <-recBuilder ⊗ <-recBuilder ] P facRec (n₀ , suc n₀ ∸ 4) 2-rough refl |
| 88 |
where |
| 89 |
P : ℕ × ℕ → Set |
| 90 |
P (n , k) = ∀ {m} → .{{NonTrivial n}} → .{{NonTrivial m}} → m Rough n → suc n ∸ m * m ≡ k → PrimeFactorisation n |
| 91 |
|
| 92 |
facRec : ∀ n×k → (<-Rec ⊗ <-Rec) P n×k → P n×k |
| 93 |
facRec (n , zero) _ rough eq = |
| 94 |
-- Case 1: m * m > n, ∴ Prime n |
| 95 |
primeFactorisation[p] (rough∧square>⇒prime rough (m∸n≡0⇒m≤n eq)) |
| 96 |
facRec (n@(2+ _) , suc k) (recFactor , recQuotient) {m@(2+ _)} rough eq with m ∣? n |
| 97 |
-- Case 2: m ∤ n, try larger m, reducing k accordingly |
| 98 |
... | no m∤n = recFactor (≤-<-trans (m∸n≤m k (m + m)) (n<1+n k)) {suc m} (∤⇒rough-suc m∤n rough) $ begin |
| 99 |
suc n ∸ (suc m + m * suc m) ≡⟨ cong (λ # → suc n ∸ (suc m + #)) (*-suc m m) ⟩ |
| 100 |
suc n ∸ (suc m + (m + m * m)) ≡⟨ cong (suc n ∸_) (+-assoc (suc m) m (m * m)) ⟨ |
| 101 |
suc n ∸ (suc (m + m) + m * m) ≡⟨ cong (suc n ∸_) (+-comm (suc (m + m)) (m * m)) ⟩ |
| 102 |
suc n ∸ (m * m + suc (m + m)) ≡⟨ ∸-+-assoc (suc n) (m * m) (suc (m + m)) ⟨ |
| 103 |
(suc n ∸ m * m) ∸ suc (m + m) ≡⟨ cong (_∸ suc (m + m)) eq ⟩ |
| 104 |
suc k ∸ suc (m + m) ∎ |
| 105 |
where open ≡-Reasoning |
| 106 |
-- Case 3: m ∣ n, record m and recurse on the quotient |
| 107 |
... | yes m∣n = record |
| 108 |
{ factors = m ∷ ps |
| 109 |
; isFactorisation = sym m*Πps≡n |
| 110 |
; factorsPrime = rough∧∣⇒prime rough m∣n ∷ primes |
| 111 |
} |
| 112 |
where |
| 113 |
m<n : m < n |
| 114 |
m<n = begin-strict |
| 115 |
m <⟨ s≤s (≤-trans (m≤n+m m _) (+-monoʳ-≤ _ (m≤m+n m _))) ⟩ |
| 116 |
pred (m * m) <⟨ s<s⁻¹ (m∸n≢0⇒n<m λ eq′ → 0≢1+n (trans (sym eq′) eq)) ⟩ |
| 117 |
n ∎ |
| 118 |
where open ≤-Reasoning |
| 119 |
|
| 120 |
q = quotient m∣n |
| 121 |
|
| 122 |
instance _ = n>1⇒nonTrivial (quotient>1 m∣n m<n) |
| 123 |
|
| 124 |
factorisation[q] : PrimeFactorisation q |
| 125 |
factorisation[q] = recQuotient (quotient-< m∣n) (suc q ∸ m * m) (rough∧∣⇒rough rough (quotient-∣ m∣n)) refl |
| 126 |
|
| 127 |
ps = factors factorisation[q] |
| 128 |
|
| 129 |
primes = factorsPrime factorisation[q] |
| 130 |
|
| 131 |
m*Πps≡n : m * product ps ≡ n |
| 132 |
m*Πps≡n = begin |
| 133 |
m * product ps ≡⟨ cong (m *_) (isFactorisation factorisation[q]) ⟨ |
| 134 |
m * q ≡⟨ m∣n⇒n≡m*quotient m∣n ⟨ |
| 135 |
n ∎ |
| 136 |
where open ≡-Reasoning |
| 137 |
|
| 138 |
------------------------------------------------------------------------ |
| 139 |
-- Properties of a factorisation |
| 140 |
|
| 141 |
factorisationHasAllPrimeFactors : ∀ {as} {p} → Prime p → p ∣ product as → All Prime as → p ∈ as |
| 142 |
factorisationHasAllPrimeFactors {[]} {2+ p} pPrime p∣Πas [] = contradiction (∣1⇒≡1 p∣Πas) λ () |
| 143 |
factorisationHasAllPrimeFactors {a ∷ as} {p} pPrime p∣aΠas (aPrime ∷ asPrime) with euclidsLemma a (product as) pPrime p∣aΠas |
| 144 |
... | inj₂ p∣Πas = there (factorisationHasAllPrimeFactors pPrime p∣Πas asPrime) |
| 145 |
... | inj₁ p∣a with prime⇒irreducible aPrime p∣a |
| 146 |
... | inj₁ refl = contradiction pPrime ¬prime[1] |
| 147 |
... | inj₂ refl = here refl |
| 148 |
|
| 149 |
private |
| 150 |
factorisationUnique′ : (as bs : List ℕ) → product as ≡ product bs → All Prime as → All Prime bs → as ↭ bs |
| 151 |
factorisationUnique′ [] [] Πas≡Πbs asPrime bsPrime = ↭-refl |
| 152 |
factorisationUnique′ [] (b@(2+ _) ∷ bs) Πas≡Πbs prime[as] (_ ∷ prime[bs]) = |
| 153 |
contradiction Πas≡Πbs (<⇒≢ Πas<Πbs) |
| 154 |
where |
| 155 |
Πas<Πbs : product [] < product (b ∷ bs) |
| 156 |
Πas<Πbs = begin-strict |
| 157 |
1 ≡⟨⟩ |
| 158 |
1 * 1 <⟨ *-monoˡ-< 1 {1} {b} sz<ss ⟩ |
| 159 |
b * 1 ≤⟨ *-monoʳ-≤ b (productOfPrimes≥1 prime[bs]) ⟩ |
| 160 |
b * product bs ≡⟨⟩ |
| 161 |
product (b ∷ bs) ∎ |
| 162 |
where open ≤-Reasoning |
| 163 |
|
| 164 |
factorisationUnique′ (a ∷ as) bs Πas≡Πbs (prime[a] ∷ prime[as]) prime[bs] = a∷as↭bs |
| 165 |
where |
| 166 |
a∣Πbs : a ∣ product bs |
| 167 |
a∣Πbs = divides (product as) $ begin |
| 168 |
product bs ≡⟨ Πas≡Πbs ⟨ |
| 169 |
product (a ∷ as) ≡⟨⟩ |
| 170 |
a * product as ≡⟨ *-comm a (product as) ⟩ |
| 171 |
product as * a ∎ |
| 172 |
where open ≡-Reasoning |
| 173 |
|
| 174 |
shuffle : ∃[ bs′ ] bs ↭ a ∷ bs′ |
| 175 |
shuffle with ys , zs , p ← ∈-∃++ (factorisationHasAllPrimeFactors prime[a] a∣Πbs prime[bs]) |
| 176 |
= ys ++ zs , ↭-trans (↭-reflexive p) (shift a ys zs) |
| 177 |
|
| 178 |
bs′ = proj₁ shuffle |
| 179 |
bs↭a∷bs′ = proj₂ shuffle |
| 180 |
|
| 181 |
Πas≡Πbs′ : product as ≡ product bs′ |
| 182 |
Πas≡Πbs′ = *-cancelˡ-≡ (product as) (product bs′) a {{prime⇒nonZero prime[a]}} $ begin |
| 183 |
a * product as ≡⟨ Πas≡Πbs ⟩ |
| 184 |
product bs ≡⟨ product-↭ bs↭a∷bs′ ⟩ |
| 185 |
a * product bs′ ∎ |
| 186 |
where open ≡-Reasoning |
| 187 |
|
| 188 |
prime[bs'] : All Prime bs′ |
| 189 |
prime[bs'] = All.tail (All-resp-↭ bs↭a∷bs′ prime[bs]) |
| 190 |
|
| 191 |
a∷as↭bs : a ∷ as ↭ bs |
| 192 |
a∷as↭bs = begin |
| 193 |
a ∷ as <⟨ factorisationUnique′ as bs′ Πas≡Πbs′ prime[as] prime[bs'] ⟩ |
| 194 |
a ∷ bs′ ↭⟨ bs↭a∷bs′ ⟨ |
| 195 |
bs ∎ |
| 196 |
where open PermutationReasoning |
| 197 |
|
| 198 |
factorisationUnique : (f f′ : PrimeFactorisation n) → factors f ↭ factors f′ |
| 199 |
factorisationUnique {n} f f′ = |
| 200 |
factorisationUnique′ (factors f) (factors f′) Πf≡Πf′ (factorsPrime f) (factorsPrime f′) |
| 201 |
where |
| 202 |
Πf≡Πf′ : product (factors f) ≡ product (factors f′) |
| 203 |
Πf≡Πf′ = begin |
| 204 |
product (factors f) ≡⟨ isFactorisation f ⟨ |
| 205 |
n ≡⟨ isFactorisation f′ ⟩ |
| 206 |
product (factors f′) ∎ |
| 207 |
where open ≡-Reasoning |