-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrabb_Pardo-Knuth_algorithm
executable file
·50 lines (43 loc) · 1.05 KB
/
Trabb_Pardo-Knuth_algorithm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/local/bin/perl
#u# http://rosettacode.org/wiki/Trabb_Pardo%E2%80%93Knuth_algorithm
#c# 2018-08-09 <>RC
#p# OK
use strict;
use warnings;
use feature 'say';
my $result;
# DH - on RC
# print "Enter 11 numbers\n" ;
# for ( 1..11 ) {
# $number = <STDIN> ;
# chomp $number ;
# push @sequence , $number ;
# }
#
# for my $n (reverse @sequence) {
# my $result = sqrt( abs($n) ) + 5 * $n**3;
# printf "f( %6.2f ) %s\n", $n, $result > 400 ? " too large!" : sprintf "= %6.2f", $result
# }
# }
my @sequence = (2,1.2,3,3.4,4,4.5,5,7.8,2.7,13,11.2);
for my $n (reverse @sequence) {
my $v = sqrt( abs($n) ) + 5 * $n**3;
$result .= sprintf "f( %6.2f ) %s\n", $n, $v > 400 ? " too large!" : sprintf "= %6.2f", $v
}
say $result;
my $ref = <<'EOD';
f( 11.20 ) too large!
f( 13.00 ) too large!
f( 2.70 ) = 100.06
f( 7.80 ) too large!
f( 5.00 ) too large!
f( 4.50 ) too large!
f( 4.00 ) = 322.00
f( 3.40 ) = 198.36
f( 3.00 ) = 136.73
f( 1.20 ) = 9.74
f( 2.00 ) = 41.41
EOD
use Test::More;
is ($result, $ref);
done_testing;