Recursions and functions c++ - c++

I have made a function and I am trying to make it recursive. Does anyone have any tips on how I can make this function recursive? I know recursive means to use the function in the function itself.
int countEven(int n){
int evens = 0;
if(n <= 0) return 0; //base case
while(n > 0){
int digit = n%10; //get the last digit
if(digit%2 == 0){
evens = evens + 1;
}
n = n/10;
}
cout << evens;
}

int rec(int n)
{
int sum = 0;
if(n<=0)
return 0;
else if ((n%10)%2==0)
sum = rec(n/10)+1;
else
sum = rec(n/10);
return sum;
}
Maybe something like this :)

For counting the even digits of an integer base 10 you can simplify the function to the following
int countEven(int n)
{
if (n != 0) return !(n % 2) + countEven(n/10);
else return 0;
}
This expands as follows. Assume n = 258:
countEven(258) =
1 + countEven(25) =
1 + 0 + countEven(2) =
1 + 0 + 1 + countEven(0) = 2
Note that the statement !(n % 2) returns 1 if n is even and 0 if it's odd.
For shorter you can do the following:
int ce(int n) { return n ? !(n&1) + ce(n/10) : 0; }
using the ternary operator.

seems like you're trying to count the even digits in a number
int countEven(int n){
if(n == 0)
return 0; //base case
if (n<10)
return !(n%2);
return !(n%2)+countEven(n/10);
}

looks like a similar question i received from QC.
to make it recursive, you must have the function calling onto itself. Ask how you can make the the input simpler and have some sort of base so that the function doesn't break.
int countEven(int number) {
if (x <= 0) return 0;
if (x % 2 == 0) {
return countEven(number / 10) + 1;
}
return countEven(number / 10)
}

Related

STRONGN Strong Number (Special Numbers Series #2) word problem on Code Wars

Strong number is the number that the sum of the factorial of its digits is equal to number itself.
For example: 145, since
1! + 4! + 5! = 1 + 24 + 120 = 145
Here is my code, It passes most of the test except one test
#include <string>
using namespace std;
string strong_num (int number )
{
int sum = 0;
while(number != 0) {
int last = number % 10;
number /= 10;
sum+= last * (last-1);
}
if(sum == number)
return "STRONG!!!!";
else
return "Not Strong !!";
}
What is wrong with my code?
I'm surprised you're passing any test cases at all. For one thing, you are destroying number before you compare it to sum, and for another your logic is flawed.
Try this:
int factorial (int x)
{
int result = 1;
while (x > 1)
{
result *= x;
x--;
}
return result;
}
string strong_num (int number)
{
int sum = 0;
int x = number;
while (x != 0) {
int digit = x % 10;
sum += factorial (digit);
x /= 10;
}
if (sum == number)
return "STRONG!!!!";
else
return "Not Strong !!";
}
Live demo
Replace int by long long to be able to test larger numbers.
There are two problems:
first - you are changing the value of number before comparing it to sum,
second - the thing you used last * (last-1) is not a definition of factorial, the definition of factorial is factorial(x) = 1 * 2 * 3 * ... * x
int factorial (int x) {
if(x < 2) return 1;
return x * factorial(x - 1);
}
string strong_num (int number)
{
int sum = 0;
int x = number;
while (x != 0) {
int last = x % 10;
sum += factorial (last);
x /= 10;
}
if (sum == number)
return "STRONG!!!!";
else
return "Not Strong !!";
}

Recursive function calls working behind the scenes

So I can't wrap my head around recursive function calls especially with this example:
int Addup(int n)
{
//6
if(n <= 1)
return 1;
else
return n + Addup(n - 1);
/*
6 + 5 + 4 + 3 + 2 + 1
*/
}
Now if for example we did Addup(6), how will that work, what will the program do at runtime? Will it chain all the evaluations after the n then sum them together. I really can't visualize it.
If someone could have a simple way of demonstrating what actually happens at runtime it would be really great.
Thanks in advance.
The secret to understanding recursion is that recursive functions work exactly like non-recursive functions.
Your functions work exactly like these:
int Addup_1(int n)
{
return 1;
}
int Addup_2(int n)
{
if(n <= 1)
return 1;
else
return n + Addup_1(n - 1);
}
int Addup_3(int n)
{
if(n <= 1)
return 1;
else
return n + Addup_2(n - 1);
}
int Addup_4(int n)
{
if(n <= 1)
return 1;
else
return n + Addup_3(n - 1);
}
int Addup_5(int n)
{
if(n <= 1)
return 1;
else
return n + Addup_4(n - 1);
}
int Addup_6(int n)
{
if(n <= 1)
return 1;
else
return n + Addup_5(n - 1);
}

Nth Day of Christmas with recursion in C++

I want to solve this with recursion, but I am having trouble figuring out whats wrong. Create a function where given n days as an argument, return the total amount of items received throughout Christmas days as an integer.
xmasItems(1) = 1
xmasItems(3) = 10 : Day1 = (1), Day2 = (1+2), Day3 = (1+2+3) | Day 3 total = (1)+ (1+2) + (1+2+3) = 10
int xmasItems(int n) {
if (n == 0) { return 0; }
else {
int forThatDay = 0;
while (n != 0) {
forThatDay += n;
n--;
}
return forThatDay + xmasItems(n - 1);
}
}
In the while loop of your else branch, you are decrementing n all the way to 0. So you are computing only the first day's value.
Instead, you could use a copy of n for the forThatDay calculation, so you can make the recursive call correctly:
int xmasItems(int n) {
if (n == 0) { return 0; }
else {
int forThatDay = 0;
int m = n; // copy n and use it
while (m != 0) {
forThatDay += m;
m--;
}
return forThatDay + xmasItems(n - 1); // now n is correct
}
}
Aside: there is a closed form solution for the sum of n natural numbers, so you shouldn't need a loop anyway.

Calculate the function F(n) with recursion

Read the topic that I do not know what it is saying:
The function F (n) determined on non-negative integers as follows:
F (0) = 1; F (1) = 1; F (2n) = f (n); F (2n + 1) = F (n) + F (n + 1)
Calculated F (n) by recursion.
and my code:
#include<iostream.h>
double TINH_F(int n)
{
if(n == 0)
{
return 0;
}
if(n == 1)
{
return 1;
}
return (F(n+1) - F(2*n+1));
}
This is obviously incorrect. A recursive function calls itself and includes a stopping condition:
#include<iostream.h>
double TINH_F(int n)
{
if(n == 0)
{
return 0;
}
if(n == 1)
{
return 1;
}
// Note the function name change
return (TINH_F(n+1) - TINH_F(2*n+1));
}
What should your function do if the integer passed in is negative? Will the recursion still work? Or should you throw an exception to indicate to callers that the contract is broken?
int f(int n)
{
if (n<0) return -1; // check if n is positive
if (n<=1) return 1; // we can catch the first two cases in a single condition
int half = n/2;
if (n % 2 == 0) return f(half); // if n is even
else return f(half) + f(half+1); // if n is odd
}
Your last case says
F (n) = F (n+1) + F(2 * n + 1), for all n > 1
If you read the definition carefully, this case is not mentioned anywhere.
I believe you're being tricked by the naming of the parameter - you need four cases.
Let's break it down:
F (0) = 1 (or 0 - your definition says 1, but the code says 0...)
F (1) = 1
F (2n) = F (n)
F (2n + 1) = F (n) + F (n + 1)
The first two cases are trivial.
The third case says that if the argument - let's call it m - is even, the result is F(m/2).
The fourth case says that if the argument m is odd, the result is F(m/2) + F(m/2 + 1).
(Confirming the arithmetic left as an exercise.)
In C++:
unsigned int TINH_F(unsigned int n)
{
if(n == 0)
{
return 1;
}
else if(n == 1)
{
return 1;
}
else if (n % 2 == 0)
{
return TINH_F(n / 2);
}
else
{
return TINH_F(n/2) + TINH_F(n/2 + 1);
}
}

C++ Recursion Segmentation Fault

#include <iostream>
using namespace std;
int findSumofOdds(int n);
int main()
{
int n = 88;
int x;
x = findSumofOdds(n);
cout << x << endl;
return 0;
}
int findSumofOdds(int n)
{
if (n != 1)
{
if( n % 2 == 0)
n = (n - 1);
return(findSumofOdds(n-1) + 1);
}
else
return 1;
}
Why isn't this recursion working? It tries to run and then crashes. Please let me know. My teacher said that it would work but doesn't.
When n is even, you are decrementing n by two. If it skips over n == 1, it will recurse until it causes a stack overflow. Since n starts out at 88, that's what's happening.
int findSumofOdds(int n)
{
if (n != 1)
{
if( n % 2 == 0)
n = (n - 1); // <== first decrement
return(findSumofOdds(n-1) + 1); // <== second decrement
}
else
return 1;
}
Also, you seem to be counting the number of odd numbers, not adding them. My guess is that you actually want something like:
int findSumofOdds(int n)
{
if (n != 1)
{
if( n % 2 == 0)
return(findSumofOdds(n - 1));
return(findSumofOdds(n-1) + n); // or + 1 to just count
}
else
return 1;
}
If you want to practice recursion, that's fine. But there's a much simpler way to write a function to sum the odd numbers up to and including n:
int fundSumofOdds(int n) {
n = (n + 1) / 2;
return n * n;
}
This is because there's a general formula:
1 + 3 + 5 + ... + 2n-1 = n2
You have to make it
if (n > 1)
Consider n = 2 here
if (n != 1)
{
if( n % 2 == 0) // Yes
n = (n - 1); // n = 1
return(findSumofOdds(n-1) + 1); // n = 0 <-- will not stop.
and change this too. right now it is just counting the number of odd numbers. You need to sum them.
return(n + findSumofOdds(n - 1));
}
else
return 0;
If you print n in findSumofOdds you can see what is happening -- n becomes negative and you get infinite recursion. If your program doesn't crash earlier, you can get integer overflow (when n goes below the minimum value for int) which yields undefined behavior.
To correct this you can do this:
int findSumofOdds(int n)
{
if(n < 1)
{
return 0;
}
if(n % 2 == 0)
{
return findSumofOdds(n - 1) + 1;
}
return findSumofOdds(n - 2) + 1;
}
You can subtract 2 from n in the last statement, because you only need odd numbers and you know that n can't be even at that point (because of if(n % 2 == 0)).
Also, do you need to find the sum of all odd numbers smaller than n (e.g. 4 (== 1 + 3) for n=5) or do you just need to count them (which is what you are doing now)?
If you want to sum the numbers, you have do add n instead of 1 when returning.