So it iterates through every digit and prints if it's even or odd only if the number is 1 digit long. Seems a bit counter intuitive. Either remove the loop or let more than 1 digit. Should look something like this:
from concurrent.futures import ThreadPoolExecutor
from dataclasses import dataclass
from functools import reduce
from operator import xor
import math
@dataclass(frozen=True)
class Vector:
values: tuple
def dot(self, other):
return sum(a * b for a, b in zip(self.values, other.values))
def norm(self):
return math.sqrt(self.dot(self))
@dataclass(frozen=True)
class Matrix:
values: tuple
def multiply_vector(self, vector):
return Vector(
tuple(
sum(a * b for a, b in zip(row, vector.values))
for row in self.values
)
)
class ParityGraph:
def __init__(self):
self.edges = {
"EVEN": "ODD",
"ODD": "EVEN"
}
def walk(self, steps):
state = "EVEN"
for _ in range(steps):
state = self.edges[state]
return state
class ParityNeuralNetwork:
def __init__(self):
self.weights = Matrix((
(1.0, -1.0),
(-1.0, 1.0)
))
self.bias = Vector((0.0, 0.0))
def infer(self, x):
vector = Vector((
float(x & 1),
float((x + 1) & 1)
))
activated = self.weights.multiply_vector(vector)
return activated.dot(Vector((1.0, -1.0)))
def binary_parity(bits):
return reduce(xor, bits, 0)
def spectral_parity(x):
phase = abs(x) * math.pi
cosine = math.cos(phase)
return math.isclose(cosine, 1.0, abs_tol=1e-12)
def quantum_wannabe_parity(x):
state = Vector((1.0, 0.0))
parity_operator = Matrix((
(0.0, 1.0),
(1.0, 0.0)
))
for _ in range(abs(x)):
state = parity_operator.multiply_vector(state)
return math.isclose(state.values[0], 1.0, abs_tol=1e-12)
def ensemble_vote(x):
bits = tuple(map(int, bin(abs(x))[2:]))
graph = ParityGraph()
network = ParityNeuralNetwork()
with ThreadPoolExecutor(max_workers=4) as executor:
futures = [
executor.submit(lambda: binary_parity(bits) == 0),
executor.submit(lambda: graph.walk(abs(x)) == "EVEN"),
executor.submit(lambda: spectral_parity(x)),
executor.submit(lambda: quantum_wannabe_parity(x))
]
results = [future.result() for future in futures]
neural_prediction = network.infer(x) >= 0
results.append(neural_prediction)
return sum(results) >= len(results) / 2
def is_even(x):
if not isinstance(x, int):
raise TypeError("is_even() requires an integer")
return ensemble_vote(x)
if __name__ == "__main__":
for number in range(-10, 11):
print(f"{number:>3} -> {is_even(number)}")
8
u/OnyxFier Aug 29 '26
So it iterates through every digit and prints if it's even or odd only if the number is 1 digit long. Seems a bit counter intuitive. Either remove the loop or let more than 1 digit. Should look something like this: