-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariable_template.cpp
More file actions
60 lines (37 loc) · 1.08 KB
/
variable_template.cpp
File metadata and controls
60 lines (37 loc) · 1.08 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
#include <iostream>
///////////////////////////////////////////
template<int N>
const int factorial = N * factorial<N-1>;
template<>
const int factorial<0> = 1;
///////////////////////////////////////////
template<int E, int N=1>
const int pow = E * pow<E,N-1>;
template<int E>
const int pow<E,0> = 1;
///////////////////////////////////////////
template<int N, int M=N-1>
const bool isPrime = N % M && isPrime<N,M-1>;
template<int N>
const bool isPrime<N,1> = N != 1;
template<int N>
const bool isPrime<N,0> = false;
///////////////////////////////////////////
template<int N>
const bool isPoweorOf2 = (N & N-1) == 0 && N != 0;
///////////////////////////////////////////
template<int N>
const int fibonacci = fibonacci<N-1> + fibonacci<N-2>;
template<>
const int fibonacci<1> = 1;
template<>
const int fibonacci<2> = 1;
///////////////////////////////////////////
int main(){
std::cout<< factorial<7> <<'\n';
std::cout<< pow<2,7> <<'\n';
std::cout<< isPrime<35> <<'\n';
std::cout<< isPoweorOf2<32> <<'\n';
std::cout<< fibonacci<9> <<'\n';
return 0;
}