-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp015.rs
More file actions
58 lines (50 loc) · 1.54 KB
/
p015.rs
File metadata and controls
58 lines (50 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use std::ops::{Div, Mul};
use bigdecimal::{BigDecimal, FromPrimitive};
/// # [Problem 15 「格子経路」](https://odz.sakura.ne.jp/projecteuler/?Problem+15)
/// ## Description
/// ```txt
/// 2×2 のマス目の左上からスタートした場合, 引き返しなしで右下にいくルートは 6 つある.
///
/// では, 20×20 のマス目ではいくつのルートがあるか.
/// ```
///
/// ## Note
/// 組み合わせの公式を使う。 https://rikeilabo.com/formula-and-diferrence-of-permutation-combination
/// - → と ↓ の2種類しか使わない。
/// - →も↓も、それぞれ2個づつしか置けない。
/// - →を2個選ぶと、自動的に↓の2個が決定する。
/// - 4個から2個選ぶ、という組み合わせ。
/// - nCr -> 4C2
///
/// 4 x 3
/// ----- -> 12 / 2 = 6通り
/// 2 x 1
///
/// 40 x 39 x ... 21
/// ----------------
/// 20 x 19 x .... 1
fn problem_015(x: u128, y: u128) -> String {
let n = x + y;
let r = n / 2;
let mut numerator: BigDecimal = BigDecimal::from_u8(1).unwrap();
for i in r + 1..=n {
numerator = numerator.mul(BigDecimal::from_u128(i).unwrap());
}
let mut denominator: BigDecimal = BigDecimal::from_u8(1).unwrap();
for i in 1..=r {
denominator = denominator.mul(BigDecimal::from_u128(i).unwrap());
}
numerator.div(denominator).to_string()
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn problem_015_1_test() {
assert_eq!(problem_015(2, 2), "6");
}
#[test]
fn problem_015_2_test() {
assert_eq!(problem_015(20, 20), "137846528820");
}
}