Guest User

Untitled

a guest
Oct 17th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. use std::option::Option;
  2.  
  3. // Returns Some(nCk) if nCk <= num, None, otherwise
  4. fn exceeds(n: usize, k: usize, num: usize) -> Option<usize> {
  5. if n <= k || k == 0 {
  6. return Some(1);
  7. }
  8.  
  9. let sub = match exceeds(n-1,k-1, num) {
  10. None => return None,
  11. Some(x) if x > num => return None,
  12. Some(x) => x,
  13. };
  14.  
  15. match exceeds(n-1,k, num) {
  16. None => None,
  17. Some(x) if x + sub > num => None,
  18. Some(x) => Some(sub + x),
  19. }
  20. }
  21.  
  22. fn main() {
  23. let mut sum = 0;
  24. for n in 1..(100+1) {
  25. let mut combinations_under =
  26. (0..n).map(|k| exceeds(n, k, 1_000_000))
  27. .take_while(|x| x.is_some())
  28. .count();
  29.  
  30. if combinations_under < n {
  31. // We skipped the last half of combinations with the `.take_while`
  32. combinations_under *= 2;
  33. }
  34.  
  35. println!("{} has {} combinations under 1,000,000", n, combinations_under);
  36. sum += combinations_under;
  37. }
  38. println!("Total combinations: {}", sum);
  39. }
Add Comment
Please, Sign In to add comment