number_theory.html


001| '''Set-based Number Theory.
001| 
001| zero  := {} (null set)
001| one   := { {} }
001| two   := { {{}}, {} }
001| three := { {{{}},{}}, {{}}, {} }
001| ...etc...
001| 
001| So, 5 is:
001|     { {{{{},{{}}},{{}},{}}, {{},{{}}}, {{}}, {}}
001|     , {{{},{{}}}, {{}}, {}}
001|     , {{{}}, {}}
001|     , {{}}
001|     , {}
001|     }
001| 
001| '''

002| ####################################################################################################
003| from sys import argv, stdout
004| from os import path
005| ####################################################################################################
006| BasePath = r'C:\CJS\prj\Python\app\maths'
007| 
008| ##------------------------------------------------------------------------------------------------##
009| '''The only axiomatic object.'''
010| NULL = ()
011| 
012| ##------------------------------------------------------------------------------------------------##
013| '''Premade dictionary of the first six cardinals.'''
014| ExampleCardinals = {
015|     0:(),
016|     1:((),),
017|     2:(((),), ()),
018|     3:((((),),()), ((),), ()),
019|     4:(((((),),()),((),),()), (((),),()), ((),), ()),
020|     5:((((((),),()),((),),()),(((),),()),((),),()), ((((),),()),((),),()), (((),),()), ()),
021| }
022| 
023| ##================================================================================================##
024| def successor (n=None):
025|     '''Integers Successor Function.
025| 
025| The only axiom. Given a number, returns the next number.
025| '''

026|     # If no number given, return axiomatic first number...
027|     if n is None:
028|         return NULL
029|     # Special case: if number is NULL (zero), return ONE...
030|     if len(n) == 0:
031|         return tuple([NULL])
032|     # All other numbers: generate next number...
033|     t = []
034|     a = n
035|     # Build new number by adding all previous numbers...
036|     while 0 < len(a):
037|         t.append(a)
038|         a = a[0]
039|     t.append(NULL)
040|     return tuple(t) # return as tuple
041| 
042| ##================================================================================================##
043| def get_numbers (up_to=10):
044|     '''Return a dictionary of numbers up to N.'''
045|     # Get the axiomatic first number...
046|     curr_number = successor()
047|     # Use it to initialize the set of cardinal numbers...
048|     cardinals = {0:curr_number}
049| 
050|     # Generate numbers through iteration...
051|     for n in range(1, up_to+1):
052|         # Get the next number...
053|         next_number = successor(curr_number)
054|         # Add it to the cardinal numbers set...
055|         cardinals[n] = next_number
056|         # Advance current number...
057|         curr_number = next_number
058| 
059|     # Return the cardinal numbers set...
060|     return cardinals
061| 
062| 
063| ##================================================================================================##
064| def op_add (na, nb):
065|     '''Adder. Returns sum of numbers A and B.'''
066|     _na = na
067|     _nb = nb
068|     while len(_nb):
069|         _na = successor(_na)
070|         _nb = _nb[0]
071|     return _na
072| 
073| ##================================================================================================##
074| def op_sub (na, nb):
075|     '''Subtracter. Returns A-B.'''
076|     _na = na
077|     _nb = nb
078|     while len(_na) and len(_nb):
079|         _na = _na[0]
080|         _nb = _nb[0]
081|     if len(_nb):
082|         raise RuntimeError('Negative subtraction result.')
083|     return _na
084| 
085| ##================================================================================================##
086| def op_mul (na, nb):
087|     '''Multiplier. Returns product of numbers A and B.'''
088|     _np = NULL  # product
089|     _nb = nb
090|     while len(_nb):
091|         _np = op_add(_np, na)
092|         _nb = _nb[0]
093|     return _np
094| 
095| ##================================================================================================##
096| def op_div (na, nb):
097|     '''Divider. Returns dividend and remainder of numbers A and B.'''
098|     if len(nb) == 0:
099|         raise RuntimeError('Division by zero.')
100|     _nd = NULL  # dividend
101|     _nr = na
102|     while len(nb) <= len(_nr):
103|         _nr = op_sub(_nr, nb)
104|         _nd = successor(_nd)
105|     return (_nd, _nr)
106| 
107| 
108| ##================================================================================================##
109| def prn_cardinal_set (nums, stream=stdout):
110|     '''Print Cardinal Number Sets.'''
111| 
112|     print('Cardinal Sets (Numbers):', file=stream)
113|     print(file=stream)
114|     for ix in sorted(nums):
115|         ts = [len(t) for t in nums[ix]]
116|         print('%2d: %s' % (ix, ts), file=stream)
117|     print(file=stream)
118|     print('(Only showing lengths of items in the set.)', file=stream)
119|     print(file=stream)
120| 
121| ##================================================================================================##
122| def prn_number_set (nbr, stream=stdout, wrap=72):
123|     '''Print the Set and all sub-sets of a given Number.'''
124|     cmap = {ord(' '):None, ord('('):'{', ord(')'):'}'}
125| 
126|     prn = str(nbr)
127|     prn = prn.translate(cmap)
128|     # While string is too long, break at commas...
129|     while wrap < len(prn):
130|         sx = prn.find(',', wrap)
131|         if sx < 0:
132|             break
133|         print(prn[0:sx+1], file=stream)
134|         prn = prn[sx+1:]
135|     print(prn, file=stream)
136|     print(file=stream)
137| 
138| ##================================================================================================##
139| def prn_title (title, stream=stdout, line_before=True, line_after=True):
140|     if line_before:
141|         print(file=stream)
142|     print('='*len(title), file=stream)
143|     print(title, file=stream)
144|     print('-'*len(title), file=stream)
145|     if line_after:
146|         print(file=stream)
147| 
148| ##================================================================================================##
149| def test_add_op (nums, stream=stdout):
150|     '''Test adding two Numbers.'''
151|     prn_title('Adding two Numbers:', stream=stream)
152| 
153|     a = nums[5]
154|     b = nums[8]
155|     tot = op_add(a, b)
156|     print('>%2d: %s' % (len(a), [len(t) for t in a]), file=stream)
157|     print('+%2d: %s' % (len(b), [len(t) for t in b]), file=stream)
158|     print('=%2d: %s' % (len(tot), [len(t) for t in tot]), file=stream)
159|     print(file=stream)
160| 
161|     a = nums[5]
162|     b = nums[0]
163|     tot = op_add(a, b)
164|     print('>%2d: %s' % (len(a), [len(t) for t in a]), file=stream)
165|     print('+%2d: %s' % (len(b), [len(t) for t in b]), file=stream)
166|     print('=%2d: %s' % (len(tot), [len(t) for t in tot]), file=stream)
167|     print(file=stream)
168| 
169|     a = nums[0]
170|     b = nums[8]
171|     tot = op_add(a, b)
172|     print('>%2d: %s' % (len(a), [len(t) for t in a]), file=stream)
173|     print('+%2d: %s' % (len(b), [len(t) for t in b]), file=stream)
174|     print('=%2d: %s' % (len(tot), [len(t) for t in tot]), file=stream)
175|     print(file=stream)
176| 
177|     a = nums[0]
178|     b = nums[0]
179|     tot = op_add(a, b)
180|     print('>%2d: %s' % (len(a), [len(t) for t in a]), file=stream)
181|     print('+%2d: %s' % (len(b), [len(t) for t in b]), file=stream)
182|     print('=%2d: %s' % (len(tot), [len(t) for t in tot]), file=stream)
183|     print(file=stream)
184| 
185| ##================================================================================================##
186| def test_sub_op (nums, stream=stdout):
187|     '''Test subtracting two Numbers.'''
188|     prn_title('Subtracting two Numbers:', stream=stream)
189| 
190|     a = nums[12]
191|     b = nums[5]
192|     tot = op_sub(a, b)
193|     print('>%2d: %s' % (len(a), [len(t) for t in a]), file=stream)
194|     print('-%2d: %s' % (len(b), [len(t) for t in b]), file=stream)
195|     print('=%2d: %s' % (len(tot), [len(t) for t in tot]), file=stream)
196|     print(file=stream)
197| 
198|     a = nums[12]
199|     b = nums[0]
200|     tot = op_sub(a, b)
201|     print('>%2d: %s' % (len(a), [len(t) for t in a]), file=stream)
202|     print('-%2d: %s' % (len(b), [len(t) for t in b]), file=stream)
203|     print('=%2d: %s' % (len(tot), [len(t) for t in tot]), file=stream)
204|     print(file=stream)
205| 
206|     a = nums[0]
207|     b = nums[0]
208|     tot = op_sub(a, b)
209|     print('>%2d: %s' % (len(a), [len(t) for t in a]), file=stream)
210|     print('-%2d: %s' % (len(b), [len(t) for t in b]), file=stream)
211|     print('=%2d: %s' % (len(tot), [len(t) for t in tot]), file=stream)
212|     print(file=stream)
213| 
214|     try:
215|         a = nums[5]
216|         b = nums[12]
217|         print('>%2d: %s' % (len(a), [len(t) for t in a]), file=stream)
218|         print('-%2d: %s' % (len(b), [len(t) for t in b]), file=stream)
219|         tot = op_sub(a, b)
220|         print('=%2d: %s' % (len(tot), [len(t) for t in tot]), file=stream)
221|         print(file=stream)
222|     except Exception as e:
223|         print('### %s\n' % e, file=stream)
224| 
225| ##================================================================================================##
226| def test_mul_op (nums, stream=stdout):
227|     '''Test multiplying two Numbers.'''
228|     prn_title('Multiplying two Numbers:', stream=stream)
229| 
230|     a = nums[0]
231|     b = nums[0]
232|     tot = op_mul(a, b)
233|     print('>%2d: %s' % (len(a), [len(t) for t in a]), file=stream)
234|     print('*%2d: %s' % (len(b), [len(t) for t in b]), file=stream)
235|     print('=%2d: %s' % (len(tot), [len(t) for t in tot]), file=stream)
236|     print(file=stream)
237| 
238|     a = nums[0]
239|     b = nums[1]
240|     tot = op_mul(a, b)
241|     print('>%2d: %s' % (len(a), [len(t) for t in a]), file=stream)
242|     print('*%2d: %s' % (len(b), [len(t) for t in b]), file=stream)
243|     print('=%2d: %s' % (len(tot), [len(t) for t in tot]), file=stream)
244|     print(file=stream)
245| 
246|     a = nums[1]
247|     b = nums[0]
248|     tot = op_mul(a, b)
249|     print('>%2d: %s' % (len(a), [len(t) for t in a]), file=stream)
250|     print('*%2d: %s' % (len(b), [len(t) for t in b]), file=stream)
251|     print('=%2d: %s' % (len(tot), [len(t) for t in tot]), file=stream)
252|     print(file=stream)
253| 
254|     a = nums[1]
255|     b = nums[1]
256|     tot = op_mul(a, b)
257|     print('>%2d: %s' % (len(a), [len(t) for t in a]), file=stream)
258|     print('*%2d: %s' % (len(b), [len(t) for t in b]), file=stream)
259|     print('=%2d: %s' % (len(tot), [len(t) for t in tot]), file=stream)
260|     print(file=stream)
261| 
262|     a = nums[2]
263|     b = nums[2]
264|     tot = op_mul(a, b)
265|     print('>%2d: %s' % (len(a), [len(t) for t in a]), file=stream)
266|     print('*%2d: %s' % (len(b), [len(t) for t in b]), file=stream)
267|     print('=%2d: %s' % (len(tot), [len(t) for t in tot]), file=stream)
268|     print(file=stream)
269| 
270|     a = nums[3]
271|     b = nums[6]
272|     tot = op_mul(a, b)
273|     print('>%2d: %s' % (len(a), [len(t) for t in a]), file=stream)
274|     print('*%2d: %s' % (len(b), [len(t) for t in b]), file=stream)
275|     print('=%2d: %s' % (len(tot), [len(t) for t in tot]), file=stream)
276|     print(file=stream)
277| 
278|     a = nums[9]
279|     b = nums[4]
280|     tot = op_mul(a, b)
281|     print('>%2d: %s' % (len(a), [len(t) for t in a]), file=stream)
282|     print('*%2d: %s' % (len(b), [len(t) for t in b]), file=stream)
283|     print('=%2d: %s' % (len(tot), [len(t) for t in tot]), file=stream)
284|     print(file=stream)
285| 
286|     a = nums[19]
287|     b = nums[19]
288|     tot = op_mul(a, b)
289|     print('>%2d: %s' % (len(a), [len(t) for t in a]), file=stream)
290|     print('*%2d: %s' % (len(b), [len(t) for t in b]), file=stream)
291|     print('=%2d: %s' % (len(tot), [len(t) for t in tot]), file=stream)
292|     print(file=stream)
293| 
294| ##================================================================================================##
295| def test_div_op (nums, stream=stdout):
296|     '''Test dividing two Numbers.'''
297|     prn_title('Dividing two Numbers:', stream=stream)
298| 
299|     a = nums[0]
300|     b = nums[5]
301|     d,r = op_div(a, b)
302|     print('>%2d: %s' % (len(a), [len(t) for t in a]), file=stream)
303|     print('/%2d: %s' % (len(b), [len(t) for t in b]), file=stream)
304|     print('=%2d: %s' % (len(d), [len(t) for t in d]), file=stream)
305|     print('=%2d: %s' % (len(r), [len(t) for t in r]), file=stream)
306|     print(file=stream)
307| 
308|     try:
309|         a = nums[10]
310|         b = nums[0]
311|         print('>%2d: %s' % (len(a), [len(t) for t in a]), file=stream)
312|         print('/%2d: %s' % (len(b), [len(t) for t in b]), file=stream)
313|         tot = op_div(a, b)
314|         print('=%2d: %s' % (len(tot), [len(t) for t in tot]), file=stream)
315|         print(file=stream)
316|     except Exception as e:
317|         print('### %s\n' % e, file=stream)
318| 
319|     a = nums[20]
320|     b = nums[5]
321|     d,r = op_div(a, b)
322|     print('>%2d: %s' % (len(a), [len(t) for t in a]), file=stream)
323|     print('/%2d: %s' % (len(b), [len(t) for t in b]), file=stream)
324|     print('=%2d: %s' % (len(d), [len(t) for t in d]), file=stream)
325|     print('=%2d: %s' % (len(r), [len(t) for t in r]), file=stream)
326|     print(file=stream)
327| 
328|     a = nums[20]
329|     b = nums[2]
330|     d,r = op_div(a, b)
331|     print('>%2d: %s' % (len(a), [len(t) for t in a]), file=stream)
332|     print('/%2d: %s' % (len(b), [len(t) for t in b]), file=stream)
333|     print('=%2d: %s' % (len(d), [len(t) for t in d]), file=stream)
334|     print('=%2d: %s' % (len(r), [len(t) for t in r]), file=stream)
335|     print(file=stream)
336| 
337|     a = nums[19]
338|     b = nums[3]
339|     d,r = op_div(a, b)
340|     print('>%2d: %s' % (len(a), [len(t) for t in a]), file=stream)
341|     print('/%2d: %s' % (len(b), [len(t) for t in b]), file=stream)
342|     print('=%2d: %s' % (len(d), [len(t) for t in d]), file=stream)
343|     print('=%2d: %s' % (len(r), [len(t) for t in r]), file=stream)
344|     print(file=stream)
345| 
346|     a = nums[18]
347|     b = nums[5]
348|     d,r = op_div(a, b)
349|     print('>%2d: %s' % (len(a), [len(t) for t in a]), file=stream)
350|     print('/%2d: %s' % (len(b), [len(t) for t in b]), file=stream)
351|     print('=%2d: %s' % (len(d), [len(t) for t in d]), file=stream)
352|     print('=%2d: %s' % (len(r), [len(t) for t in r]), file=stream)
353|     print(file=stream)
354| 
355| 
356| ##================================================================================================##
357| def do_all_the_tests (nums, stream=stdout):
358|     '''Do (all the) tests.'''
359|     # Print the entire set of numbers...
360|     prn_cardinal_set(nums, stream=stream)
361| 
362|     # Display the number ten fully...
363|     num = nums[10]
364|     prn_title('The Set for the Number "%d":' % len(num), stream=stream, line_after=False)
365|     prn_number_set(num, stream=stream, wrap=75)
366| 
367|     # Test operations with numbers...
368|     test_add_op(nums, stream=stream)
369|     test_sub_op(nums, stream=stream)
370|     test_mul_op(nums, stream=stream)
371|     test_div_op(nums, stream=stream)
372|     print(file=stream)
373| 
374| ##================================================================================================##
375| def do_output_console (nums):
376|     '''Do all the tests -- output to the console.'''
377|     do_all_the_tests(nums)
378| 
379| ##================================================================================================##
380| def do_output_file (nums, fname='set_theory_nums.out', fpath=BasePath):
381|     '''Do all the tests -- output to a file.'''
382|     fn = path.join(fpath,fname) if fpath else fname
383|     fp = open(fn, mode='w', encoding='utf8')
384|     try:
385|         do_all_the_tests(nums, fp)
386|     except:
387|         raise
388|     finally:
389|         fp.close()
390|         print('wrote: %s' % fn)
391| 
392| ####################################################################################################
393| if __name__ == '__main__':
394|     print('autorun: %s' % argv[0])
395|     print()
396|     n = int(argv[1]) if 1 < len(argv) else 20
397| 
398|     # Get the numbers from 0 to N...
399|     ns = get_numbers(n)
400| 
401|     do_output_console(ns)
402|     do_output_file(ns)
403| 
404|     print()
405| ####################################################################################################
406| '''eof'''
407|