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
#![feature(slice_patterns)] | |
fn same_relation<Elem: Ord>(pivot: Elem, arr: &[Elem]) -> bool { | |
match *arr { | |
[] => true, | |
[ref _single] => true, | |
[ref first, ref rest..] => { | |
let relation = pivot.cmp(first); | |
rest.iter().all(|x| pivot.cmp(x) == relation) | |
}, |
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
#![deny(overflowing_literals)] | |
#[macro_use] | |
//Библиотека для ленивой инициализации. Позволяет удостовериться, | |
//что IS_PRIME инициализируется только один раз | |
extern crate lazy_static; | |
//Верхняя граница на входные данные | |
const BOUND: usize = 1_000; | |
const IS_PRIME_SIZE: usize = BOUND - 2 + 1; |
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
#![deny(overflowing_literals)] | |
#[macro_use] | |
extern crate lazy_static; | |
const BOUND: usize = 1_000; | |
const IS_PRIME_SIZE: usize = BOUND - 2 + 1; | |
lazy_static! { | |
static ref IS_PRIME: [bool; IS_PRIME_SIZE] = make_is_prime(); |
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
#![feature(range_contains)] | |
//^Строго говоря, необязательно, но проверку на принадлежность диапозону | |
//удобнее всего делать с этой фичей | |
type Coeff = i64; | |
//Расширенный алгоритм Евклида (рекурсивный вариант) | |
fn gcd_extended_recursive(a: Coeff, b: Coeff) -> (Coeff, Coeff, Coeff) { | |
if b == 0 { | |
(a, 1, 0) |
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
#![deny(overflowing_literals)] | |
type Number = u32; | |
//Верхняя граница на входные данные | |
const BOUND: Number = 1_000_000_000; | |
//Вычисляет число делителей квадрата аргумента | |
fn number_of_divisors_of_square(mut n: Number) -> Number { | |
let mut acc = 1; | |
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
//Превратить переполнение числовых литералов в ошибку компиляции | |
#![deny(overflowing_literals)] | |
//Тип для подсчёта значений | |
type Count = u32; | |
//Возвращает факториал аргумента | |
fn factorial(n: Count) -> Count { | |
(1..=n).product() | |
} |
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
solve k = | |
if k == 0 | |
then 0 | |
else extract . dropWhile (notSameOddity k') . dropWhile (notIncludes k') . enhance $ [0..] | |
where | |
k' = abs k | |
sums = scanl (+) 0 [1..] | |
enhance = zip3 (cycle [0, 1, 1, 0]) sums | |
first (x, _, _) = x | |
second (_, x, _) = x |
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
//Тип стороны прямоугольника | |
type RectSide = f64; | |
//Верхняя граница на стороны прямоугольника | |
const SIDE_BOUND: RectSide = 1_000_000_000.0; | |
#[derive(Debug, PartialEq)] | |
enum MaxSquareSideError { //Тип ошибки при решении задачи: | |
NonPositive(RectSide), //сторона неположительна, | |
OutOfBound(RectSide), //сторона слишком большая, |
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
-- Решение напрямую | |
data Variant = Chosen | Skipped | |
deriving (Eq) | |
type Row = [Variant] | |
isValid :: Row -> Bool | |
isValid [] = True | |
isValid (x:[]) = True | |
isValid (x:y:ys) = ((x, y) /= (Chosen, Chosen)) && (isValid (y:ys)) |
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
extern crate num_bigint; | |
extern crate num_traits; | |
use num_bigint::BigUint; | |
use num_traits::{Zero, One}; | |
type Length = u32; | |
#[deny(overflowing_literals)] | |
const BORDER: Length = 1_001; |