How to resolve the algorithm Twelve statements step by step in the Julia programming language
How to resolve the algorithm Twelve statements step by step in the Julia 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 Julia programming language
This code is a program written in Julia that checks a set of propositions against all possible combinations of binary digits.
The function showflaggedbits
takes two bit arrays as input, one representing the binary digits and the other representing which bits are flagged. It returns a string representing the binary digits with the flagged bits marked with an asterisk.
For example, if the binary digits are 10110101
and the flagged bits are 01001001
, the function would return the string 1*11*101*1
.
The main part of the program defines an array of 12 propositions, each of which takes a 12-bit binary string as input and returns a Boolean value indicating whether the proposition is true for that string. The program then iterates over all possible 12-bit binary strings and checks each string against each proposition.
For each string, the program counts the number of propositions that are false for that string. The program then increments the corresponding entry in an array that histograms the number of propositions that are false for each string.
After iterating over all possible strings, the program prints out the histogram, showing the number of strings for which each number of propositions is false.
For example, the following output shows that there are 1024 strings for which all 12 propositions are true, 4096 strings for which 11 propositions are true, and so on:
Distribution of matches
Matches Cases
11 => 1024
10 => 4096
9 => 16384
8 => 65536
7 => 262144
6 => 1048576
5 => 0
4 => 0
3 => 0
2 => 0
1 => 0
0 => 0
Source code in the julia programming language
using Printf
function showflaggedbits{T<:BitArray{1}}(a::T, f::T)
tf = map(x->x ? "T" : "F", a)
flg = map(x->x ? "*" : " ", f)
join(tf .* flg, " ")
end
const props = [s -> length(s) == 12,
s -> sum(s[7:12]) == 3,
s -> sum(s[2:2:end]) == 2,
s -> !s[5] || (s[6] & s[7]),
s -> !any(s[2:4]),
s -> sum(s[1:2:end]) == 4,
s -> s[2] $ s[3],
s -> !s[7] || (s[5] & s[6]),
s -> sum(s[1:6]) == 3,
s -> s[11] & s[12],
s -> sum(s[7:9]) == 1,
s -> sum(s[1:end-1]) == 4]
const NDIG = length(props)
NDIG < WORD_SIZE || println("WARNING, too many propositions!")
mhist = zeros(Int, NDIG+1)
println("Checking the ", NDIG, " statements against all possibilities.\n")
print(" "^15)
for i in 1:NDIG
print(@sprintf "%3d" i)
end
println()
for i in 0:(2^NDIG-1)
s = bitpack(digits(i, 2, NDIG))
t = bitpack([p(s) for p in props])
misses = s$t
mcnt = sum(misses)
mhist[NDIG-mcnt+1] += 1
mcnt < 2 || mcnt == NDIG || continue
if mcnt == 0
print(" Exact Match: ")
elseif mcnt == NDIG
print(" Total Miss: ")
else
print(" Near Miss: ")
end
println(showflaggedbits(t, misses))
end
println()
println("Distribution of matches")
println(" Matches Cases")
for i in (NDIG+1):-1:1
println(@sprintf " %2d => %4d" i-1 mhist[i])
end
You may also check:How to resolve the algorithm Knight's tour step by step in the Clojure programming language
You may also check:How to resolve the algorithm String append step by step in the Robotic programming language
You may also check:How to resolve the algorithm Closures/Value capture step by step in the Haskell programming language
You may also check:How to resolve the algorithm Count in factors step by step in the Run BASIC programming language
You may also check:How to resolve the algorithm Pascal's triangle step by step in the Delphi programming language