-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractice.cpp
More file actions
64 lines (52 loc) · 1.61 KB
/
practice.cpp
File metadata and controls
64 lines (52 loc) · 1.61 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
59
60
61
62
63
64
#include <iostream>
// find fibonacci using template meta-programming
template <long long N>
struct fibonacci{
static constexpr long long value = fibonacci<N-1>::value + fibonacci<N-2>::value;
};
template<>
struct fibonacci<1>{
static constexpr long long value = 1;
};
template<>
struct fibonacci<0>{
static constexpr long long value = 0;
};
// end code for fibonacci
// -------------------------------------------------------------------------------------------
// find gcd using template meta programming
template<unsigned M, unsigned N>
struct gcd{
static constexpr auto value = gcd<N,M%N>::value;
};
template<unsigned M>
struct gcd<M,0>{
static_assert(M!=0);
static constexpr auto value = M;
};
// -------------------------------------------------------------------------------------------
// factorial using template meta-programming
template<long long N>
struct factorial{
static constexpr long long value = N*(factorial<N-1>::value);
};
template<>
struct factorial<0>{
static constexpr long long value = 1;
};
// -------------------------------------------------------------------------------------------
// X*Y using templates
template<long long x, long long y>
struct multiply{
static constexpr long long value = N*(factorial<N-1>::value);
};
template<>
struct multiply<0>{
static constexpr long long value = 1;
};
// -------------------------------------------------------------------------------------------
int main(){
std::cout << gcd<15,5>::value << '\n'; // 5
std::cout << fibonacci<80>::value << '\n'; // 23416728348467685
std::cout << factorial<20>::value << '\n'; // 2432902008176640000
}