### I'm stuck!

Hey guys, I'm trying to write a code that will calculate this equation y^3=x^2+4
I've got it sorta figured out, but I'm having some problems with cbrt function.
There should be two answers first one is i=2, and the second one would be i=5.
But it seems that the code doesn't work properly for some reason, it gives me only i=2 but not the second solution, any suggestions why?
Any help would be appreciated.

 ``123456789101112131415161718192021222324`` ``````#include #include using namespace std; int main() { int i=1; double a=0; int b=0; int d=0; while(i<1000) { i++; d=pow(i,2)+4; a=(double)cbrt(d); b=(int)cbrt(d); if(a==b) cout<<"y = " << i << endl; } return 0; } ``````
Last edited on
 y^3=x^2+4

This equation has infinite number of solutions.

https://www.wolframalpha.com/input/?i=y%5E3%3Dx%5E2%2B4
Last edited on
Yeah, if you use decimals or expand the limit which is 1000 now.
 ``12345678910111213141516171819`` ``````#include #include bool is_perfect_square( unsigned int n ) { const double root = std::sqrt(n) ; const unsigned int lbound = root - 1 ; const unsigned int ubound = root + 2 ; for( auto i = lbound ; i < ubound ; ++i ) if( i*i == n ) return true ; return false ; } int main () { for( unsigned int i = 2 ; i < 1000 ; ++i ) if( is_perfect_square( i*i*i - 4 ) ) std::cout << i << '\n' ; }``````

http://coliru.stacked-crooked.com/a/eb28ec89dd0047f8

If you have an equation with 2 variables, why are you solving for only 1 variable?

If you have variables in your equation named x and y, why does your code use a variable named i? Line 19 looks goofy in this regard.

The values a and b are actually the "y" value in your equation, where a is the actual value and b is the floor of the actual value. What you are checking for in line 18 is that the value y is an integer (has no decimal places). Not only is this not what you are trying to do, it is an unsafe way to make that determination.

I'm guessing what you are trying to find is the solution of x^3 = x^2 + 4. The value 2 is a solution to this equation, but 5 is not.

Edit: I started my reply before @JLBorges's reply was posted. That response helps me understand the problem description, and I agree with that solution.
Last edited on
Solved it myself, but anyway thanks guys for the help.
Don't worry Doug4, there are two solutions, just didn't really formulated the sentece when I said that there are two i's . Here check the code for yourself.
Here is the solved issue in the code.

 ``123456789101112131415161718192021222324`` ``````#include #include using namespace std; int main() { int i=1; double a=0; int b=0; int d=0; while(i<1000) { i++; d=i*i+4; a=cbrt(d); b=cbrt(d); if(a==b) cout<<"x = " << i << ", y = " << a << endl; } return 0; } ``````
Last edited on
Topic archived. No new replies allowed.