5

Let $a,b$ be nimbers. Is there an efficient algorithm to calculate $a*b$, the nim-product of $a$ and $b$?

The following rule seems like it could be helpful:

$$ 2^{2^m} * 2^{2^n} = \begin{cases} 2^{2^m} 2^{2^n} & \text{if $m \ne n$} \\ 3(2^{2^m - 1}) & \text{if $m = n$} \\ \end{cases}. $$

Juxtaposition denotes ordinary ordinal multiplication here (not nim-multiplication).

Snowball
  • 3,078
  • 18
  • 37
  • 1
    I think, the recursive algorithm should work fine. You can somehow combine Karatsuba algorithm with the representation of a nim number $a$ as $a = bk+c$ where $k=[\log_2\log_2 a]$ where $b,c \leq k.$ – Ehsan M. Kermani Aug 26 '14 at 03:07

2 Answers2

3

Here is an algorithm to multiply nimbers. From now on, $+$ and $\cdot$ will refer to nimber operations, while operations enclosed in brackets will denote the usual math on $\mathbb N$. That is, $2+3=1$, but $[2+3]=5$.

Let $F_n=[2^{2^n}]$. In addition to the rules you stated, that $F_{n}F_{m}=[F_nF_m]$ when $n\neq m$, and $F_n^2=[(3/2)F_n]=F_n+[F_n/2]$, it can also be shown that $$ x<F_n \implies xF_n=[xF_n] $$ That is, the nimber product of a Fermat $2$-power with any number less than itself is equal to the regular product.

Now, onto the algorithm. Given two nimbers $a$ and $b$, you first find natural numbers $m,n$ and nimbers $a_1,a_2,b_1,b_2$ for which $a_1,a_2<F_m$, $b_1,b_2<F_n$, and $$ a=a_1F_m+a_2, \qquad b=b_1F_n+b_2 $$ Explicitly, $F_m$ is the largest Fermat $2$-power less than or equal to $a$, $a_1=[\lfloor a/F_m\rfloor]$, and $a_2=[a\pmod {F_m}]$, and similarly for $b_1$ and $b_2$.

First suppose $m$ and $n$ are unequal, say $m<n$. Then $$ ab=(ab_1)F_n+(ab_2) $$ The nimber products $ab_1$ and $ab_2$ can be computed recursively, and then $(ab_1)$ can be multiplied with $F_n$ in the ordinary sense, since it will be true that $ab_1<F_n$.

So, from now on assume $m=n$. Then \begin{align} ab &=(a_1F_n+a_2)(b_1F_n+b_2) \\&=a_1b_1F_n^2+(a_1b_2+a_2b_1)F_n+a_2b_2 \\&=(a_1b_1+a_1b_2+a_2b_1)F_n+a_2b_2+a_1b_1\cdot[F_n/2] \end{align} Therefore, we can compute $ab$ as follows:

  • Compute $p_1=a_1b_1,p_2=a_2b_2,p_3=(a_1+a_2)(b_1+b_2)$, and $p_4=p_1\cdot [F_n/2]$ using recursive calls to the multiplication function.

  • Compute $p_5=p_3 + p_2$, which is equal to $a_1b_1+a_1b_2+a_2b_1$, and then find the ordinary product of $p_5$ with $F_n$. Finally, the result of this is added to $p_2+p_4$ (in the nim sense).


For the runtime of this algorithm, we start with a product of two numbers with some number of bits, and then we compute this using four recursive calls to multiplication on numbers with half as many bits in their binary representation, along with doing a linear amount of work in terms of additions. That is, if $T(n)$ is the runtime of the algorithm when both $a$ and $b$ have $n$ bits, then $$ T(n)=4T(n/2)+O(n) $$ which implies $T(n)=O(n^2)$.

Mike Earnest
  • 75,930
  • How do we know that $ab_1<F_n$? Also thank you for the book recommendation in my other question. I have borrowed that book from the library now :) – Will Harris Apr 06 '22 at 19:18
  • 1
    Basically, both $a$ and $b$ can be written as a sum of $2$-powers, and each of those two powers can be written as a product of Fermat $2$-powers $F_m$, and all of the $F_m$ are smaller than $F_n$. You can compute $a_1b$ by expanding out this sum of products, which can be simplified by applying the two rules for multiplying Fermat $2$-powers. Since $F_n$ was not present before, and the two rules cannot produce $F_n$, it will never appear, so you can prove the result will still only involve $F_0,F_1,\dots,F_{n-1}$, and therefore be smaller than $F_n$. – Mike Earnest Apr 06 '22 at 19:26
  • 1
    @WillHarris And I hope you enjoy the book! It is one of my favorites. The same Lemma I mentioned also includes a proof of $a<F_n$ and $b<F_n$ implies $ab<F_n$, but starting from the definition of $a\otimes b$ in terms of mex. – Mike Earnest Apr 06 '22 at 19:37
  • Thank you very much! Also just in case anyone from the future is reading this, my other question was https://math.stackexchange.com/questions/4421557/prove-the-following-rule-for-fermat-2-power-nim-multiplication – Will Harris Apr 07 '22 at 11:38
0

An algorithm is given at https://www.ics.uci.edu/~eppstein/numth/ (C++ implementation of J.H.Conway's "nimber" arithmetic.). The function to actually perform the multiplication is at nimber.C:316:nim_times.

Snowball
  • 3,078
  • 18
  • 37