This file contains 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
import qualified Data.Vector.Unboxed as V | |
import Control.Arrow ((***), (&&&)) | |
(&) = flip ($) | |
infixl 0 & | |
shiftedZipWith f vec = | |
enumFromTo 0 (len-1) | |
& map index &&& map (index . (`rem` len) . (+1)) |
This file contains 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
extern crate num; //В Rust нет нативной длинной арифметики | |
#[derive(Debug, PartialEq)] | |
enum POTError<'a> { | |
NoCoincide(&'a str), | |
EmptyString, | |
PowerIsTooLarge, | |
} | |
type BoundType = u32; //Если впоследствие верхняя граница поменяется, |
This file contains 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
type BaseCoord = i32; | |
type DeltaCoord = u32; | |
type InputPoint = (BaseCoord, BaseCoord); | |
//gcd - greatest common divisor | |
//Считает НОД аргументов по алгоритму Евклида | |
//В случае, если один из аргументов равен нулю, | |
//возвращает второй | |
fn gcd(a: DeltaCoord, b: DeltaCoord) -> DeltaCoord { | |
if a < b { gcd(b, a) } |
This file contains 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
type Seq = String | |
type Solution = [Seq] | |
seed :: Solution | |
seed = ["44", "47", "74", "77"] | |
extendSeq :: Seq -> [Seq] | |
extendSeq base@(x:y:ys) = | |
case (x, y) of | |
('4', '4') -> ['7':base] |
This file contains 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
fibs = 1 : 1 : zipWith (+) fibs (tail fibs) | |
fibs2 = map (*2) fibs | |
solution n = fibs2 !! n | |
main = print $ solution 8 | |
-- > 68 |
This file contains 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
//Постоянный пересчёт чисел Фибоначчи слишком неэффективен, | |
//поэтому заведём отдельный буфер для хранения уже вычисленных | |
//чисел, а чтобы не испортить случайно значения, заведём для | |
//буфера отдельный модуль | |
mod fib { | |
//Тип числа, в котором хранятся вычисленные значения. | |
//Выбирается исходя из критерия F(INDEX_BOUND+1) <= Base::max_value(), | |
//где F(n) - n-ое число Фибоначчи | |
pub type Base = u32; | |
pub struct FibAccum { //Буфер вычисленных значений. |
This file contains 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
import Data.Word (Word64) | |
power5 = iterate (*5) 5 | |
factorialZeroes n = sum . takeWhile (/=0) . zipWith (flip div) power5' . repeat $ n | |
where | |
takeWhilePlusOne cond x = | |
let (first, s:econd) = span cond x | |
in first ++ [s] | |
power5' = takeWhilePlusOne (<= (n `div` 5)) power5 |
This file contains 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
type Coord = i16; | |
type Point = (Coord, Coord); //Тип входных данных | |
type VectorCoord = i32; | |
struct Vector2(VectorCoord, VectorCoord); //Тип вектора на плоскости | |
impl Vector2 { | |
//Конструирует вектор от точки from до точки to | |
fn from_points(from: &Point, to: &Point) -> Self { | |
Self{ |
This file contains 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
--Считает сумму первых n членов арифметической прогрессии | |
--с разностью d и первым членом n0 | |
ariphmSum d n0 n = ((2*n0 + d*(n-1))*n) `div` 2 | |
treeWater = (+top) . ariphmSum tierDiff initLeaves | |
where | |
top = 1 | |
tierDiff = 2 | |
initLeaves = 2 |
This file contains 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
type Base = u64; | |
//Тип возможной ошибки при вычислении степени | |
#[derive(Debug, PartialEq)] | |
enum ModPowErr { | |
ZeroModulus, //Деление на ноль не определено | |
ZeroPowerOfZero //Выражение 0^0 не определено | |
} | |
//Считает x ≡ init^power (mod modulus). |