Arithmetic Golfing

A recent PCG golfing question When do I get my sandwich? asked to find a mapping between seven input strings (sandwich names) and the seven days of the week (indexed by number).

The first answer was made by a user named i cri everytim and utilized a string of characters which uniquely appear at the same position in all seven input strings, enklact, to perform the mapping in Python 2 requiring 29 bytes. After their answer, a lot of answers appeared using the same magic string in different languages to reduce the number of bytes needed. Yet nobody reduced the byte count in Python.

Trying to solve the problem on my own, my first attempt was using only the input strings’ last decimal digit to perform the mapping, though this approach did not save on bytes (read my PCG answer for more on this 30 byte solution).

After a few more hours of working on this problem, however, I achieved to bring down the byte count by one entire byte.

I did so by using a simple brute-force algorithm to check for Python expressions which can be used to perform the sought after mapping. To do so, I use Python’s apostrophes (`...`) to turn the found expression into a string — str(...) is three whole bytes longer — and index that string with the input strings’ lengths. It sure is not very readable, but only takes 28 bytes — and that is all that matters.

lambda S:`6793**164`[len(S)]

After finding the 28 byte function which uses a 9 byte expression (6793**164), I attempted to find an even shorter expression. And even though I did not yet find one, I did write a more general brute-force Python program (source code shown below; can also be downloaded) than the one I linked to in my PCG answer.

Brute-forcing takes exponentially more time the more digits you have to check, so my brute-forcer still requires the user to decide for themselves which expressions should be tried.
There are three parameters that define the search; a regex pattern that should be contained in the expression’s string, an offset that pattern should ideally have and a target length. If an expression is found that takes as many bytes as or less bytes than the target length, an exclamation point is printed.
Though this program did not prove useful in this case, there may come another challenge where an arithmetic expression golfer could come in handy.

My program may not have found shorter expressions, but definitely some impressive ones (the +... at the end refers to an additional offset from the string index which — unsurprisingly — take additional bytes):

  • 2**2**24+800415
  • 2**2**27+5226528
  • 2**7**9+11719750
  • 7954<<850

I also considered using division to generate long strings of digits which may match; the only problem is that Python floating-point numbers only have a certain precision which does not produce long enough strings. Again, using exponentiation (**) and bitshifting (<<) I could not come up with a working expression that takes less bytes.

# Python 2.7 code; 7th, 8th of September 2017

Continue reading



The On-Line Encyclopedia of Integer Sequences gets regularly updated with new integer sequences. One of the recent updates was contributed by me, A285494.

A285494 is the list of all numbers k so that its digit sum equals its number of distinct prime factors.
A number’s digit sum is the sum of all of its decimal digits. The number 62831853, for example, has a digit sum of 6+2+8+3+1+8+5+3 = 36.
A number’s number of distinct prime factors is the number of different prime numbers that multiply together to result in the original number. As an example, 62831853 = 3^2 \cdot 7 \cdot 127 \cdot 7853, so it has five prime factors of which four are distinct.
Thereby one can conclude that 62831853 is not an entry in this sequence, as 36 \neq 4.

The sequence is certainly infinite, as the number k = 2 \cdot 10^n with n \in \mathbb{N}^* has a digit sum of 2 + (0 \cdot n) = 2 and — because k = 2^{n+1} \cdot 5^n — exactly two distinct prime factors.

In the encyclopedia entry, I provided a Mathematica one-liner to compute the first few entries of this sequence. Since then, I have also written a Python two-liner to achieve the same goal.

(* Mathematica *)
Out = {20, 30, 102, 120, 200, 300, 1002, 1200, 2000, 2001, 2002, 3000, 3010}
# Python 2.7
>>> def p(n):exec"i,f=2,set()\nwhile n>1:\n\tif n%i==0:f.add(i);n/=i;i=1\n\ti+=1";return len(f)
>>> print filter(lambda n:p(n)==sum(map(int,str(n))),range(2,10001))
[20, 30, 102, 120, 200, 300, 1002, 1200, 2000, 2001, 2002, 3000, 3010]

Multibrot Set

The Mandelbrot Set is typically defined as the set of all numbers c \in \mathbb{C} for which — with z_0 = 0, z_{n+1} = f_c(z_n) and f_c(z) = z^2 + c — the limit \lim\limits_{n \to \infty} z_n converges. Visualizations of this standard Mandelbrot Set can be seen in three of my posts (Mandelbrot Set, Mandelbrot Set Miscalculations and Mandelbrot Set II).

f_c(z)=z^2+cHowever, one can extend the fractal’s definition beyond only having the exponent 2 in the function to be f_c(z)=z^\text{exp}+c with \text{exp} \in \mathbb{R}. The third post I mentioned actually has some generalization as it allows for \text{exp} \in \{2,3,4,5\}, although the approach used cannot be extended to real or even rational numbers.

f_c(z)=z^3+cThe method I used in the aforementioned post consists of manually expanding (a+b\cdot i)^n for each n. The polynomial (a+b\cdot i)^3, for example, would be expanded to (a^3 - 3 \cdot a \cdot b^2) + (3 \cdot a^2 \cdot b - b^3) \cdot i.
This method is not only tedious, error-prone and has to be done for every exponent (of which there are many), it also only works for whole-number exponents. To visualize real Multibrots, I had to come up with an algorithm for complex number exponentiation.

f_c(z)=z^4+cLuckily enough, there are two main ways to represent a complex number, Cartesian form z = a+b\cdot i and polar form z = k\cdot e^{\alpha\cdot i}. Converting from Cartesian to polar form is simply done by finding the number’s vector’s magnitude k = \sqrt{a^2+b^2} and its angle to the x-axis \alpha = \mbox{atan2}(\frac{a}{b}). (The function \mbox{atan2} is used in favor of \arctan to avoid having to divide by zero. View this Wikipedia article for more on the function and its definition.)
Once having converted the number to polar form, exponentiation becomes easy as z^\text{exp} = (k \cdot e^{\alpha\cdot i})^\text{exp} = k^\text{exp} \cdot e^{\alpha \cdot \text{exp} \cdot i}. With the exponentiated z^\text{exp} in polar form, it can be converted back in Cartesian form with z^\text{exp} = k^\text{exp} \cdot (\cos{(\alpha \cdot \text{exp})} + \sin{(\alpha \cdot \text{exp})} \cdot i \big).

f_c(z)=z^5+cUsing this method, converting the complex number to perform exponentiation, I wrote a Java program which visualizes the Multibrot for a given range of exponents and a number of frames.
Additionally, I added a new strategy for coloring the Multibrot Set, which consists of choosing a few anchor colors and then linearly interpolating the red, green and blue values. The resulting images have a reproducible (in contrast to randomly choosing colors) and more interesting (in contrast to only varying brightness) look.

f_c(z)=z^6+cThe family of Multibrot Sets can also be visualized as an animation, showing the fractal with an increasing exponent. The animated gif shown below was created using ImageMagick’s convert -delay <ms> *.png multibrot.gif command to stitch together the various .png files the Java application creates. To speed up the rendering, a separate thread is created for each frame, often resulting in 100% CPU-usage. (Be aware of this should you render your own Multibrot Sets!)

f_c(z)=z^10+cTo use the program on your own, either copy the source code listed below or download the .java file. The sections to change parameters or the color palette are clearly highlighted using block comments (simply search for ‘/*’).
To compile and execute the Java application, run (on Linux or MacOS) the command javac; java -Xmx4096m multibrot in the source code’s directory (-Xmx4096m tag optional, though for many frames at high quality it may be necessary as it allows Java to use more memory).
If you are a sole Windows user, I recommend installing the Windows 10 Bash Shell.

Multibrot animation (probably loading...)

// Java 1.8 Code
// Jonathan Frech, 11th of September 2016
//          edited 17th of April     2017
//          edited 18th of April     2017
//          edited 20th of April     2017
//          edited 21st of April     2017
//          edited 22nd of April     2017

Continue reading