module prime where open import Data.Nat open import Data.Nat.Properties open import Data.Empty open import Data.Unit using (⊤ ; tt) open import Relation.Nullary open import Relation.Binary.PropositionalEquality open import Relation.Binary.Definitions open import gcd open import nat record Prime (i : ℕ ) : Set where field p>1 : i > 1 isPrime : ( j : ℕ ) → j < i → 0 < j → gcd i j ≡ 1 record NonPrime ( n : ℕ ) : Set where field factor : ℕ prime : Prime factor dividable : Dividable factor n PrimeP : ( n : ℕ ) → Dec ( Prime n ) PrimeP 0 = no (λ p → ⊥-elim ( nat-<> (Prime.p>1 p) (s≤s z≤n))) PrimeP 1 = no (λ p → ⊥-elim ( nat-≤> (Prime.p>1 p) (s≤s (≤-refl)))) PrimeP (suc (suc n)) = isPrime1 (suc (suc n)) (suc n) (s≤s (s≤s z≤n)) a n ¬a ¬b c = ⊥-elim ( nat-≤> c i 1 → m < n → ( (i : ℕ) → m < i → i < n → gcd n i ≡ 1 ) → Dec ( Prime n ) isPrime1 n zero n>1 m1 = n>1 } isPrime1 n (suc m) n>1 m ( gcd>0 n (suc m) (<-trans (s≤s z≤n) n>1) (s≤s z≤n)) a ) ... | tri≈ ¬a b ¬c = isPrime1 n m n>1 (<-trans a ¬a ¬b c = ⊥-elim ( nat-≤> m ¬a ¬b c = no ( λ p → nat-≡< (sym (Prime.isPrime p (suc m) m 0 → ¬ Prime p is-g : (j : ℕ) → g ≤ j → j < p → gcd p j ≡ 1 is-g= : {g p : ℕ} → g ≡ p → (j : ℕ) → g ≤ j → j < p → gcd p j ≡ 1 is-g= eq _ g≤j j

1 p ; div = {!!} ; npr = λ () ; is-g = {!!} } ... | no np = {!!} -- record { p = suc (suc f) ; g = suc (suc f) ; 1

1 = G.1

n≤j j (≤-trans {!!} m ¬a ¬b c = mg j {!!} j1 = 1 ( gcd>0 n (suc m) (<-trans (s≤s z≤n) 1 ¬a ¬b c with PrimeP ( gcd n (suc m) ) ... | yes y = record { factor = gcd n (suc m) ; prime = y ; dividable = proj1 (gcd-divable n (suc m) ) } ... | no ngcd = np1 m (mg1 m mg {!!} ) prime-is-infinite : (max-prime : ℕ ) → ¬ ( (j : ℕ) → max-prime < j → ¬ Prime j ) prime-is-infinite zero pmax = pmax 3 (s≤s z≤n) record { isPrime = λ n lt 01 = s≤s (s≤s z≤n) } where pif3 : (n : ℕ) → n < 3 → 0 < n → gcd 3 n ≡ 1 pif3 .1 (s≤s (s≤s z≤n)) _ = refl pif3 .2 (s≤s (s≤s (s≤s z≤n))) _ = refl prime-is-infinite (suc m) pmax = getPrime where factorial : (n : ℕ) → ℕ factorial zero = 1 factorial (suc n) = (suc n) * (factorial n) prime ¬a ¬b c = ⊥-elim ( pmax n c p ) factorial-mono : (n : ℕ) → factorial n ≤ factorial (suc n) factorial-mono n = begin factorial n ≤⟨ x≤x+y ⟩ factorial n + n * factorial n ≡⟨ refl ⟩ (suc n) * factorial n ≡⟨ refl ⟩ factorial (suc n) ∎ where open ≤-Reasoning factorial≥1 : {m : ℕ} → 1 ≤ factorial m factorial≥1 {zero} = ≤-refl factorial≥1 {suc m} = begin 1 ≤⟨ s≤s z≤n ⟩ (suc m) * 1 ≤⟨ *-monoʳ-≤ (suc m) (factorial≥1 {m}) ⟩ (suc m) * factorial m ≡⟨ refl ⟩ factorial (suc m) ∎ where open ≤-Reasoning mm : suc m < suc (factorial (suc m)) f>m = begin suc (suc m) ≡⟨ cong (λ k → 1 + suc k ) (+-comm _ m) ⟩ suc (1 + 1 * m) ≡⟨ cong (λ k → suc (1 + k )) (*-comm 1 m) ⟩ suc (1 + m * 1) ≤⟨ s≤s (s≤s (*-monoʳ-≤ m (factorial≥1 {m}) )) ⟩ suc (1 + m * factorial m) ≤⟨ s≤s (+-monoˡ-≤ _ (factorial≥1 {m})) ⟩ suc (factorial m + m * factorial m) ≡⟨ refl ⟩ suc (factorial (suc m)) ∎ where open ≤-Reasoning fact< : (m n : ℕ) → 0 < n → n < suc (suc m) → Dividable n ( factorial (suc m) ) fact< zero 1 0 ¬a ¬b c = ⊥-elim ( nat-≤> c n1 p)) ( prime 1 → Dividable k i → ¬ Dividable k (suc i) getPrime : ⊥ getPrime with PrimeP ( suc (factorial (suc m)) ) ... | yes p = pmax _ f>m p ... | no np = div+1 (Prime.p>1 (NonPrime.prime p1)) (fact (NonPrime.factor p1) (NonPrime.prime p1) ) (NonPrime.dividable p1) where p1 : NonPrime ( suc (factorial (suc m)) ) p1 = nonPrime (begin 2 ≤⟨ s≤s ( s≤s z≤n) ⟩ suc (suc m) ≤⟨ s≤s (m