You can use trig and brute force a method -- I'm a little busy to give it a go, but the complex roots lie on a circle in the complex plane of the same radius as the real root. With a cube root, that means you need to find the coords of the points at 120 and 240 degrees (1/3 and 2/3 around the circle) on that circle -- hopefully that's clear. For the nth root you need to look at the (i * 360/n) degree points with i = 0..(n-1). i=0 corresponds to the real root.
Update:
#!/usr/bin/perl -w
use strict;
my $twoPi = 4 * atan2(1, 0);
for (2..5) {
my @roots = nthRoots(8, $_);
print "$_ th roots of 8: $/";
foreach my $root (@roots) {
print "$root->{Real} $root->{Imag}$/";
}
};
sub nthRoots {
my $x = shift;
my $n = shift;
# Should check for integer powers
my @roots;
$roots[0] = {
Real => $x ** (1/$n),
Imag => 0
};
for (1..($n-1)) {
push @roots, {
Real => $roots[0]->{Real} * cos( $_ * $twoPi/$n ),
Imag => $roots[0]->{Real} * sin( $_ * $twoPi/$n )
};
};
return @roots;
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.