Skip to content

Commit a4dae8c

Browse files
committed
added sieve of eratosthenes
1 parent 0f9f017 commit a4dae8c

10 files changed

+42
-0
lines changed

Diff for: #random-integers-common-factor.ml#

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
(*
2+
What is the probability of four random integers having a common factor?
3+
*)
4+
5+
(* implementation of the Sieve of Eratosthenese
6+
we use 0 to denote struck-out numbers.
7+
could be made more efficient by choosing to strike out multiples
8+
above p^2 rather than p
9+
*)
10+
11+
let rec strike_nth k n l = match l with
12+
| [] -> []
13+
| h :: t ->
14+
if k = 0 then 0 :: strike_nth (n-1) n t
15+
else h :: strike_nth (k-1) n t;;
16+
17+
let primes n =
18+
let limit = truncate(sqrt(float n)) in
19+
let rec range a b = if a > b then [] else a :: range (a+1) b in
20+
let rec sieve_primes l = match l with
21+
| [] -> []
22+
| 0 :: t -> sieve_primes t
23+
| h :: t -> if h > limit then List.filter ((<) 0) l else
24+
h :: sieve_primes (strike_nth (h-1) h t) in
25+
sieve_primes (range 2 n);;
26+

Diff for: .#random-integers-common-factor.ml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Sana@MacBook-Pro-6.local.9792

Diff for: .DS_Store

0 Bytes
Binary file not shown.

Diff for: papers/Applied-Cryptography-Schneier.pdf

8.44 MB
Binary file not shown.

Diff for: papers/btc_scalability_full.pdf

261 KB
Binary file not shown.

Diff for: papers/measure-theory-for-dummies.pdf

136 KB
Binary file not shown.

Diff for: papers/personal-data=problems.pdf

135 KB
Binary file not shown.

Diff for: papers/why-teach-probability-to-kids.pdf

28.6 KB
Binary file not shown.

Diff for: random-integers-common-factor.ml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
(*
2+
What is the probability of four random integers having a common factor?
3+
*)
4+
5+
(* implementation of the Sieve of Eratosthenese
6+
we use 0 to denote struck-out numbers.
7+
could be made more efficient by choosing to strike out multiples
8+
above p^2 rather than p
9+
*)
10+
11+
let rec strike_nth k n l = match l with
12+
| [] -> []
13+
| h :: t ->
14+
if k = 0 then 0 :: strike_nth (n-1) n t
15+
else h :: strike_nth (k-1) n t;;

Diff for: random-integers-common-factor.ml~

Whitespace-only changes.

0 commit comments

Comments
 (0)