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);
}
}
Related
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);
}
I want to store the values produced by a recursive function in a string, but I am not sure how to keep after each iteration of the loop. Im not necessarily looking for you to solve it particular to the attached code, but I figured it would give it some context. Simply commenting resources where I can learn this is, of course, welcome.
Thanks
int HailstoneNumbers(int N)
{
vector <char> sequence;
static int c;
cout << N << " ";
if (N == 1 && c == 0) {
// N is initially 1.
return c;
}
else if (N == 1 && c != 0) {
// N is reduced to 1.
c++;
return c;
}
else if (N % 2 == 0) {
// If N is Even.
c++;
HailstoneNumbers(N / 2);
}
else if (N % 2 != 0) {
// N is Odd.
c++;
HailstoneNumbers(3 * N + 1);
}
}
If you want to store N values, you can store them in a vector as follows
int HailstoneNumbers(int N, vector<int>& sequence)
{
int c;
sequence.push_back(N);
if (N == 1 && c == 0) {
// N is initially 1.
return c;
}
else if (N == 1 && c != 0) {
// N is reduced to 1.
c++;
return c;
}
else if (N % 2 == 0) {
// If N is Even.
c++;
HailstoneNumbers(N / 2, sequence);
}
else if (N % 2 != 0) {
// N is Odd.
c++;
HailstoneNumbers(3 * N + 1, sequence);
}
}
declare a vector before calling your function as vector<int> sequence; then call your function using your N and this vector
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)
}
Write a recursive C ++ function that returns the smallest even digit of a natural number transmitted as a parameter.
If the number does not contain any even digit, it will return -1.
Easy to do without recursion:
int cifminpar(int x)
{
int mi = 9;
while(x)
{
if(x % 10 % 2 == 0)
mi = min(mi , x%10 );
x /= 10;
}
if(mi == 9)
return -1;
else return mi;
}
How do I do that with recursion ?
What about something like the following?
int cifminpar(const int x)
{
if(!x)
return 11;
//recursive call
int minrest=cifminpar(x/10); //min even in the rest of the digits
if(x % 10 % 2 == 0)
return min(minrest , x%10 );
return minrest;
}
It returns 11 if no even digits are found and work as follows and assumes that the initial number is not 0.
You can easily fix modify it to return -1 on failure and for 0 as input.
Remember: The smallest even digit is either "the first one" or "the smallest one in the number without the first one".
You can recursively compare "current digit" and "the smallest digit after it":
int cifminpar(int x)
{
int ret = cifminpar_recur(x, 10);
if (ret == 10)
return -1;
return ret;
}
int cifminpar_recur(int x, int mi)
{
if (x == 0)
return mi;
if (x % 2 == 1)
mi = min(mi, x % 10);
return cifminpar_recur(x / 10, mi);
}
And we can even drop the extra variable.
int cifminpar(int x)
{
int ret = cifminpar_recur_optimised(x);
if (ret == 10)
return -1;
return ret;
}
int cifminpar_recur_optimised(int x)
{
if (x == 0)
return 10;
if (x % 2 == 1)
return min(x % 10, cifminpar_recur_optimised(x / 10));
return cifminpar_recur_optimised(x / 10);
}
#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.