Expressing y = x^x

Hello,

I was wonder how I could get the value of x in C code if you are given the value of y? I've been going over it in my head again and again and I can't come up with a solution. If y = x^x...x = y?y ?

Doing it in head is one thing, but coding it is another(for me)
Thanks
Last edited on
Doing it in head is one thing,


How do you do it in your head?

The translation of head->code is the easy part. I can't wrap my head around how to solve the problem at all. To isolate x you need to do log base x -- but you can't do log base x because you don't know what x is for the base.
No, I misspoke without really thinking about it I guess. If I was given a calculate trial and error would solve it haha. However...this questions is blowing my bits off.

y=e^(x*ln(x))
is the same thing

Hmm, is there a software algorithm that could would literally check every possible value? of x^x to get y? That would work too. Although i'd say it'd be a long process...
Last edited on
"Hey irishguy95.

You are going to have to use a numerical algorithm to get an approximate answer. For a given value of y you are solving xlog(x) - log(y) = 0 where log(y) is given and x is not."

From a maths forum...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main()
{

	float x,y,logx,logy, z;
	x = 0;
	y = 20;
	
	logx = log(x);
	logy = log(y);
	
	z = x*log(x) - log(y); 
	x = 0 + x*log(x) - log(y);
	
	cout << "x is "<< x << endl <<"y is"<< y << endl<< "Log of y is " << logy;
}


Eh, I don't even know here
Okay i'm not sure if i'm close or not. I'm trying to do it with a margin of error instead...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <math.h>
using namespace std;

int main()
{

	float x,y,logx,logy,logx2, z,z1,z2;
	x = 1; 
	y = 12;
	z2 = pow(x,x);
		while (z2 <= y )
		{	x+0.1;
		}

		
	cout << "\n\nAnswer X is approximately " << x
		 << "\nZ2 is : " << z2
		 << "\ny is : " << y ;
		
}


So...if it went like I hoped, x would add 0.1 until z2 became greater than y...which would leave a value for x that was close to the answer. But nothign is outputting, its stuck in the loop I think.
Last edited on
I don't really get you.
The thing i understood that you are given an expression y=x^x, that i think you want to say?
and you are having value of y known and want to get value of x from this expression...
so, i want help you as y=x^x can also be expressed as x=y^0.5
1
2
3
4
5
6
7
8
9
10
11
12
13
#include<iostream>
#include<math.h>

using namespace std;

int main(void )
{
    float x=0.0,y=0.0;
    cout<<"enter value of y :\t";
    cin>>y;
    x=pow(y,0.5);
    cout<<endl<<"value of x :\t"<<x;
}
closed account (D80DSL3A)
Here's something simple that works...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cmath>
//using namespace std;

int main()
{
    double x=1.0, y=12.0, dx=1.0, toler = 1e-8;

    while( y - pow(x,x) > toler )// continue refining x value
    {
        while( pow(x,x) < y )
            x += dx;// last iteration will overshoot the value
        x -= dx;// back up that last step
        dx /= 10.0;// again, but with finer steps
    }

    std::cout << "x = " << x << "  x^x = " << pow(x,x) << "  dx = " << dx << '\n';

    return 0;
}
Last edited on
> If I was given a calculate trial and error would solve it haha.
well, do that. You've got a computer, can easily test a lot of candidates quite easily.

> Hmm, is there a software algorithm that could would literally check every possible value?
not every one
The density property tells us that we can always find another real number that lies between any two real numbers.
(perhaps if you know that the solution is an integer...)

You work with an approximation and a tolerance.
fun2code shows you a linear search, where you move your candidate according to a delta (refining the delta).

Here you've got other methods https://en.wikipedia.org/wiki/Root-finding_algorithm


Edit: by looking at your attempts it seems that you've got a conceptual error.
In C++ z = x*log(x) - log(y); is not an equation, but an statement. It says «assign to `z' the result of the operation on the right», the logs are computed using the current value of `x' and `y',
It works with numbers, not with expresions, if you later change `x', `z' would remain unaffected.
So
1
2
3
4
	z2 = pow(x,x);
		while (z2 <= y )
		{	x+0.1;
		}
is an infinitie loop, as the condition will never change
Last edited on
Thanks guys, coulda swore I posted this a few days ago :/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <math.h>
using namespace std;

int main()
{

	float x,y,z;    //declaring float variables
	x = 0;			// intializing variables
	y = 0;			//.................
	z = pow(x,x);	//.................

	cout << "User please enter any value for y" << endl;
	cin >> y;

		while (z <= y ) // main loop, while z(which is equal to x^x) is less than y, the while loop will run.
		{	x= 0.000001;  //increment x by adding 0.000001 each time
						// the margin of error will be reduced if the incrementation is reduced, but it will take longer
		    z = pow(x,x); // z = x^x
		
		
		}

		
	cout << "\n\nX is approximately " << x 
		 << "\nZ is : " << z
		 << "\ny is : " << y ;
		
	
}


appi, thank you...but I tried it and it doesn't work.

y^0.5 = x multiplied by x. It must be to the power
Last edited on
Topic archived. No new replies allowed.