Skip to content

Commit

Permalink
recursion_basics solutions added
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristian-Krastev committed Jan 14, 2020
1 parent b175f1a commit 931e9fe
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 0 deletions.
51 changes: 51 additions & 0 deletions 11_recursion/practice/recursion_basic/tasks_01-02.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <iostream>

const int MAX = 12;

/*************** Task 1 ***************/
unsigned int factorial(unsigned int number) {
//limit
if (number > MAX)
return 0;
if (number == 0 || number == 1)
return 1;
return number * factorial(number - 1);
}

/*************** Task 2 ***************/
// main logic
bool is_prime_rec(unsigned int number, unsigned int stop, unsigned int i) {

if (number == 1 || number == 2)
return true;
if (number % i == 0)
return false;
if (i == stop)
return true;

return is_prime_rec(number, stop, i + 1);
}

// "wrapper function" is a function which wraps another function (in our example the recursive one).
// It's essentially another function which calls the actual function.
bool is_prime(unsigned int number) {

return is_prime_rec(number, sqrt(number), 2);
}

int main() {
unsigned int number;

// task_01
std::cin >> number;
std::cout << factorial(number) << std::endl;

// task_02
std::cin >> number;
if (is_prime(number))
std::cout << "Is prime \n";
else
std::cout << "Not prime \n";

return 0;
}
64 changes: 64 additions & 0 deletions 11_recursion/practice/recursion_basic/tasks_03-04.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <iostream>

/*************** Task 3 ***************/
// print the binary representation of a number
// "actual function"
void print_bits_rec(unsigned int number, unsigned int position) {

unsigned int mask = 1 << (position - 1);
std::cout << ((number&mask) ? "1" : "0");

if (position == 1)
return;
else
return print_bits_rec(number, position - 1);
}

// example of a "wrapper function"
// "wrapper function" is a function which wraps another function (in our example the recursive one).
// It's essentially another function which calls the actual function.
void print_bits(unsigned int number) {

unsigned int position = sizeof(unsigned int) * 8;
return print_bits_rec(number, position);
}

/*************** Task 4 ***************/
// print the number of zeros and ones in the binary representation of a number
// main logic
void count_bits_rec(unsigned int number, unsigned int position, int &zeroes, int &ones) {

unsigned int mask = 1 << (position - 1);
((number&mask) ? ones++ : zeroes++);

if (position == 1)
return;

else
return count_bits_rec(number, position - 1, zeroes, ones);
}

void count_bits(unsigned int number) {

int zeroes = 0;
int ones = 0;
unsigned int position = sizeof(unsigned int) * 8;
count_bits_rec(number, position,zeroes,ones);
std::cout << "In number: " << number << " we have " << zeroes << " zeroes and " << ones << " ones! \n";
}

int main() {

unsigned int number;
std::cin >> number;
// task_03
print_bits(number);

std::cout << std::endl;

// task_04
count_bits(number);
std::cout << std::endl;

return 0;
}
38 changes: 38 additions & 0 deletions 11_recursion/practice/recursion_basic/tasks_05-06.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <iostream>

// PAY ATTENTION how swapping two code lines changes the whole program
/*************** Task 5 ***************/
void print_forward(unsigned int number) {
if (number <= 0)
return;
print_forward(number / 10);
std::cout << (number % 10) << " ";
}

/*************** Task 6 ***************/
void print_backward(unsigned int number) {

if (number <= 0)
return;

std::cout << (number % 10) << " ";
print_backward(number / 10);
}

int main() {

unsigned int number;
std::cin >> number;

// task_05
std::cout << "Forward: ";
print_forward(number);
std::cout << std::endl;

// task_06
std::cout << "Backward: ";
print_backward(number);
std::cout << std::endl;

return 0;
}
59 changes: 59 additions & 0 deletions 11_recursion/practice/recursion_basic/tasks_07-08.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include<iostream>

/*************** Task 7 ***************/
// the actual recursive function
void print_line_rec(unsigned int k,unsigned int it, unsigned long long result) {

std::cout << result << ' ';

if (it < k)
print_line_rec(k, it + 1, result * 10);

std::cout << result << ' ';


}

// the wrapper
void printLine(unsigned int k) {

print_line_rec(k, 1, 10);
}


/*************** Task 8 ***************/
// just by following the recursive definition of the algorithm
int gcd(int a, int b) {

return (b == 0) ? a : gcd(b, a % b);
}


int main() {

// task_07 output
std::cout << "Enter a positive number k >= 1, k = ";
unsigned int k;

std::cin >> k;

if (k < 1)
std::cout << "cheater!\n";
else {
std::cout << "The line looks like : \n";
printLine(k);
}

// task_08 output
std::cout << "\n\nEnter two numbers :\n";

int a, b;

std::cin >> a >> b;

std::cout << "gcd(" << a << ", " << b << ") = "
<< gcd(a, b) << '\n';


return 0;
}

0 comments on commit 931e9fe

Please sign in to comment.