How do I write a recursive function to compute (3^2)+(3^3)+(3^4).......+(3^n) ?

Dani AI

Generated

Quick summary and plan: the sum you want is S(n) = 3^2 + 3^3 + ... + 3^n. There are two practical approaches: a closed-form geometric formula (fast and exact for small n) or a simple loop/recursive helper (safer for avoiding floating-point rounding). was right to ask for code, pointed to the need for a clear base case, correctly caught the prototype mismatch, and ’s helper idea is the right direction.

Closed form (fast). Using the geometric-series identity,
S(n) = (3^(n+1) - 9) / 2.
Avoid using pow (it returns double and can round). Compute integer powers with exponentiation-by-squaring:

long long ipow(long long base, int exp) {
    long long result = 1;
    while (exp > 0) {
        if (exp & 1) result *= base;
        base *= base;
        exp >>= 1;
    }
    return result;
}

long long sum3_closed(int n) {
    if (n < 2) return 0;
    return (ipow(3LL, n + 1) - 9) / 2;
}

Iterative / tail-recursive option (robust). Iteration avoids stack/optimization issues and is easy to reason about:

long long sum3_iter(int n) {
    if (n < 2) return 0;
    long long sum = 0;
    long long p = 9; // 3^2
    for (int k = 2; k <= n; ++k) {
        sum += p;
        if (k != n) p *= 3;
    }
    return sum;
}

Practical notes and debugging tips: match your prototype and definition (the int func(int); vs int func(int,int) mismatch that flagged will cause compiler/link confusion). If you see LNK1561 in Visual Studio, ensure a proper main exists and the project’s Subsystem is correct (Console vs Windows) — the linker is complaining it can’t find the entry point. Watch integer overflow: with signed 64-bit, n <= 38 is safe for the closed form; larger n require big-integer types (e.g., Boost.Multiprecision) or arbitrary-precision arithmetic.

Recommended Answers

All 9 Replies

What have you done, besides posting your homework assignment?Show your code to us and pinpoint the errors you have.We will be more than happy to help. :)

Well said ddanbe.
Here the principle of recursive function :
Technically, a recursive function is a function that makes a call to itself. To prevent infinite recursion, you need to know the stop condition.

Here in your case the stop condition should be n == 1. and you should care about the specif case of n == 0.

An other hint?

What could the function prototype look like?

int sumToN( const int nSum, const int nTerm, const int N );

What would the tail recursive call look like?

How would you call the the function?
(What initial values ?)

not sure but is it something along the line of this?

int func(int base, int power)
{   base=3
    if(power == 2)
        return pow(base, power);
    return pow(base, power) + function(base, power - 1);
}

Don't get why its saying I have too many arguments:

#include <iostream>
using namespace std;

//Function prototype
int function(int);

int main()
{
    int number;
    cout << "Enter an integer value and I will display\n";
    cout << "the value: ";
    cin >> number;

    cout << "The function of " << number << "is";
    cout << function(number) << endl;

    system("pause");
    return 0;
}

int func(int base, int power)
{   base=3;
    if(power == 2)
        return pow(base, power);
    return pow(base, power) + function(base, power - 1);
}

Don't call your function, "function" or "func". Name it after what your function does.
You have defined a function prototype which can accept one int parameter, yet you use that function function with two parameters on line 25.

I'm getting error that says, LNK 1561: entry point must be defined error.
How do I fix this, am I doing this right?

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

//Function prototype
int func(int);

int main()
{
int number;
cout << "Enter an integer value and I will display\n";
cout << "the value: ";
cin >> number;       
cout << "The function of " << number << "is";
cout << func(number) << endl;

    system("pause");
    return 0;
}

int func(int base, int power)
 {  base=3;
      if(power <= 2)
         return pow(base, power);
      else if (power==3)
         return 27;
      else
         return pow(base, power) + func(base, power + 1);
 }

Does line 7:int func(int); look the same as line 22:int func(int base, int power)?

If you can use an 'helper' calling function then ... HINT:

double sumToN( const double nSum, const double x, const double nTerm, const int N )
{
    // if( N ...... /// return nSum;
    // return sumToN( nSum + /////////////////////
}

// helper calling function ...
double sumToN( const double x, const int N )
{
    return sumToN( 0, x, x*x, N );
}

Just fill in the 2 lines and you are done.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.