Last active
May 6, 2019 03:37
-
-
Save yhay81/f5dc78ed04e203932793da36c24dd320 to your computer and use it in GitHub Desktop.
Pythonで算術入門 ref: https://qiita.com/yhay81/items/de74be5abbb09f872183
This file contains hidden or 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
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 |
This file contains hidden or 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 __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 |
This file contains hidden or 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
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