Advertisement
Guest User

Untitled

a guest
Mar 5th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 3.67 KB | None | 0 0
  1. open System
  2. open Microsoft.FSharp.Collections
  3. let rec f1 n =
  4.    if n = 1 then 1
  5.    else if n = 2 then 2
  6.    else f1 (n - 1) * f1 (n - 2)
  7. let rec fib n =
  8.     if n = 0 then 0
  9.     else if n = 1 then 1
  10.     else fib(n-2) + fib(n-1)
  11. let f2 (s:string) : char =
  12.    s.ToUpper() |> Seq.countBy id |> Seq.maxBy snd |> fst
  13. let makeSeq (b:float) : seq<float> =
  14.     (b, 1.) |> Seq.unfold (fun (x, y) -> Some (x, (x-1./sqrt(y), y+1.)))
  15. let f3(k:int, lst: seq<float>) : List<float> =
  16.     lst |> Seq.filter(fun (x) -> x < 0.) |> Seq.take(k) |> Seq.toList
  17. let f4 =
  18.     seq { 1 .. 1000 } |> Seq.filter(fun (x) -> x % 3 = 0 && x % 5 = 0) |> Seq.toList
  19. let rec gcd a b =
  20.  match b with
  21.  | _ when b = 0 -> a
  22.  | _ -> gcd b (a % b)
  23. let lcm a b =
  24.     a * b / gcd a b
  25. let f5 =
  26.     Seq.reduce (lcm) [1..20]
  27. let ispalindrom (s:string):bool =
  28.   s  = (s  |>  (fun  x -> new string(x.ToCharArray() |> Array.rev)))
  29. let f6 =
  30.     seq { 1 .. 1000 } |> Seq.filter(fun (x) -> ispalindrom(x.ToString()) && ispalindrom(Convert.ToString(x, 2))) |> Seq.fold (+) 0
  31. let generate n =
  32.     let m = 1 <<< n
  33.     { 0 .. m-1 } |> Seq.map(fun(x) ->
  34.         let s_2 = Convert.ToString(x, 2)
  35.         let s = String.replicate(n-s_2.Length) "0"
  36.         s+s_2 |> Seq.map(fun(x) -> x='1') |> Seq.toList) |> Seq.toList
  37. let rec istrue (lst : List<bool>) : bool =
  38.     match lst with
  39.      | _ when lst.Tail.Tail <> []  -> istrue(lst.Tail) && (not(lst.Head && lst.Tail.Head) || (lst.Tail.Tail.Head))
  40.      | _ when lst.Tail.Tail = [] -> true
  41. let f10 (lst : List<List<bool>>) : int =
  42.     let rec loop lst acc =
  43.         match lst with
  44.         | head::tail -> loop tail acc + Convert.ToInt32(istrue(head));
  45.         | [] -> acc
  46.     loop lst 0
  47. //y_n = y_(n-1) + fib(n+1)
  48. let rec f10_fib (n : int) : int =
  49.     match n with
  50.     | 2 -> 4
  51.     | n -> f10_fib(n-1) + fib(n+1)
  52. let rec extractDigits acc input =
  53.     if input <= 9 then input::acc  
  54.     else extractDigits (input % 10::acc) (input/10)
  55.  
  56. let sumOfSquares input =
  57.     input
  58.     |> List.fold(fun acc i -> acc + i*i) 0
  59. let sumOfSqr input =
  60.     input
  61.     |> List.fold(fun acc i -> acc + i) 0
  62. let rec sum85 input =
  63.     if input = 85 then true
  64.     else if input = 1 || input = 4 then false
  65.     else
  66.         let newInput =
  67.             input
  68.             |> extractDigits List.empty
  69.             |> sumOfSquares
  70.         sum85 newInput
  71. let makeArithmeticSeq a_1 d =
  72.     a_1 |> Seq.unfold (fun x -> Some (x, x+d))
  73. let get_a_n (sq:seq<int>, n:int)
  74.     a_n = Seq.take(1)
  75. let f7 (a_1:int, d:int, n:int) : int =
  76.     a_1 |> Seq.unfold (fun x -> Some (x, x+d)) |> Seq.take(n) |> Seq.sum
  77. let rec f8 input =
  78.     if input >= 0 && input < 10 then input
  79.     else let newInput =
  80.             input
  81.             |> extractDigits List.empty
  82.             |> sumOfSqr
  83.          f8 newInput
  84. let f9 =
  85.     seq {10..99} |> Seq.filter(fun (x) -> sum85 (x)) |> Seq.min
  86. [<EntryPoint>]
  87. let main argv =
  88.     Console.WriteLine(f1(6))
  89.     let list3 = f3(3, makeSeq(4.))
  90.     printf "%A" list3
  91.     Console.WriteLine()
  92.     Console.WriteLine(f2("AAAAABbbbbbCC"))
  93.     let list4 = f4
  94.     printf "%A" list4
  95.     Console.WriteLine()
  96.     Console.WriteLine(f5)
  97.     Console.WriteLine(f6)
  98.     let list7 = generate 3
  99.     printf "%A" list7
  100.     Console.WriteLine()
  101.     let n = 13
  102.     Console.WriteLine(f10_fib(n))
  103.     Console.WriteLine(f10(generate n))
  104.     (*
  105.     [10..99]
  106.     |> List.filter(fun i -> sum85 i)
  107.     |> List.iter(fun i -> printfn "%d" i)
  108.     *)
  109.     (*
  110.     let ap = 2 |> Seq.unfold (fun x -> Some (x, x+3)) |> Seq.take(10) |> Seq.toList
  111.     printf "%A" ap
  112.     *)
  113.     Console.WriteLine(f7 (2, 3, 5))
  114.     Console.WriteLine(f8 65536)
  115.     Console.WriteLine(f9)
  116.     Console.ReadKey()
  117.     0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement