2018-12-24, post № 208

art, C, programming, #fir, #quine, #tree


Try it online.

Symbolic Closed-Form Fibonacci

2018-12-01, post № 207

Haskell, mathematics, programming, #diagonalization

Theoretical Framework

Let V:=\{(a_j)_{j\in\mathbb{N}}\subset\mathbb{C}|a_n=a_{n-1}+a_{n-2}\forall n>1\} be the two-dimensional complex vector space of sequences adhering to the Fibonacci recurrence relation with basis B:=((0,1,\dots),(1,0,\dots)).
Let furthermore f:V\to V,(a_j)_{j\in\mathbb{N}}\mapsto(a_{j+1})_{j\in\mathbb{N}} be the sequence shift endomorphism represented by the transformation matrix


By iteratively applying the sequence shift a closed-form solution for the standard Fibonacci sequence follows.

Prime Intirety

2018-11-03, post № 206

C, mathematics, programming, #integer, #list, #primes, #representation

Since ancient times humanity knew that there are infinitely many primes — though countable, writing a complete list of every prime is impossible if one intends to finish.
However, in practice one often only considers a minute subset of the naturals to work with and think about. When writing low-level languages like C, one is nearly forced to forget about almost every natural number — the data type u_int_32, for example, is only capable of representing \{\mathbb{N}_0\ni n<2^{32}\}.
Therefore, it is possible to produce a complete list of every prime representable in thirty-two bits using standard bit pattern interpretation — the entirety of the first 𝟤𝟢𝟥 𝟤𝟪𝟢 𝟤𝟤𝟣 primes.

Generating said list took about two minutes on a 4GHz Intel Core i7 using an elementary sieve approach written in C compiled with gcc -O2.
All primes are stored in little-endian format and packed densely together, requiring four bytes each.

Using the resulting file, one can quickly index the primes, for example p_{10^7}=179\,424\,691 = \text{ab1cdb3}_{16} (using zero-based indexing). Since each prime is stored using four bytes, the prime’s index is scaled by a factor of four, resulting in its byte index.

dd status=none ibs=1 count=4 if=primes.bin skip=40000000 | xxd 
00000000: b3cd b10a                                ....

Source code: prime-intirety.c
Prime list (gzipped and split): prime-intirety_primes.bin.gz.parts

Halloween MMXVIII

2018-10-31, post № 205

art, haiku, poetry, #bat

Tiny droplet falls.
A bat’s screech echoes through the cave.
Flowstone grew again.

Conky Clock

2018-10-06, post № 204

art, ASCII, programming, Python, #ASCII art, #time

For a few months now, I have been a vivid user of the ArchLabs distribution which — in a recent release — added the system monitor Conky to display various pieces of information such as uptime, CPU usage and UTC time.

However, Conky does not statically produce a wall of text and plops it on your desktop; it periodically updates itself as to be able to display time-dependent information.
Furthermore, it allows to be fully configured through a simple ~/.config/conky/ArchLabs.conkyrc file.

I wanted to display a useful time-dependent piece of information which does not require user interaction of any kind and found it — an analogue ASCII-art clock.

Time smiley optional.

For installation, download conky-clock.py and add a ${exec python <chosen_path>/conky-clock.py} line to your conky configuration file.

Snippet #2

2018-09-22, post № 203

programming, #shell

cat /dev/urandom > /dev/null

Interpreting brainfuck in C

2018-09-08, post № 202

brainfuck, C, programming, #interpreter

Esoteric programming languages come in an astonishing magnitude of variety — golfing languages, Turing tarpits, obfuscation languages, one-time joke languages and plenty more. However, among all of them, brainfuck is by far one of the most intriguing to me — an elegant combination of syntactic brevity, apparent lack of functionality and the theorectical might of a Turing machine.
Combined with its seemingly trivially realizable implementation, I have implemented brainfuck in Python 2, a brainfuck flavour in Python 2, and even written an interpreter in DrRacket.
However, like Cristofani writes in their The Epistle to the Implementors, writing a satisfactory brainfuck interpreter is no easy task.
Therefore I have designed another brainfuck command-line interpreter, written in pure C (brainfuck.c).

Key features of this implementation are a large tape size, source code pre-processing — instruction condensing and loop matching —, apt command-line flags and C execution speed.
For further detail on the interpreter’s usage, compile the interpreter (e. g. gcc -O2 brainfuck.c -o brainfuck) and run ./brainfuck -h.

To better demonstrate brainfuck’s true power, I wrote a non-Kolmogorov-complexity program; a palindrome tester.

[ A palindrome tester written in brainfuck. ]
[ Jonathan Frech, 21st of August 2018.      ]
[ Tape layout: (STR ... STR NUL FLG ACC)    ]

,[>,]             read in STR
>+                set FLG to true
<<<[              while STR is of length at least two
 [<]>             go to the first byte
 [[>]>>+<<<[<]>-] transfer first byte to ACC
 >[>]<            go to last byte
 [->>>-<<<]       subtract last byte from ACC
 >>>[             if ACC is not zero
  <[-]            set FLG to false
  >-]             clear ACC
 <[<+>-]          move FLG over
 <<<<             go to last byte
]>>>.             output FLG
% echo ",[>,]>+<<<[[<]>[[>]>>+<<<[<]>-]>[>]<[->>>-<<<]>>>[<[-]>-]<[<+>-]<<<<]>>>.\!existence" > palindrome.b
% ./brainfuck -x palindrome.b
00000000: 00                                       .               

% echo ",[>,]>+<<<[[<]>[[>]>>+<<<[<]>-]>[>]<[->>>-<<<]>>>[<[-]>-]<[<+>-]<<<<]>>>.\!hubbibbuh" > palindrome.b
% ./brainfuck -x palindrome.b
00000000: 01                                       .


2018-08-11, post № 201

C, programming, #algorithms, #sorting


Continuing my journey implementing various sorting algorithms in C, in this post I am departing from the most well-known algorithms and implementing one of my personal favourites — heapsort.
Download the C source file here: heapsort.c.

Contrary to algorithms like quicksort which immediately jump into action sorting the given array, heapsort operates on a data structure called a heap which it efficiently transforms into a sorted list. However, as most arrays are not of the heap structure, heapsort first needs to transform a given array into a heap. Thus heapsort is a two-step process — first creating a heap and then sorting said heap.

Sorting can be applied to an infinite number of objects provided there is an order defined among them. However, not much is gained from changing the underlying value one is sorting which is why in this post I will only focus on sorting integers — technically even only integers in the C type sense; n\in\mathbb{Z},-2^{31} \leq n < 2^{31}.

The Heap

A heap is a special type of binary tree that satisfies the heap property — every parent node’s value is not less than its child node’s (if existent) values. Furthermore, a heap is maximally filled at every level but possibly the last where the elements are as far left as possible.
From these properties it follows that the greatest value will be located at the root node.

One can also define a heap such that the root node will house the smallest element; such a heap would lead to a list sorted in descending order (more on that later).

      ( 86 )      
     /      \     
  (31)      (64)  
  /  \      /     
(20)(-4)  (17)    

A heap containing integers.
Jonathan Frech's blog; built 2024/07/06 12:42:51 CEST