version 1.2, 2005/01/31 21:53:51
|
version 1.3, 2006/09/29 20:55:36
|
Line 1
|
Line 1
|
use strict; |
use strict; |
use capa; |
use warnings; |
|
|
my $unit="N"; |
my $n = 0; |
my $answer="3.4 N"; |
my $total = 0; |
my $scaled="3.2"; |
my $num_left = 0; |
print("\n return code is (should be 6)".&capa::caparesponse_get_real_response($unit,$answer,\$scaled)); |
my @order; |
print("\nscaled (should be 0.0034) ".$scaled." unit $unit answer $answer"); |
|
$unit="m"; |
sub factorial { |
$answer="3.4 mm/J"; |
my $input = CORE::int(shift); |
$scaled=3.2; |
return "Error - unable to take factorial of an negative number ($input)" if $input < 0; |
print("\n return code is (should be 15)".&capa::caparesponse_get_real_response($unit,$answer,\$scaled)); |
return "Error - factorial result is greater than system limit ($input)" if $input > 170; |
print("\nscaled (should be 3.2) ".$scaled." unit $unit answer $answer"); |
return 1 if $input == 0; |
print("\n"); |
my $result = 1; |
|
for (my $i=2; $i<=$input; $i++) { $result *= $i } |
|
return $result; |
|
} |
|
|
|
sub init { |
|
my ($size) = @_; |
|
@order = (0..$size-1); |
|
$n = $size; |
|
$total = $num_left = &factorial($size); |
|
} |
|
|
|
sub get_next { |
|
if ($num_left == $total) { |
|
$num_left--; |
|
return @order; |
|
} |
|
|
|
|
|
# Find largest index j with a[j] < a[j+1] |
|
|
|
my $j = scalar(@order) - 2; |
|
while ($order[$j] > $order[$j+1]) { |
|
$j--; |
|
} |
|
|
|
# Find index k such that a[k] is smallest integer |
|
# greater than a[j] to the right of a[j] |
|
|
|
my $k = scalar(@order) - 1; |
|
while ($order[$j] > $order[$k]) { |
|
$k--; |
|
} |
|
|
|
# Interchange a[j] and a[k] |
|
|
|
@order[($k,$j)] = @order[($j,$k)]; |
|
|
|
# Put tail end of permutation after jth position in increasing order |
|
|
|
my $r = scalar(@order) - 1; |
|
my $s = $j + 1; |
|
|
|
while ($r > $s) { |
|
@order[($s,$r)]=@order[($r,$s)]; |
|
$r--; |
|
$s++; |
|
} |
|
|
|
$num_left--; |
|
return(@order); |
|
} |
|
|
|
&init(9); |
|
while($num_left) { |
|
print(join(':',&get_next()).$/); |
|
} |