-
Notifications
You must be signed in to change notification settings - Fork 0
/
primefactors.fsx
43 lines (35 loc) · 1.14 KB
/
primefactors.fsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
(*
**Prime Factorization** - Have the user enter a number and find all Prime Factors (if there are any) and display them.
*)
let primefactors x =
let limit = uint64((ceil(sqrt(float(x)))));
let check = seq { yield 2UL; yield! { 3UL .. 2UL .. limit} }
let getFirstOrDefault def ns =
if ns |> Seq.isEmpty then
def
else
ns |> Seq.head
let nextfactor x =
match x with
|1UL -> x
|_ -> check
|> Seq.skipWhile(fun idx -> x % idx <> 0UL)
|> getFirstOrDefault x
let rec getfactors x factors =
match nextfactor x with
|1UL -> factors
|factor -> (factors |> List.append [factor]) |> getfactors (x / factor)
[] |> getfactors x
#time
let smallFactors = primefactors 3672215407307975928UL
printfn "smallfactors: %A" smallFactors
#time
#time
let bigPrimeFactor = primefactors 18446744073709551556UL
printfn "bigfactors: %A" bigPrimeFactor
#time
#time
let biggestuint64Prime = primefactors 18446744073709551557UL
printfn "biggestprime: %A" biggestuint64Prime
#time
let ePrime = primefactors 600851475143UL