euler001.mlw 2.29 KB
 MARCHE Claude committed Apr 10, 2011 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ``````(* Euler Project, problem 1 If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000. *) theory SumMultiple use import int.Int use import int.EuclideanDivision `````` Jean-Christophe Filliâtre committed May 20, 2011 16 `````` (* [sum_multiple_3_5_lt n] is the sum of all the multiples `````` MARCHE Claude committed Apr 10, 2011 17 `````` of 3 or 5 below n] *) `````` Andrei Paskevich committed Jun 29, 2011 18 `````` function sum_multiple_3_5_lt int : int `````` MARCHE Claude committed Apr 10, 2011 19 20 21 22 `````` axiom SumEmpty: sum_multiple_3_5_lt 0 = 0 axiom SumNo : forall n:int. n >= 0 -> `````` Andrei Paskevich committed Jun 29, 2011 23 `````` mod n 3 <> 0 /\ mod n 5 <> 0 -> `````` MARCHE Claude committed Apr 10, 2011 24 25 26 `````` sum_multiple_3_5_lt (n+1) = sum_multiple_3_5_lt n axiom SumYes: forall n:int. n >= 0 -> `````` Andrei Paskevich committed Jun 29, 2011 27 `````` mod n 3 = 0 \/ mod n 5 = 0 -> `````` MARCHE Claude committed Apr 10, 2011 28 29 30 `````` sum_multiple_3_5_lt (n+1) = sum_multiple_3_5_lt n + n lemma div_minus1_1 : `````` Andrei Paskevich committed Jun 29, 2011 31 `````` forall x y:int. x >= 0 /\ y > 0 -> `````` MARCHE Claude committed Apr 10, 2011 32 33 34 `````` mod (x+1) y = 0 -> div (x+1) y = (div x y) + 1 lemma div_minus1_2 : `````` Andrei Paskevich committed Jun 29, 2011 35 `````` forall x y:int. x >= 0 /\ y > 0 -> `````` Jean-Christophe Filliâtre committed May 20, 2011 36 `````` mod (x+1) y <> 0 -> div (x+1) y = (div x y) `````` MARCHE Claude committed Apr 10, 2011 37 `````` `````` Andrei Paskevich committed Jun 29, 2011 38 `````` function closed_formula (n:int) : int = `````` MARCHE Claude committed Apr 10, 2011 39 40 41 `````` let n3 = div n 3 in let n5 = div n 5 in let n15 = div n 15 in `````` Jean-Christophe Filliâtre committed May 20, 2011 42 43 44 `````` div (3 * n3 * (n3+1) + 5 * n5 * (n5+1) - 15 * n15 * (n15+1)) 2 `````` MARCHE Claude committed Apr 10, 2011 45 `````` `````` MARCHE Claude committed Sep 06, 2011 46 ``````(* `````` MARCHE Claude committed Apr 10, 2011 47 48 `````` lemma mod_15 : forall n:int. n >= 0 -> `````` Andrei Paskevich committed Jun 29, 2011 49 `````` mod n 15 = 0 <-> (mod n 3 = 0 /\ mod n 5 = 0) `````` MARCHE Claude committed Sep 06, 2011 50 ``````*) `````` MARCHE Claude committed Apr 10, 2011 51 `````` `````` Andrei Paskevich committed Jun 29, 2011 52 `````` predicate p (n:int) = sum_multiple_3_5_lt (n+1) = closed_formula n `````` MARCHE Claude committed Apr 10, 2011 53 54 55 `````` lemma Closed_formula_0: p 0 `````` MARCHE Claude committed Sep 06, 2011 56 `````` lemma Closed_formula_n: `````` Jean-Christophe Filliâtre committed May 20, 2011 57 `````` forall n:int. n > 0 -> p (n-1) -> `````` Andrei Paskevich committed Jun 29, 2011 58 `````` mod n 3 <> 0 /\ mod n 5 <> 0 -> p n `````` MARCHE Claude committed Apr 10, 2011 59 `````` `````` MARCHE Claude committed Sep 06, 2011 60 61 62 63 64 65 66 67 68 `````` lemma Closed_formula_n_3: forall n:int. n > 0 -> p (n-1) -> mod n 3 = 0 /\ mod n 5 <> 0 -> p n lemma Closed_formula_n_5: forall n:int. n > 0 -> p (n-1) -> mod n 3 <> 0 /\ mod n 5 = 0 -> p n lemma Closed_formula_n_15: `````` MARCHE Claude committed Apr 10, 2011 69 `````` forall n:int. n > 0 -> p (n-1) -> `````` MARCHE Claude committed Sep 06, 2011 70 `````` mod n 3 = 0 /\ mod n 5 = 0 -> p n `````` MARCHE Claude committed Apr 10, 2011 71 `````` `````` Andrei Paskevich committed Jun 29, 2011 72 `````` clone int.Induction as I with predicate p = p `````` MARCHE Claude committed Apr 10, 2011 73 `````` `````` Jean-Christophe Filliâtre committed May 20, 2011 74 `````` lemma Closed_formula: `````` MARCHE Claude committed Apr 10, 2011 75 76 77 78 79 80 81 82 83 84 `````` forall n:int. 0 <= n -> p n end module Euler001 use import SumMultiple use import int.Int use import int.EuclideanDivision `````` Jean-Christophe Filliâtre committed May 20, 2011 85 `````` let solve n = `````` MARCHE Claude committed Apr 10, 2011 86 87 88 89 90 91 92 `````` { n >= 1 } let n3 = div (n-1) 3 in let n5 = div (n-1) 5 in let n15 = div (n-1) 15 in div (3 * n3 * (n3+1) + 5 * n5 * (n5+1) - 15 * n15 * (n15+1)) 2 { result = sum_multiple_3_5_lt n } `````` Jean-Christophe Filliâtre committed May 20, 2011 93 94 95 96 97 98 99 100 ``````end (* Local Variables: compile-command: "unset LANG; make -C ../.. examples/programs/euler001.gui" End: *) ``````