File: example.cpp

package info (click to toggle)
aspectc%2B%2B 1.0pre4~svn.20080711-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 104,504 kB
  • ctags: 363,975
  • sloc: cpp: 1,645,814; ansic: 16,601; sh: 2,175; makefile: 1,043
file content (58 lines) | stat: -rw-r--r-- 1,054 bytes parent folder | download | duplicates (11)
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
#include <iostream>

int prod(int m, int n) {
  if (m > n)
    return -1;
  int prod = m;
  do {
    m++;
    prod *= m;
  } while (m < n);
  return prod;
}
       
int prod_r(int m, int n) {
  if (m > n)
    return -1;
  if (m == n-1)
    return m * n;
  else
    return m * prod_r(++m, n);
}
       
int fac(int n) {
  int fac = n;
  while (n > 1) {
    n--;
    fac *= n;
  }
  return fac;
}
       
int fac_r(int n) {
  if (n == 0)
    return 1;
  return n * fac_r(--n);
}
       
int binom1(int m, int n) {
  if (m < n)
    return 0;
  return fac(m) / (fac(n) * fac(m-n));
}
       
int binom2(int m, int n) {
  return prod(n, m) * (m-n + 1) / fac(n);
}


int main() {
  int x = 2, y = 4;
  std::cout << "Product:    " << prod(x,y) << std::endl;
  std::cout << "Product:    " << prod_r(x,y) << std::endl;
  std::cout << "Faculty:    " << fac(x) << std::endl;
  std::cout << "Faculty:    " << fac_r(x) << std::endl;  
  std::cout << "Binom 1:    " << binom1(y,x) << std::endl;
  std::cout << "Binom 2:    " << binom2(y,x) << std::endl;
  return 0;
}