Create Function for cube root | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Create Function for cube root

I need To create a function to find cube root?

7th Jan 2022, 5:48 PM
ABUBAKAR ALHOMIDY
ABUBAKAR ALHOMIDY - avatar
3 Answers
0
#include <iostream> #include <math.h> using namespace std; int main() { int num; cin>>num; int cubeRoot=cbrt(num); cout<<"Cube root of "<<num<<" is "<<cubeRoot <<endl; return 0; }
7th Jan 2022, 7:59 PM
HK Lite
HK Lite - avatar
0
I need a function without cmath. Function by use for loop.
7th Jan 2022, 8:32 PM
ABUBAKAR ALHOMIDY
ABUBAKAR ALHOMIDY - avatar
0
You could use the Babylonian algorithm for a sqrt adapted for cbrt Let's consider y = cbrt(x+k) You can see that when x=0 than y=cbrt(k) Which translates to finding the zeros of the inverse function: y^3 - k = x We can use Newton's method: next = prev - f(prev)/f'(prev) Here f(y) = y^3-k And f'(y) = 3y^2 (the derivative of f) At the end you should get next = (2prev + k/prev^2)/3
8th Jan 2022, 2:17 PM
Angelo
Angelo - avatar