From 3833c952f1dff3957bc423045df6d52c14a6db78 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Sun, 19 Jul 2020 21:24:19 +0200 Subject: chore: new algorithms --- I_anno/Programmazione_2/algorithms/pow.cc | 34 +++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 I_anno/Programmazione_2/algorithms/pow.cc (limited to 'I_anno/Programmazione_2/algorithms/pow.cc') diff --git a/I_anno/Programmazione_2/algorithms/pow.cc b/I_anno/Programmazione_2/algorithms/pow.cc new file mode 100644 index 0000000..70f5b43 --- /dev/null +++ b/I_anno/Programmazione_2/algorithms/pow.cc @@ -0,0 +1,34 @@ +#include + +using namespace std; + +int pow(int b, int e) { + if(e == 0) return 1; + int a = b; + int inc = b; + for(int i = 0; i < e-1; ++i) { + for(int j = 0; j < b-1; ++j) { + a+=inc; + } + inc = a; + } + return a; +} + +int mul(int b, int e) { + if(e == 0) return 0; + + return b+mul(b, e-1); +} + +int pow2(int b, int e) { + if(e == 0) return 1; + + return mul(b, pow(b, e-1)); +} + +int main() { + cout << pow(2, 5) << endl; + cout << pow2(2, 5) << endl; + return 0; +} -- cgit v1.2.3-18-g5258