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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#![feature(libc)]

pub mod rust {
  pub fn arrayify(i: i32) -> Vec<i32> {
     (1..(i+1)).collect::<Vec<i32>>()
  }

  pub fn factorial(i: Vec<i32>) -> i32 {
    let x = i.iter().fold(1, |a, b| a * b);
    match x {
      0 => 1,
      _ => x
    }
  }

  pub fn permutations(n: i32, k: i32) -> i32 {
    if k > n { panic!("Aaah! First parameter must be the larger number!") };
    let m = arrayify(n);
    factorial(m[arrayify(n-k).len()..m.len()].to_vec())
  }

  pub fn combinatorial_count(n: i32, k: i32) -> i32 {
    if k > n { panic!("Aaah! First parameter must be the larger number!") };
    permutations(n,k) / factorial(arrayify(k))
  }
}

//  #![feature(cstr_memory)]
extern crate libc;
//
//  use std::ffi::CString;
//  use std::mem;
//
//  #[repr(C)]
//  pub struct RubyArray {
//    len: libc::size_t,
//    data: *const libc::c_void,
//  }
//
//  impl RubyArray {
//    fn from_vec<T>(vec: Vec<T>) -> RubyArray {
//      let array = RubyArray {
//        data: vec.as_ptr() as *const libc::c_void,
//        len: vec.len() as libc::size_t
//      };
//      mem::forget(vec);
//      array
//    }
//  }
//
//
//  #[no_mangle]
//  pub extern fn arrayify(i: i32) -> RubyArray {
//  }
//
//  #[no_mangle]
//  pub extern fn factorial(i: RubyArray) -> i32 {
//  }
//
//  pub struct TwoNumbers {
//    n: i32,
//    k: i32,
//  }

#[no_mangle]
pub extern fn permutations(n: i32, k: i32) -> i32 {
  rust::permutations(n, k)
}

#[no_mangle]
pub extern fn combinatorial_count(n: i32, k: i32) -> i32 {
  rust::combinatorial_count(n, k)
}