fork(1) download
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my $MAXn = 10;
  7.  
  8. # Brute force.
  9. for my $n (0 .. $MAXn) {
  10. my $alphabet = '{a,b,c}';
  11. my @words = glob $alphabet x $n;
  12. my @goodwords = grep { /a/ and !/a.*c/ } @words;
  13.  
  14. # print $_ . "\n" for (@goodwords);
  15. print "Brute force: b($n) = " . @goodwords . "\n";
  16. }
  17.  
  18. print "\n\n" . "-" x 30 . "\n\n";
  19.  
  20. # Recurrence relation.
  21. my @bn;
  22.  
  23. for my $n (0 .. $MAXn) {
  24. $bn[$n] = 0 if ($n == 0);
  25. $bn[$n] = 2 * $bn[ $n - 1 ] + 2**($n - 1) if $n > 0;
  26.  
  27. print "Recurrence: b($n) = $bn[$n]\n";
  28. }
Success #stdin #stdout 0.28s 8896KB
stdin
Standard input is empty
stdout
Brute force: b(0) = 0
Brute force: b(1) = 1
Brute force: b(2) = 4
Brute force: b(3) = 12
Brute force: b(4) = 32
Brute force: b(5) = 80
Brute force: b(6) = 192
Brute force: b(7) = 448
Brute force: b(8) = 1024
Brute force: b(9) = 2304
Brute force: b(10) = 5120


------------------------------

Recurrence: b(0) = 0
Recurrence: b(1) = 1
Recurrence: b(2) = 4
Recurrence: b(3) = 12
Recurrence: b(4) = 32
Recurrence: b(5) = 80
Recurrence: b(6) = 192
Recurrence: b(7) = 448
Recurrence: b(8) = 1024
Recurrence: b(9) = 2304
Recurrence: b(10) = 5120