How to resolve the algorithm Truth table step by step in the PARI/GP programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Truth table step by step in the PARI/GP programming language

Table of Contents

Problem Statement

A truth table is a display of the inputs to, and the output of a Boolean function organized as a table where each row gives one combination of input values and the corresponding value of the function.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Truth table step by step in the PARI/GP programming language

Source code in the pari/gp programming language

vars(P)={
    my(v=List(),x);
    while(type(P)=="t_POL",
        x=variable(P);
        listput(v,x);
        P=subst(P,x,1)
    );
    Vec(v)
};
truthTable(P)={
    my(var=vars(P),t,b);
    for(i=0,2^#var-1,
        t=eval(P);
        for(j=1,#var,
            b=bittest(i,j-1);
            t=subst(t,var[j],b);
            print1(b)
        );
        print(!!t)
    );
};
truthTable("x+y") \\ OR
truthTable("x*y") \\ AND

  

You may also check:How to resolve the algorithm Mutual recursion step by step in the Idris programming language
You may also check:How to resolve the algorithm Return multiple values step by step in the Scheme programming language
You may also check:How to resolve the algorithm Remove lines from a file step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Simple windowed application step by step in the Gambas programming language
You may also check:How to resolve the algorithm Hello world/Newbie step by step in the zkl programming language