Truth Table for n Variables

> restart:

> iff:=proc(p,q)
    (p implies q) and (q implies p);

end proc:

Enter the number of logically independent propositional variables after n:=

> n:=3;

Enter the compound proposition to be analyzed after f:    For connectives use: not, and, or, implies.  For   variables use p1, p2, . . . , pn.  You may also use iff as a connective by writing p iff q as iff(p,q).

> f:=((p1 implies p2) and (p2 implies p3) and (p3 implies p1)) implies ((p1 implies p3) and (p2 implies p3) and (p3 implies p1));

n := 3

f := (p1 implies p2) and (p2 implies p3) and (p3 implies p1) implies (p1 implies p3) and (p2 implies p3) and (p3 implies p1)

> ;

> TT:=Array(1..1+2^n,1..1+n):  #Create an array of the appropriate dimensions for the truth-table.

> for j from 1 to n do    #Title the truth-table.
    TT[1,j]:=cat("p",convert(j,string))

end do:

> prop:=convert(f,string):  

> if length(prop)>115
    then TT[1,n+1]:="f"

    else TT[1,n+1]:=prop

end if:

> for j from 1 to n do     #For all the independent variable entries in the array enter false.
    for i from 2 to 2^n+1 do

           TT[i,j]:=false

    end do

end do:

> for j from 1 to n do     #In each column enter the initial block of T's.
    for i from 2 to 2^n+1 do

          if i-1<=2^(n-j) then TT[i,j]:=true

    end if

    end do

end do:          

> for j from 1 to n do   #Extend the initial block of T's & F's to the rest of the column.
    for i from 2+2^(n-j+1) to 1+2^n do

            for k from 2 to 1+2^(n-j) do

                  if  i mod 2^(n-j+1) = k mod 2^(n-j+1) then TT[i,j]:=TT[k,j]

                  end if

            end do

    end do

end do:         

> for i from 2 to 1+2^n do     #On each row evaluate the independent variables, then evaluate f
    for j from 1 to n do

         p||j:=TT[i,j]

    end do:

    TT[i,n+1]:=evalb(f)

end do:

> if length(prop)>115 then print("f = "||prop) end if; TT;

Array([[

If n>3, then a small rectangle appears in lieu of the truth-table.  Left-click on this rectangle to view the truth-table as a spread-sheet.

> f:='f':

>