-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from lean-ja/feature/problem41and46
solve 41 and 46
- Loading branch information
Showing
4 changed files
with
89 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/- # Problem 40 | ||
(Intermediate 🌟🌟) A list of even numbers and their Goldbach compositions in a given range. | ||
-/ | ||
|
||
def Nat.isPrime (n : Nat) : Bool := Id.run do | ||
if n ≤ 2 then | ||
return false | ||
for d in [2:n] do | ||
if n % d = 0 then | ||
return false | ||
if d ^ 2 > n then | ||
break | ||
return true | ||
|
||
def goldbach (n : Nat) : Nat × Nat := Id.run do | ||
for cand in [2:n] do | ||
if not cand.isPrime then | ||
continue | ||
let rest := n - cand | ||
if not rest.isPrime then | ||
continue | ||
return (cand, rest) | ||
|
||
panic! "we've found a counter example of goldbach conjecture!! 🎉" | ||
|
||
def goldbachList (lower upper : Nat) (primeLower : Nat := 2) : List (Nat × Nat) := | ||
-- sorry | ||
List.range (upper + 1) | ||
|>.filter (· ≥ lower) | ||
|>.filter (· % 2 == 0) | ||
|>.map (fun n => (goldbach n)) | ||
|>.filter (fun t => t.fst > primeLower) | ||
-- sorry | ||
|
||
-- The following codes are for test and you should not edit these. | ||
|
||
#guard goldbachList 9 20 == [(3, 7), (5, 7), (3, 11), (3, 13), (5, 13), (3, 17)] | ||
|
||
#guard goldbachList 9 20 3 == [(5, 7), (5, 13)] | ||
|
||
#guard goldbachList 4 2000 50 == [(73,919),(61,1321),(67,1789),(61,1867)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/- # Problem 46 | ||
(Intermediate 🌟🌟) Truth tables for logical expressions. | ||
-/ | ||
|
||
|
||
def table (p : Bool → Bool → Bool) : List (List Bool) := | ||
-- sorry | ||
[ | ||
[true, true, p true true], | ||
[true, false, p true false], | ||
[false, true, p false true], | ||
[false, false, p false false] | ||
] | ||
-- sorry | ||
|
||
-- The following codes are for test and you should not edit these. | ||
|
||
#guard table (fun a b => And a (Or a b)) == | ||
[ | ||
[true, true, true], | ||
[true, false, true], | ||
[false, true, false], | ||
[false, false, false] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters