Created
December 5, 2020 08:29
-
-
Save dasbluehole/7342b05fdae869b02dd39eefdc77a614 to your computer and use it in GitHub Desktop.
nth root of any number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//finding n nth root of any number. | |
#include <stdio.h> | |
#include <math.h> | |
#include <complex.h> | |
// find nth root of c and store it in root array | |
void cnroot(complex c, int n, complex *root) | |
{ | |
double m = cabs(c); | |
double th = carg(c); | |
for(int i=0; i< n; i++) | |
{ | |
double x = pow(m,1.0/n)*(cos((th+2*M_PI*i)/n)); | |
double y = pow(m,1.0/n)*(sin((th+2*M_PI*i)/n)); | |
root[i] = x+I*y; | |
} | |
} | |
void printfc(complex z) | |
{ | |
printf("%f%+fi\n", (double)z, *((double*)&z+1)); | |
} | |
int main() | |
{ | |
complex z; | |
complex rt[30]; // array to store roots(pre allocated for 30 | |
z = 1; | |
cnroot(z,5,rt); | |
printfc(rt[0]); | |
printfc(rt[1]); | |
printfc(rt[2]); | |
printfc(rt[3]); | |
printfc(rt[4]); | |
} | |
/* five fifth roots of 1 | |
* 1.000000+0.000000i | |
* 0.309017+0.951057i | |
* -0.809017+0.587785i | |
* -0.809017-0.587785i | |
* 0.309017-0.951057i | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment