Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
9
Task/Fibonacci-sequence/F-Sharp/fibonacci-sequence-1.fs
Normal file
9
Task/Fibonacci-sequence/F-Sharp/fibonacci-sequence-1.fs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
let fibonacci n : bigint =
|
||||
let rec f a b n =
|
||||
match n with
|
||||
| 0 -> a
|
||||
| 1 -> b
|
||||
| n -> (f b (a + b) (n - 1))
|
||||
f (bigint 0) (bigint 1) n
|
||||
> fibonacci 100;;
|
||||
val it : bigint = 354224848179261915075I
|
||||
2
Task/Fibonacci-sequence/F-Sharp/fibonacci-sequence-2.fs
Normal file
2
Task/Fibonacci-sequence/F-Sharp/fibonacci-sequence-2.fs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
let rec fib = seq { yield! [0;1];
|
||||
for (a,b) in Seq.zip fib (Seq.skip 1 fib) -> a+b}
|
||||
2
Task/Fibonacci-sequence/F-Sharp/fibonacci-sequence-3.fs
Normal file
2
Task/Fibonacci-sequence/F-Sharp/fibonacci-sequence-3.fs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
let fibonacci = Seq.unfold (fun (x, y) -> Some(x, (y, x + y))) (0I,1I)
|
||||
fibonacci |> Seq.nth 10000
|
||||
43
Task/Fibonacci-sequence/F-Sharp/fibonacci-sequence-4.fs
Normal file
43
Task/Fibonacci-sequence/F-Sharp/fibonacci-sequence-4.fs
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
open System
|
||||
open System.Diagnostics
|
||||
open System.Numerics
|
||||
|
||||
/// Finds the highest power of two which is less than or equal to a given input.
|
||||
let inline prevPowTwo (x : int) =
|
||||
let mutable n = x
|
||||
n <- n - 1
|
||||
n <- n ||| (n >>> 1)
|
||||
n <- n ||| (n >>> 2)
|
||||
n <- n ||| (n >>> 4)
|
||||
n <- n ||| (n >>> 8)
|
||||
n <- n ||| (n >>> 16)
|
||||
n <- n + 1
|
||||
match x with
|
||||
| x when x = n -> x
|
||||
| _ -> n/2
|
||||
|
||||
/// Evaluates the nth Fibonacci number using matrix arithmetic and
|
||||
/// exponentiation by squaring.
|
||||
let crazyFib (n : int) =
|
||||
let powTwo = prevPowTwo n
|
||||
|
||||
/// Applies 2n rule repeatedly until another application of the rule would
|
||||
/// go over the target value (or the target value has been reached).
|
||||
let rec iter1 i q r s =
|
||||
match i with
|
||||
| i when i < powTwo ->
|
||||
iter1 (i*2) (q*q + r*r) (r * (q+s)) (r*r + s*s)
|
||||
| _ -> i, q, r, s
|
||||
|
||||
/// Applies n+1 rule until the target value is reached.
|
||||
let rec iter2 (i, q, r, s) =
|
||||
match i with
|
||||
| i when i < n ->
|
||||
iter2 ((i+1), (q+r), q, r)
|
||||
| _ -> q
|
||||
|
||||
match n with
|
||||
| 0 -> 1I
|
||||
| _ ->
|
||||
iter1 1 1I 1I 0I
|
||||
|> iter2
|
||||
Loading…
Add table
Add a link
Reference in a new issue