in reply to Re: OT: Finding Factor Closest To Square Root
in thread OT: Finding Factor Closest To Square Root
Here's a fix:use strict; use warnings; use List::Util qw[ reduce ]; my @pfs = ( 3,5,7,19 ); my $num = reduce { $a * $b } @pfs; my $root = sqrt $num; print "N=$num, R=$root\n"; #the rest is the same as you had it
use strict; use warnings; use List::Util qw[ reduce ]; my @pfs = ( 3,5,7,19 ); my $num = reduce { $a * $b } @pfs; my $root = sqrt $num; print "N=$num, R=$root\n"; my $near = reduce{ abs($root - $a * $b) < abs($root - $a) ? $a*$b : $a } reverse @pfs; print abs($root - $near) > abs($root - ($num/$near)) ? $num/$near : $near ;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: OT: Finding Factor Closest To Square Root
by BrowserUk (Patriarch) on Feb 19, 2005 at 00:32 UTC | |
by Roy Johnson (Monsignor) on Feb 19, 2005 at 00:48 UTC | |
by BrowserUk (Patriarch) on Feb 19, 2005 at 01:08 UTC | |
by Roy Johnson (Monsignor) on Feb 19, 2005 at 22:20 UTC | |
by BrowserUk (Patriarch) on Feb 20, 2005 at 00:35 UTC | |
| |
by Limbic~Region (Chancellor) on Feb 26, 2005 at 06:52 UTC | |
by tall_man (Parson) on Feb 27, 2005 at 22:07 UTC | |
| |
by BrowserUk (Patriarch) on Feb 26, 2005 at 08:13 UTC | |
| |
by Roy Johnson (Monsignor) on Feb 22, 2005 at 17:05 UTC | |
by Limbic~Region (Chancellor) on Feb 24, 2005 at 17:12 UTC |