Created
January 31, 2017 20:46
-
-
Save iwillspeak/227d4287ed339ad14b0441116ccd5b67 to your computer and use it in GitHub Desktop.
Steps
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
#![feature(inclusive_range_syntax)] | |
/// Steps At | |
/// | |
/// Returns the number of combinations of steps which can be made | |
/// where the top step is a given size. This is used as the recursive | |
/// step. | |
/// | |
/// # Arguments | |
/// | |
/// * `top_step` - The size of the top step | |
/// * `remaining_bricks` - The number of leftover bricks that can be used. | |
fn steps_at(top_step: usize, remaining_bricks: usize) -> usize { | |
if remaining_bricks > ((2 * top_step) + 2) { | |
// If we have enough to potentially make two layers of bricks | |
((top_step + 1)...(remaining_bricks / 2)) | |
.map(|i| { steps_at(i, remaining_bricks - i)}) | |
.sum::<usize>() + 1 | |
} else if remaining_bricks > top_step { | |
// Just enough for a single layer of bricks | |
1 | |
} else { | |
// Not enough leftover bricks to make a layer | |
0 | |
} | |
} | |
/// Steps | |
/// | |
/// Computes the number of possible steps whic can be created with a | |
/// given number of bricks. | |
/// | |
/// # Arguments | |
/// | |
/// * `i` - The number of bricks available | |
fn steps(i: usize) -> usize { | |
(1...i/2) | |
.map(|j| { steps_at(j, i - j)}) | |
.sum() | |
} | |
fn main() { | |
for i in 1..200 { | |
println!("{}: {}", i, steps(i)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment