How to resolve the algorithm Twelve statements step by step in the REXX programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Twelve statements step by step in the REXX programming language

Table of Contents

Problem Statement

This puzzle is borrowed from   math-frolic.blogspot.

Given the following twelve statements, which of them are true?

When you get tired of trying to figure it out in your head, write a program to solve it, and print the correct answer or answers.

Print out a table of near misses, that is, solutions that are contradicted by only a single statement.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Twelve statements step by step in the REXX programming language

Source code in the rexx programming language

/*REXX program solves the  "Twelve Statement Puzzle".                                   */
q=12;      @stmt=right('statement',20)           /*number of statements in the puzzle.  */
m=0                                              /*[↓]  statement one is  TRUE  by fiat.*/
      do pass=1  for 2                           /*find the maximum number of  "trues". */
        do e=0   for 2**(q-1);    n = '1'right( x2b( d2x( e ) ),  q-1,  0)
              do b=1  for q                      /*define various bits in the number  Q.*/
              @.b=substr(n, b, 1)                /*define a particular  @  bit  (in  Q).*/
              end   /*b*/
        if @.1  then if yeses(1,  1)                        \==1  then iterate
        if @.2  then if yeses(7, 12)                        \==3  then iterate
        if @.3  then if yeses(2, 12,2)                      \==2  then iterate
        if @.4  then if yeses(5,  5)   then  if yeses(6, 7) \==2  then iterate
        if @.5  then if yeses(2,  4)                        \==0  then iterate
        if @.6  then if yeses(1, 12,2)                      \==4  then iterate
        if @.7  then if yeses(2,  3)                        \==1  then iterate
        if @.8  then if yeses(7,  7)   then  if yeses(5,6)  \==2  then iterate
        if @.9  then if yeses(1,  6)                        \==3  then iterate
        if @.10 then if yeses(11,12)                        \==2  then iterate
        if @.11 then if yeses(7,  9)                        \==1  then iterate
        if @.12 then if yeses(1, 11)                        \==4  then iterate
        g=yeses(1, 12)
        if pass==1  then do;  m=max(m,g);  iterate;  end
                    else if g\==m     then iterate
            do j=1  for q;             z=substr(n, j, 1)
            if z  then  say @stmt right(j, 2)    " is "    word('false true', 1 + z)
            end   /*tell*/
        end       /*e*/
      end         /*pass*/
exit                                             /*stick a fork in it,  we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
yeses: parse arg L,H,B;  #=0;    do i=L  to H  by word(B 1, 1);  #=#+@.i;  end;   return #


/*REXX program solves the  "Twelve Statement Puzzle".                                   */
q=12;      @stmt=right('statement',20)           /*number of statements in the puzzle.  */
m=0                                              /*[↓]  statement one is  TRUE  by fiat.*/
      do pass=1  for 2                           /*find the maximum number of  "trues". */
        do e=0   for 2**(q-1);    n = '1'right( x2b( d2x( e ) ),  q-1,  0)
              do b=1  for q                      /*define various bits in the number  Q.*/
              @.b=substr(n, b, 1)                /*define a particular  @  bit  (in  Q).*/
              end   /*b*/
        if @.1  then   if \ @.1                             then iterate
        if @.2  then   if @.7+@.8+@.9+@.10+@.11+@.12  \==3  then iterate
        if @.3  then   if @.2+@.4+@.6+@.8+@.10+@.12   \==2  then iterate
        if @.4  then   if @.5  then if \(@.6 & @.7)         then iterate
        if @.5  then   if @.2  |  @.3  |  @.4               then iterate
        if @.6  then   if @.1+@.3+@.5+@.7+@.9+@.11    \==4  then iterate
        if @.7  then   if \ (@.2  &&  @.3 )                 then iterate
        if @.8  then   if @.7  then  if \(@.5 & @.6)        then iterate
        if @.9  then   if @.1+@.2+@.3+@.4+@.5+@.6     \==3  then iterate
        if @.10 then   if \ (@.11 & @.12)                   then iterate
        if @.11 then   if @.7+@.8+@.9                 \==1  then iterate
                          g=@.1 +@.2 +@.3 +@.4 +@.5 +@.6 +@.7 +@.8+ @.9 +@.10 +@.11
        if @.12 then   if g                           \==4  then iterate
        g=g + @.12
        if pass==1  then do;  m=max(m,g);  iterate;  end
                    else if g\==m     then iterate
            do j=1  for q;  z=substr(n, j, 1)
            if z  then  say @stmt right(j, 2)        " is "        word('false true', 1+z)
            end   /*tell*/
        end       /*e*/
      end         /*pass*/                       /*stick a fork in it,  we're all done. */


/*REXX program solves the  "Twelve Statement Puzzle".                                   */
q=12;      @stmt=right('statement',20)           /*number of statements in the puzzle.  */
m=0                                              /*[↓]  statement one is  TRUE  by fiat.*/
      do pass=1  for 2                           /*find the maximum number of  "trues". */
        do e=0   for 2**(q-1);    n = '1'right( x2b( d2x( e ) ),  q-1,  0)
        parse var  n  @1 2 @2 3 @3 4 @4 5 @5 6 @6 7 @7 8 @8 9 @9 10 @10 11 @11 12 @12
/*▒▒▒▒  if @1  then   if \ @1                           then iterate  ▒▒▒▒*/
        if @2  then   if @7+@8+@9+@10+@11+@12  \==3     then iterate
        if @3  then   if @2+@4+@6+@8+@10+@12   \==2     then iterate
        if @4  then   if @5  then if \(@6 & @7)         then iterate
        if @5  then   if @2  |  @3  |  @4               then iterate
        if @6  then   if @1+@3+@5+@7+@9+@11    \==4     then iterate
        if @7  then   if \ (@2  &&  @3 )                then iterate
        if @8  then   if @7  then  if \(@5 & @6)        then iterate
        if @9  then   if @1+@2+@3+@4+@5+@6     \==3     then iterate
        if @10 then   if \ (@11 & @12)                  then iterate
        if @11 then   if @7+@8+@9              \==1     then iterate
                         g=@1 + @2 + @3 + @4 + @5 + @6 + @7 + @8 + @9 + @10 + @11
        if @12 then   if g                     \==4     then iterate
        g=g + @12
        if pass==1  then do;  m=max(m,g);  iterate;  end
                    else if g\==m     then iterate
            do j=1  for q;  z=substr(n, j, 1)
            if z  then say  @stmt right(j, 2)        " is "        word('false true', 1+z)
            end   /*j*/
        end       /*e*/
      end         /*pass*/                       /*stick a fork in it,  we're all done. */


  

You may also check:How to resolve the algorithm Random numbers step by step in the J programming language
You may also check:How to resolve the algorithm Vector step by step in the Java programming language
You may also check:How to resolve the algorithm Binary digits step by step in the Objeck programming language
You may also check:How to resolve the algorithm Amb step by step in the F# programming language
You may also check:How to resolve the algorithm Object serialization step by step in the J programming language