Created
June 22, 2016 15:12
-
-
Save Noble-Mushtak/89840795c04f75ddaff5338f49359716 to your computer and use it in GitHub Desktop.
Solves Part D and F from https://math.stackexchange.com/q/1835846/307483
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def implies(x, y): | |
'''Tells us if x --> y''' | |
return ((not x) or y) | |
def digits(num, base=10): | |
'''Converts num to an array containing its digits from base specified''' | |
if num < base: return [num] | |
return digits((num-(num % base))//base, base)+[num % base] | |
# Answer for part d | |
d = 0 | |
# Answer for part f | |
f = 0 | |
# Print notice so people can understand messages: | |
print("Correct answers will be printed in the form [a, b, c, d, e, f, g, h]") | |
print("0 represents false, 1 represents true") | |
# Loop through 256 possibilities | |
for i in range(256): | |
# Convert possibilities to binary and add leading 0s to get Booleans | |
bits = digits(i, 2) | |
while len(bits) < 8: bits = [0]+bits | |
# Incremend d if applicable | |
if implies(bits[0], bits[1]) and implies(bits[1], bits[0]) and implies(bits[2], bits[3]) and implies(bits[3], bits[4]) and implies(bits[4], bits[5]) and implies(bits[5], bits[6]) and implies(bits[6], bits[7]): | |
d += 1 | |
# Print the applicable bits | |
print("D is true when we have", bits) | |
# Increment f if applicable | |
if implies(bits[7], bits[0]) and implies(bits[0], (bits[1] and bits[2])) and implies((bits[1] or bits[2]), bits[3]) and implies(bits[0], bits[4]) and implies(bits[4], bits[5]) and implies(bits[5], bits[6]) and implies(bits[6], bits[7]): | |
f += 1 | |
# Print the applicable bits | |
print("F is true when we have", bits) | |
# Print answers | |
print(d, f) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment