Truth table generation for a given input

I want to generate a truth table for a given input. Suppose if I give input 2, the output will be

10 01 11 00 

if input 3, output will be

111 000 110 101 011 100 001 010

I have a piece of code

#!/usr/bin/perl

#print "a|b|c\n";

for $a (1, 0){
    for $b (1, 0){
      for  $c (1,0) {
        for $d ( 1,0) 
        {        
          print "$a $b $c $d";
          #print $x = ($a & $b & $c);
          print "\n";
        }
     }
   }
}

print "\n";

The code above is for 4.

I do not know how to do this without recording a few loops. Here for a value of 2, I need to write two for loops, etc.

can any body tell me how to configure this code for multiple input values.

Any help would be much appreciated

+3
source share
6 answers

Recursion

Here is a simple solution using recursion:

#!/usr/bin/perl -w                                                              
my $variables=$ARGV[0]||0;
show_combinations($variables);                                                           

sub show_combinations { my($n,@prefix)=@_;                                      
  if($n > 0) {                                                                  
    show_combinations( $n-1, @prefix, 0);                                       
    show_combinations( $n-1, @prefix, 1);                                       
  } else {                                                                      
    print "@prefix\n";                                                          
  }                                                                             
}

Here are some examples:

> script.pl 1
0
1
> script.pl 2
0 0
0 1
1 0
1 1
> script.pl 3
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
+7
source

Perl, , Perl, , , - :

#!/usr/bin/perl

my ($n) = @ARGV;

printf("%0*b\n", $n, $_) for 0 .. (1 << $n) - 1;
+6

Perl, :

- 0 (2 ^ n) -1, n - ;

- n- ;

+5

Perl- Math::Cartesian::Product.

use Math::Cartesian::Product;

cartesian {print "@_\n"} ([0..1]) x $ARGV[0];

    
./sample.pl 2

0 0
0 1
1 0
1 1

./sample.pl 3

0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
+5

Math::BigInt. :

#!/usr/bin/perl

use strict;
use warnings;

use Math::BigInt try => 'GMP';

my $n_bits = $ARGV[0] || 0;

my $it = make_it($n_bits);

while ( defined(my $bits = $it->()) ) {
    print "$bits\n";
}

sub make_it {
    my ($n_bits) = @_;

    my $limit = Math::BigInt->new('2');
    $limit->blsft($n_bits - 1);

    my $next = Math::BigInt->new('-1');

    return sub {
        $next->binc;
        return unless $next->bcmp($limit) < 0;
        my $bits = $next->as_bin;
        $bits =~ s/^0b//;
        if ( (my $x = length $bits) < $n_bits ) {
            $bits = '0' x ($n_bits - $x) . $bits;
        }
        return $bits;
    }
}

%b printf:

use strict;
use warnings;

my ($count) = @ARGV;

my $fmt = "%0${count}b";
my $n = 2**$count - 1;

for my $c (0 .. $n) {
    my @bits = split //, sprintf $fmt, $c;
    print "@bits\n";
}

This will only work for values $countless than 32.

Output:

C: \ Temp> y 3
0 0 0
0 0 1
0 1 0
0 1 1
one hundred
1 0 1
1 1 0
1 1 1
+4
source

I am surprised that no one mentioned globas a solution here:

perl -e 'print join "\n", glob("{0,1}" x shift || 1 )' -- 3

Fingerprints:

000
001
010
011
100
101
110
111

glob very convenient for calculating permutations of strings.

Here is the above, in a cleaner, not single-line form:

use strict;
use warnings;

my $symbol_count = shift || 1;

my @permutations = glob( '{0,1}' x $symbol_count );

print join "\n", @permutations;
+4
source

Source: https://habr.com/ru/post/1721772/


All Articles