Help me to write this function by using C++

Hello everyone . .

How can I write this function by using C++

Function is: sum=1+2^2+3^3+4^4..+n^n .


I hope someone is able to help me.

Last edited on
I tried a lot but . . . ^_^

When trial and error fails, stop! Think!

Do you know what this is 2^2? Do you know how to do it in C++?
Last edited on
I know what it means.. But I do not know how I do!!

If you know please help me .

Because this is a homework . .

So...

State what 2^2, 3^3 4^4, etc. mean, in words!

How would you calculate 2^2 on paper? 3^3? 4^4?
Last edited on
Change your friends!:)
closed account (SGb4jE8b)
The N^S is that N Multiply itself N*N*N*N...N S number of times
closed account (SGb4jE8b)
If you know how to write a function that call itself perhaps that may be the best solution for sum=1+2^2+3^3+4^4..+n^n . And the function should stop calling itself when the nth Power is reached.
The N^S is that N Multiply itself N*N*N*N...N S number of times


I know . .

Who does not know!!

If you know how to write a function that call itself perhaps that may be the best solution for sum=1+2^2+3^3+4^4..+n^n . And the function should stop calling itself when the nth Power is reached.


My problem is I just need to solve this function, I do not want detail in the C++

Because this is a homework only!!




closed account (SGb4jE8b)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//sum=1+2^2+3^3+4^4..+n^n .
#include <iostream>
#include <cmath>
using namespace std;

double series( double );

int main()
{
	cout << series(4) << endl;
	return 0;
}
double series( double m )
{ 
	double sum = 0.0;
	static double x = 1.0;
	for ( int i = 1; i <= m; i++ )
	  sum += pow(x++,i);
	return sum;
}

I believe this may help even though is not what is intended, perhaps.
closed account (SGb4jE8b)
Is the above Code useful? If not, i need to know. If there's any way i can help you further.
If you know what N^S means, you should be able to write down a function that calculates it. And once you've done that, calcuating your sum should be easy.

If you can't work out the general function directly, try a simpler one. For example, a function that returns the powers or two.

int power_of_two(int power) { ...

0 -> 1, 1 -> 2, 2 -> 4, 3 -> 8, ... (power -> result)

You did say that you want to solve the problem! (Rather than be given it?)

Andy

PS I would not use recursion (have the function call itself) here; I would just use a loop!
Last edited on
Topic archived. No new replies allowed.