Skip to content

Instantly share code, notes, and snippets.

@yhay81
Last active May 6, 2019 03:37
Show Gist options
  • Save yhay81/f5dc78ed04e203932793da36c24dd320 to your computer and use it in GitHub Desktop.
Save yhay81/f5dc78ed04e203932793da36c24dd320 to your computer and use it in GitHub Desktop.
class NaturalNumber:
def __init__(self, p=None):
self.predecessor = p
def __eq__(self, x):
if self.predecessor is None and x.predecessor is None:
return True
elif (self.predecessor is None and x.predecessor is not None) \
or (self.predecessor is not None and x.predecessor is None):
return False
else:
return self.predecessor == x.predecessor
def __add__(self, x):
if x == N_ZERO:
return self
else:
return NaturalNumber(self + x.predecessor)
def __mul__(self, x):
if x == N_ZERO:
return N_ZERO
else:
return self + self * x.predecessor
git clone https://github.com/yhay81/pythonic-algebra.git
cd pythonic-algebra
python -m unittest tests/test_natural_number.py
python -m unittest tests/test_integer.py
python -m unittest tests/test_rational.py
python
>>>from numbers.natural_number import *
>>>one = NaturalNumber(NaturalNumber())
>>>two = one + one
>>>three = two + one
>>>four = two * two
>>>n256 = four ** four
>>>x = n256 % three
>>>x == one
>>>for n in four:
>>> print(n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment