%% prime.elf - Prime Numbers and Related Theorems %% %% A prime number P > 1 is a natural number whose only divisors are 1 and P. %% ============================================================ %% DEFINITION OF PRIME %% ============================================================ %% A number is "greater than 1" gt-one : nat -> type. gt-one/intro : gt-one (s (s N)). %% N >= 2 means N > 1 %% A number is composite if it has a non-trivial divisor %% composite N means: exists D such that 1 < D < N and D | N composite : nat -> type. composite/intro : composite N <- gt-one D %% D > 1 <- lt D N %% D < N <- divides D N. %% D | N %% A number is prime if P > 1 and not composite %% We'll define small primes directly and provide a general characterization prime : nat -> type. prime/two : prime (s (s z)). %% 2 is prime prime/three : prime (s (s (s z))). %% 3 is prime prime/five : prime (s (s (s (s (s z))))). %% 5 is prime prime/seven : prime (s (s (s (s (s (s (s z))))))). %% 7 is prime %% ============================================================ %% FACTORIAL %% ============================================================ factorial : nat -> nat -> type. %mode factorial +N -R. factorial/z : factorial z (s z). %% 0! = 1 factorial/s : factorial (s N) R <- factorial N F %% F = N! <- mult (s N) F R. %% R = (N+1) * N! = (N+1)! factorial-total : {N:nat} factorial N F -> type. %mode factorial-total +N -D. factorial-total/z : factorial-total z factorial/z. factorial-total/s : factorial-total (s N) (factorial/s Dmult Dfact) <- factorial-total N Dfact <- mult-total (s N) _ Dmult. %worlds () (factorial-total _ _). %total N (factorial-total N _). %% ============================================================ %% PRIME FACTORIZATION (Representation) %% ============================================================ %% A list of natural numbers nat-list : type. nil : nat-list. cons : nat -> nat-list -> nat-list. %% Product of a list list-product : nat-list -> nat -> type. %mode list-product +L -P. list-product/nil : list-product nil (s z). list-product/cons : list-product (cons N L) P <- list-product L Q <- mult N Q P. %% All elements of a list are prime all-prime : nat-list -> type. all-prime/nil : all-prime nil. all-prime/cons : all-prime (cons P L) <- prime P <- all-prime L. %% A prime factorization of N is a list L where: %% 1. All elements of L are prime %% 2. The product of L equals N prime-factorization : nat -> nat-list -> type. prime-factorization/intro : prime-factorization N L <- all-prime L <- list-product L N. %% ============================================================ %% THEOREMS (Statements) %% ============================================================ %% Theorem: Every N > 1 has a prime divisor %% has-prime-divisor N P means P is prime and P | N has-prime-divisor : nat -> nat -> type. has-prime-divisor/intro : has-prime-divisor N P <- prime P <- divides P N. %% Theorem (Euclid): There are infinitely many primes %% For any N, there exists a prime P > N %% This is expressed as: for any list of primes, there's a prime not in the list %% Theorem (Fundamental Theorem of Arithmetic): %% 1. Existence: Every N > 1 has a prime factorization %% 2. Uniqueness: The factorization is unique up to ordering %% Euclid's Lemma: If P is prime and P | AB, then P | A or P | B %% This is key to proving uniqueness of factorization %% ============================================================ %% COMPUTATIONAL EXAMPLES %% ============================================================ %% These can be verified using %solve: %% 2! = 2 %% 3! = 6 %% 4! = 24 %% 5! = 120 %% Example factorizations: %% 6 = 2 * 3 = cons 2 (cons 3 nil) %% 12 = 2 * 2 * 3 = cons 2 (cons 2 (cons 3 nil))