Last active
August 29, 2015 14:10
-
-
Save akanehara/0e81a57b6eced7d04c23 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| (* 指定されたリストから指定された数の倍数をとりのぞく *) | |
| (* ignore_multiple : int -> int list -> int list *) | |
| let rec ignore_multiple n ns = | |
| match ns with | |
| [] -> [] | |
| | m :: ms -> if m mod n = 0 | |
| then ignore_multiple n ms | |
| else m :: ignore_multiple n ms | |
| (* range : int -> int list *) | |
| let range n m = | |
| let rec go i = | |
| if i = m then i :: [] | |
| else i :: go (i + 1) | |
| in go n | |
| (* エラトステネスの篩 *) | |
| (* sieve : int -> int list *) | |
| let sieve n = | |
| let rec go primes ns = | |
| match ns with | |
| [] -> primes | |
| | m :: ms -> go (primes @ [m]) (ignore_multiple m ms) | |
| in go [] (range 2 n) | |
| let test1 = sieve 10 = [2; 3; 5; 7] | |
| let test2 = sieve 100 = [2; 3; 5; 7; 11; 13; 17; 19; 23; 29; 31; 37; 41; 43; 47; 53; 59; 61; 67; 71; 73; 79; 83; 89; 97] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment