jblog
toc

Tau Day MMXVII

2017-06-28, post № 173

C, mathematics, programming, #constant

Today it is June the 28th which means that it is 𝜏 day!
The irrational and transcendental constant 𝜏 is what defines \pi=\frac{\tau}{2}, which obviously makes it an important constant. To celebrate this day, I created a C program which calculates 𝜏 by randomly creating 𝟫-dimensional points inside the 𝟫-dimensional hypercube and testing if they are inside the 𝟫-dimensional hypersphere with its center located at (0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5) [1].

Today’s 𝜏 time is 3:18:53 as \tau=6.2831853\dots. As one does not know if the time is specified as ante or post meridiem, there are actually two perfectly acceptable 𝜏 times.

            ;b$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$b
        h$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 
     .$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$+ 
    $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$  
  ,$$$$$$$$$kn}~I"`($$$$$$$x````````````````   
 )$$$m             $$$$$$$$                    
+$$c              q$$$$$$$>                    
$$                $$$$$$$$                     
                 $$$$$$$$.                     
                '$$$$$$$$                      
                $$$$$$$$                       
               ?$$$$$$$a                       
               $$$$$$$$                        
              Q$$$$$$$f                        
              $$$$$$$$                         
             $$$$$$$$i                         
             $$$$$$$$                          
            $$$$$$$$;                          
            $$$$$$$$                           
           J$$$$$$$w                           
           $$$$$$$$;             $$~           
           $$$$$$$$             $$$            
          '$$$$$$$$C          "$$$             
           $$$$$$$$$w       '$$$$              
           $$$$$$$$$$$$$$$$$$$$B               
            $$$$$$$$$$$$$$$$$$.                
             $$$$$$$$$$$$$$$'                  
                8$$$$$$$q                      

The formula used for calculating 𝜏 is derived from a 𝟫-dimensional hypersphere’s hypervolume formula V=\frac{32\cdot\pi^4}{945}\cdot R^9 (see this Wikipedia article).

\begin{cases}
    V = \frac{2^5 \cdot \tau^4 \cdot R^9}{945 \cdot 2^4} = \frac{2 \cdot R^9}{945} \cdot \tau^4\\
    \tau^4 = \frac{V \cdot 945}{2 \cdot R^9}; R = 0.5\\
    \tau^4 = \frac{V \cdot 945 \cdot 2^9}{2}\\
    \tau = \sqrt[4]{V \cdot 241920}
\end{cases}

The constant gets calculated to \tau^*=6.293700. The real value is approximately 𝜏 = 𝟨.𝟤𝟪𝟥𝟣𝟪𝟧…, which makes the percent error

\left|\frac{\tau^*-\tau}{\tau}\right|=\left|\frac{6.293700-6.283185}{6.283185}\right|=0.001673514 = 0.167\%.

Thereby, this C program’s approximation is not too far off. [2] The source code is listed below and can also be downloaded here. Instructions on how to compile it using GCC can be seen below or in the source code.

gcc tau.c -o tau -lm; ./tau
tau = 6.293700

Resources worth checking out regarding 𝜏 are The Tau Manifesto and 2 Pi or Not 2 Pi? I wish everybody a happy 𝜏 day.

Source code: tau-day-mmxvii.c

Mandelbrot Set ASCII Viewer

2017-06-17, post № 172

ascii, HTML, JavaScript, mathematics, programming, Python, #animation, #fractal

The Mandelbrot Set is the set of all complex points which, when one iteratively and infinitely applies the function f_c(z)=z^2+c, converge to a value. This simple rule results in stunning complexity and beauty.
Many Mandelbrot Set animations use regularly colored pixels to represent the number of iterations needed at the fractal’s edges to escape converging. Yet this mathematical object can also be represented as ASCII characters — similar to what I did in my Curses Cam post. The characters are chosen according to their opaqueness. A full stop (‘.’) looks lighter than a dollar sign (‘$’), so they represent a smaller or larger number of iterations needed. The order of characters used is taken from this post by Paul Bourke.
As there are only 𝟩𝟢 characters used, each frame is being rendered twice to determine the minimum number of iterations needed by every point in that frame. Thereby the full visual character range is used.

The characters shown below represent a Mandelbrot Set still. To see the zoom in action, either run the program (listed below) or take a look at this Mandelbrot Set ASCII journey.

      ..................''''''''``"">>II``''''......                          
    ..................''''''''``^^,,ii::^^``''''......                        
  ..................''''''''``^^::ww$$++,,````''''......                      
................''''''''``^^^^""::$$$$$$::""^^``''''......                    
..............''''''````""{{;;XX$$$$$$$$uuUU,,,,""''......                    
............''''``````^^,,rr$$$$$$$$$$$$$$$$<<$$--``........                  
........''``````````^^""LL$$$$$$$$$$$$$$$$$$$$__""``''......                  
..''''''^^!!"""",,""""::__$$$$$$$$$$$$$$$$$$$$$$ll""''........                
''''````^^::__IIYYii::ll$$$$$$$$$$$$$$$$$$$$$$$$pp^^''........                
''``````"";;[[$$$$$$++__$$$$$$$$$$$$$$$$$$$$$$$$$$^^''''......                
``^^^^,,;;>>$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ww``''''......                
"",,,,II$$nn$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$""``''''......                
"",,,,II$$nn$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$""``''''......                
``^^^^,,;;>>$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ww``''''......                
''``````"";;[[$$$$$$++__$$$$$$$$$$$$$$$$$$$$$$$$$$^^''''......                
''''````^^::__IIYYii::ll$$$$$$$$$$$$$$$$$$$$$$$$pp^^''........                
..''''''^^!!"""",,""""::__$$$$$$$$$$$$$$$$$$$$$$ll""''........                
........''``````````^^""LL$$$$$$$$$$$$$$$$$$$$__""``''......                  
............''''``````^^,,rr$$$$$$$$$$$$$$$$<<$$--``........                  
..............''''''````""{{;;XX$$$$$$$$uuUU,,,,""''......                    
................''''''''``^^^^""::$$$$$$::""^^``''''......                    
  ..................''''''''``^^::ww$$++,,````''''......                      
    ..................''''''''``^^,,ii::^^``''''......                        

The fractal viewer is written in Python 2.7 and works by determining the terminal’s size and then printing a string of according size. This creates the illusion of a moving image, as the terminal will hopefully always perfectly scroll so that only one frame is visible at a time. In the code’s first non-comment line one can change the complex point at the image’s center, (really, its conjugate, which is partially irrelevant as the set is symmetric along the real axis) the initial zoom value (complex distance above the image’s center), the zoom factor (the factor by which the zoom value gets multiplied after a frame), the total number of frames (- 𝟣 means there is no upper limit), the delay between frames (in seconds, can be floating-point) and the color characters used.

The program’s source code may not be particularly easy to read, yet it does its job and only requires seven non-comment lines! The code is shown below, though the .py file can also be downloaded.
To achieve the JavaScript animation linked to above, I wrote a simple Python converter which takes in the fractal renderer’s output and it spits out an HTML page. This converter’s code is not listed, though the .py file can be downloaded. Instructions on how to use the converter can be seen in its source code.

mandelbrot-set-ascii-viewer.py; Python2, 593 bytes, 8 lines
# Python 2.7 Code; Jonathan Frech, 15th and 16th of June 2017
P,Z,F,N,D,K=-.707+.353j,3,.9,-1,.1," .'`^\",:;Il!i><~+_-?][}{1)(|\\/tfjrxnuvczXYUJCLQ0OZmwqpdbkhao*#MW&8%B@$"
import os,time,sys;H,W,S,n=map(int,os.popen("stty size").read().split())+[sys.stdout,0];W/=2
def C(c):
	global m;z,i=0j,-1
	while abs(z)<=2 and i<len(K)-1+M:z,i=z*z+c,i+1
	m=min(m,i);return K[i-M]*2
while n<N or N==-1:h=Z*2.;w=h*W/H;R=lambda:"\n\n"*(n!=0)+"\n".join("".join(C(P-complex(w/2-w*x/W,h/2-h*y/H))for x in range(W))for y in range(H));M,m=0,len(K);R();M=max(M,m);S.write(R());S.flush();Z,n=Z*F,n+1;time.sleep(D)

A285494

2017-06-03, post № 171

mathematics, OEIS, programming, Python, Wolfram Language, #decimal, #decimal sum, #digit sum, #distinct prime, #int, #integer, #number, #number theory, #prime, #prime factors, #primes

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 𝑘 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 𝟨𝟤𝟪𝟥𝟣𝟪𝟧𝟥, for example, has a digit sum of 𝟨 + 𝟤 + 𝟪 + 𝟥 + 𝟣 + 𝟪 + 𝟧 + 𝟥 = 𝟥𝟨.
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 𝟨𝟤𝟪𝟥𝟣𝟪𝟧𝟥 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 *)
Select[Range[2,10000],Total[IntegerDigits[#]]==Length[FactorInteger[#]]&]
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]
Jonathan Frech's blog; built 2021/04/16 20:21:20 CEST