Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Learn more about F# at http://fsharp.org
- // See the 'F# Tutorial' project for more help.
- module problema1 =
- open System
- let contFraction (arr : int[]) =
- let mutable arrAux = Array.map (fun x -> float x) arr
- let mutable arrAuxRev = Array.rev arrAux
- let mutable sum = 1.0 / arrAuxRev.[0]
- for i = 1 to arrAuxRev.Length-2 do
- sum <- 1.0 / (sum+arrAuxRev.[i])
- sum <- sum + arrAuxRev.[arrAuxRev.Length-1]
- printfn "%f" sum
- let mainP1() =
- contFraction [|3; 7; 15; 1; 292; 1|]
- // ===================================
- module problema1b =
- let contFraction (arr : int[]) =
- let mutable arrAux = Array.map (fun x -> float x) arr
- let mutable arrAuxRev = Array.rev arrAux
- let mutable sum = Array.reduce(fun x y -> y + 1.0 / x) arrAuxRev
- printfn "%f" sum
- let mainP1b() =
- contFraction [|3; 7; 15; 1; 292; 1|]
- // ===================================
- module problema3 =
- let multiplyI (a:int[,]) (b:int[,]) =
- let n = a.GetLength(0)
- let m = a.GetLength(1)
- let p = b.GetLength(0)
- let q = b.GetLength(1)
- if (m <> p) then
- Array2D.zeroCreate 1 1
- else
- let c = Array2D.init n q (fun x y -> 0)
- for i=0 to n-1 do
- for j=0 to p-1 do
- for k=0 to m-1 do
- c.[i,j] <- c.[i,j] + a.[i,k] * b.[k,j]
- c
- let mainP3() =
- let rand = System.Random()
- let a = Array2D.init 2 3 (fun x y -> rand.Next(10))
- let b = Array2D.init 3 4 (fun x y -> rand.Next(10))
- let c = multiplyI a b
- printfn "A = \n%A\n\nB = \n%A\n\nA * B = \n%A" a b c
- let d = multiplyI b a
- printfn "\nB * A = \n%A" d
- // ===================================
- // ===================================
- module problema3b =
- let multiplyF (a:int[,]) (b:int[,]) =
- let n = a.GetLength(0)
- let m = a.GetLength(1)
- let p = b.GetLength(0)
- let q = b.GetLength(1)
- if (m <> p) then
- Array2D.zeroCreate 1 1
- else
- let compute ai bj =
- let row = a.[ai, *]
- let column = b.[*, bj]
- let prod = Array.map2 (fun x y -> x + y) row column
- let p = Array.reduce (fun x y -> x + y) prod
- p
- let c = Array2D.init n q (fun x y -> compute x y)
- c
- let mainP3b() =
- let rand = System.Random()
- let a = Array2D.init 2 3 (fun x y -> rand.Next(10))
- let b = Array2D.init 3 4 (fun x y -> rand.Next(10))
- let c = multiplyF a b
- printfn "A = \n%A\n\nB = \n%A\n\nA * B = \n%A" a b c
- let d = multiplyF b a
- printfn "\nB * A = \n%A" d
- [<EntryPoint>]
- let main argv =
- problema1.mainP1()
- problema1b.mainP1b()
- problema3.mainP3()
- problema3b.mainP3b()
- 0 // return an integer exit code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement