Last active
September 26, 2018 11:31
-
-
Save mAlishera/f59c99cf077af6126a821619c670e1cd to your computer and use it in GitHub Desktop.
Информатика д/з
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
факториал Ruby | |
2.3.1 :001 > def factorial n | |
2.3.1 :002?> n > 1 ? n * factorial(n - 1) : 1 | |
2.3.1 :003?> end | |
2.3.1 :004 > factorial(3) | |
=> 6 | |
Фибоначчи Ruby one line | |
2.5.1 :025 > print (1...10).inject( [0, 1] ) { | fib | fib << fib.last(2).inject(:+) } | |
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55] => nil | |
2.5.1 :026 > | |
факториал Elixir | |
iex(1)> defmodule Factorial do | |
...(1)> def factorial(0), do: 1 | |
...(1)> def factorial(n) when n > 0 do | |
...(1)> n * factorial(n - 1) | |
...(1)> end | |
...(1)> end | |
iex(2)> Factorial.factorial(4) | |
24 | |
iex(3)> Factorial.factorial(5) | |
120 | |
квадратное уравнение Elixir | |
iex(6)> defmodule SqEv do | |
...(6)> def sq_ev(a, b, c) do | |
...(6)> solve(descriminant(a, b, c), a, b) | |
...(6)> end | |
...(6)> | |
...(6)> defp solve(0, a, b), do: %{x1: -b/2*a, x2: -b/2*a} | |
...(6)> defp solve(descr, a, b) when descr > 0 do | |
...(6)> %{x1: (-b - :math.sqrt(descr))/2*a, x2: (-b + :math.sqrt(descr))/2*a} | |
...(6)> end | |
...(6)> defp solve(_descr, _a, _b), do: %{x1: nil, x2: nil} | |
...(6)> | |
...(6)> defp descriminant(a, b, c) do | |
...(6)> b*b - 4*a*c | |
...(6)> end | |
...(6)> end | |
iex(18)> SqEv.sq_ev(2, -3, 4) | |
%{x1: nil, x2: nil} | |
iex(22)> SqEv.sq_ev(9, 6, 1) | |
%{x1: -27.0, x2: -27.0} | |
числа Фибоначчи Elixir | |
iex(29)> defmodule Fib do | |
...(29)> def fib(n) when n == 1, do: 1 | |
...(29)> def fib(n) when n == 2, do: 1 | |
...(29)> def fib(n) do | |
...(29)> fib(n-1) + fib(n-2) | |
...(29)> end | |
...(29)> end | |
iex(32)> Fib.fib(6) | |
8 | |
iex(33)> Fib.fib(7) | |
13 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment