#!/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; }