Arithmetic Algorithms for Hereditarily Binary Natural Numbers
Total Page:16
File Type:pdf, Size:1020Kb
Arithmetic Algorithms for Hereditarily Binary Natural Numbers Paul Tarau Department of Computer Science and Engineering University of North Texas [email protected] Abstract and future cryptographic systems might also justify devising al- We study some essential arithmetic properties of a new tree-based ternative numbering systems with higher limits on the size of the number representation, hereditarily binary numbers, defined by numbers with which we can perform tractable computations. applying recursively run-length encoding of bijective base-2 digits. While notations like Knuth’s “up-arrow” [1] or tetration are use- Our representation expresses giant numbers like the largest ful in describing very large numbers, they do not provide the abil- known prime number and its related perfect number as well as ity to actually compute with them – as, for instance, addition or the largest known Woodall, Cullen, Proth, Sophie Germain and multiplication with a natural number results in a number that can- twin primes as trees of small sizes. not be expressed with the notation anymore. More exotic notations More importantly, our number representation supports novel like Conway’s surreal numbers [2] involve uncountable cardinali- algorithms that, in the best case, collapse the complexity of various ties (they contain real numbers as a subset) and are more useful for computations by super-exponential factors and in the worse case modeling game-theoretical algorithms rather than common arith- are within a constant factor of their traditional counterparts. metic computations. As a result, it opens the door to a new world, where arith- The novel contribution of this paper is a tree-based numbering metic operations are limited by the structural complexity of their system that allows computations with numbers comparable in size operands, rather than their bitsizes. with Knuth’s “arrow-up” notation. Moreover, these computations have a worse case complexity that is comparable with the tradi- Categories and Subject Descriptors D.3.3 [PROGRAMMING tional binary numbers, while their best case complexity outper- LANGUAGES]: Language Constructs and Features—Data types forms binary numbers by an arbitrary tower of exponents factor. and structures Simple operations like successor, multiplication by 2, exponent of 2 are practically constant time and a number of other operations General Terms Algorithms, Languages, Theory benefit from significant complexity reductions. Keywords arithmetic computations with giant numbers, heredi- For the curious reader, it is basically a hereditary number sys- tary numbering systems, declarative specification of algorithms, tem [3], based on recursively applied run-length compression of a compressed number representations, compact representation of special (bijective) binary digit notation. large primes A concept of structural complexity is introduced, based on the size of our tree representations and it is shown that several “record holder” large numbers like Mersenne, Cullen, Woodall and Proth 1. Introduction primes have unusually small structural complexities. Number representations have evolved over time from the unary We have adopted a literate programming style, i.e. the code “cave man” representation where one scratch on the wall repre- contained in the paper forms a self-contained Haskell module sented a unit, to the base-n (and in particular base-2) number sys- (tested with ghc 7.6.3), also available as a separate file at http: arXiv:1306.1128v1 [cs.DS] 5 Jun 2013 tem, with the remarkable benefit of a logarithmic representation //logic.cse.unt.edu/tarau/research/2013/hbin.hs . Al- size. Over the last 1000 years, this base-n representation has proved ternatively, a Scala package implementing the same tree-based to be unusually resilient, partly because all practical computations computations is available from http://code.google.com/p/ could be performed with reasonable efficiency within the notation. giant-numbers/. We hope that this will encourage the reader to However, when thinking ahead for the next 1000 years, com- experiment interactively and validate the technical correctness of putations with very large numbers are likely to become more and our claims. The Appendix contains a quick overview of the subset more “natural”, even if for now, they are mostly driven by purely of Haskell we are using as our executable function notation. theoretical interests in fields like number theory, computability or The paper is organized as follows. Section 2 gives some back- multiverse cosmology. Arguably, more practical needs of present ground on bijective base-2 numbers and iterated function applica- tions. Section 3 introduces hereditarily binary numbers. Section 4 describes practically constant time successor and predecessor oper- ations on tree-represented numbers. Section 5 shows an emulation of bijective base-2 with hereditarily binary numbers and section 6 Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies are not made or distributed describes novel algorithms for arithmetic operations taking advan- for profit or commercial advantage and that copies bear this notice and the full citation tage of our number representation. Section 7 defines a concept of on the first page. To copy otherwise, to republish, to post on servers or to redistribute structural complexity and studies best and worse cases. Section 8 to lists, requires prior specific permission and/or a fee. describes efficient tree-representations of some important number- theoretical entities like Mersenne, Fermat, Proth, Woodall primes. Copyright c ACM [to be supplied]. $15.00 Section 10 discusses related work. Section 11 concludes the paper DEFINITION 2. We call two functions f; g conjugates through h if and discusses future work. h is a bijection such that g = h−1 ◦f ◦h, where ◦ denotes function composition. 2. Natural numbers as iterated function n n PROPOSITION 3. If f; g are conjugates through h then f and g applications are too, i.e. gn = h−1 ◦ f n ◦ h. Natural numbers can be seen as represented by iterated applications of the functions o(x) = 2x + 1 and i(x) = 2x + 2 corresponding Proof By induction, using the fact that h−1 ◦h = λx.x = h◦h−1. the so called bijective base-2 representation [4] together with the n n convention that 0 is represented as the empty sequence. As each PROPOSITION 4. o and i are conjugates with respect to s and 0 n 2 N can be seen as a unique composition of these functions we s , i.e. the following 2 identities hold: can make this precise as follows: DEFINITION 1. We call bijective base-2 representation of n 2 N n n 0 the unique sequence of applications of functions o and i to that o (k) = s(i (s (k))) (11) evaluates to n. With this representation, and denoting the empty sequence , one in(k) = s0(on(s(k))) (12) obtains 0 = , 1 = o , 2 = i , 3 = o(o ); 4 = i(o ); 5 = o(i ) etc. and the following holds: Proof An immediate consequence of i = s ◦ o (by 1) and Prop. 3. n i(x) = o(x) + 1 (1) Note also that proposition 1 can be seen as stating that o is the conjugate of the leftshift operation l(n; x) = 2nx through 2.1 Properties of the iterated functions on and in s(x) = x + 1 (eq. 2) and so is in through s ◦ s (eq. 3). n The following equations relate successor and predecessor to the PROPOSITION 1. Let f denote application of function f n times. Let o(x) = 2x + 1 and i(x) = 2x + 2, s(x) = x + 1 and iterated applications of o and i: 0 n 0 n s (x) = x − 1. Then k > 0 ) s(o (s (k)) = k2 and 0 k > 1 ) s(s(in(s0(s0(k)))) = k2n. In particular, s(on(0)) = 2n s(on(k)) = i(os (n)(k)) (13) and s(s(in(0))) = 2n+1. Proof By induction. Observe that for n = 0; k > 0; s(o0(s0(k)) = s(in(k)) = on(s(k)) (14) k20 because s(s0(k))) = k. Suppose that P (n): k > 0 ) s(on(s0(k))) = k2n holds. Then, assuming k > 0, P(n+1) follows, 0 n n 0 given that s(on+1(s0(k))) = s(on(o(s0(k)))) = s(on(s0(2k))) = s (o (k)) = i (s (k)) (15) 2k2n = k2n+1. Similarly, the second part of the proposition also follows by induction on n. 0 s0(in(k)) = o(is (n)(k)) (16) The underlying arithmetic identities are: By setting k = 2m + 1 in eq. 2 we obtain: n n k > 0 ) 1 + o (k − 1) = 2 k (2) n n 1 + o (2m) = 2 (2m + 1) (17) n n As the right side of this equation expresses a bijection between k > 1 ) 2 + i (k − 2) = 2 k (3) + N × N and N , so does the left side, i.e. the function c(m; n) = n + from where one can deduce 1 + o (2m) maps pairs (m,n) to unique values in N . on(k) = 2n(k + 1) − 1 (4) Similarly, by setting k = 2m + 1 in eq. 3 we obtain: n n in(k) = 2n(k + 2) − 2 (5) 2 + i (2m − 1) = 2 (2m + 1) (18) and in particular on(0) = 2n − 1 (6) 3. Hereditarily binary numbers in(0) = 2n+1 − 2 (7) 3.1 Hereditary Number Systems Also, one can directly relate ok and ik Let us observe that conventional number systems, as well as the bi- n n n jective base-2 numeration system described so far, represent blocks o (k) + 2 = i (k) + 1 (8) of 0 and 1 digits somewhat naively - one digit for each element of in(k) = on(k) + on(0) (9) the block. Alternatively, one might think that counting them and binary numbers n n representing the resulting counters as would be o (k + 1) = i (k) + 1 (10) also possible.