Math, Bits, Magic, and a Bit of Abnormal C Programming | alexeev-dev notes
Math, Bits, Magic, and a Bit of Abnormal C Programming
16 May, 2026
Good day, ladies and gentlemen! Sometimes some people get the urge to engage in outright indecency in programming — things that don't bring direct practical benefit but help to have fun. And I am no exception. In this article, I want to tell you about lifehacks, tricks (magical and not so magical), and algorithms in the C language!
The idea to write this article came from my post. In it, I explained how the Fibonacci sequence can convert miles to kilometers with a small margin of error. Seeing that many people liked it, I thought: why not explore some other tricks while practicing C programming?
Everyone who's interested — please follow me under the cut.
Since I've already started talking about Fibonacci, let me quote a bit from my post:
The Fibonacci sequence can convert miles to kilometers with a small error. 5 miles ≈ 8 km (5 and 8 are Fibonacci numbers). Reality: 5 miles = 8.04672 km.<br>Why? 1 mile = 1.609344 kilometers (exact value). Golden ratio (φ) ≈ 1.618034<br>The error arises because the ratio Fₙ₊₁ / Fₙ tends toward φ ≈ 1.618034, while the exact mile/km ratio = 1.609344.<br>Relative error: (1.618034 — 1.609344) / 1.609344 * 100% ≈ 0.54%.
Fun fact: if you take a cube and a sphere of the same volume, the radius of the sphere in miles equals the edge of the cube in kilometers. Because the cube root of (4/3)pi is exactly 1.61. © Enfriz
Let's take a look at how to implement such a converter in C.
So, let's say we have this function:
uint64_t fibonacci(int num) {<br>if (num 0)<br>return 0;<br>if (num == 0)<br>return 0;
uint64_t a = 0;<br>uint64_t b = 1;
if (num == 1)<br>return b;
for (int i = 2; i num; i++) {<br>uint64_t next = a + b;<br>a = b;<br>b = next;
return b;
I think you all know how this algorithm works. The only thing worth clarifying is that we must pass the number of miles incremented by 1 as an argument — meaning, to convert 5 miles to kilometers, we pass 5+1=6 (the next number) and get 8.
Also, we can do the calculation based on the golden ratio — since it is also connected to the Fibonacci sequence.
float fib_golden_ratio(float miles) {<br>const double PHI = (1.0 + sqrt(5.0)) / 2.0;
if (miles 1e-5) {<br>return 0.0F;
double n = log(miles * sqrt(5.0)) / log(PHI);<br>int k = (int)floor(n);
double Fk = (pow(PHI, k) - pow(-PHI, -k)) / sqrt(5.0);<br>double Fk1 = (pow(PHI, k + 1) - pow(-PHI, -k - 1)) / sqrt(5.0);<br>double Fk2 = (pow(PHI, k + 2) - pow(-PHI, -k - 2)) / sqrt(5.0);
if (Fk1 - Fk DBL_EPSILON) {<br>return basic_miles2km(miles);
return Fk1 + ((miles - Fk) * ((float)(Fk2 - Fk1) / (Fk1 - Fk)));
Based on the number φ ((1 + √5) / 2), we can also convert miles to kilometers. The golden ratio φ ≈ 1.618 is close to 1.609, so Fibonacci numbers simulate conversion.
The variable n here is the index of the Fibonacci number corresponding to the value miles, based on the property that F(n) ≈ φ^n / √5. A logarithmic function is used to find n, and then it is rounded down to the nearest smaller integer k.
The variables Fk, Fk1, Fk2 are three consecutive Fibonacci numbers calculated using Binet's formulas for Fibonacci numbers: F(n) = (φ^n — (1 — φ)^n) / √5.
Binet's formula is an explicit formula for finding the n-th Fibonacci number without recursive computation. It is named after the French mathematician who discovered it. Binet's formula allows computing Fibonacci numbers in constant time (O(1)), whereas the recursive method can take exponential time. This makes Binet's formula very useful in computations related to Fibonacci sequences.
If the difference between F(k+1) and F(k) is less than DBL_EPSILON, it means the numbers are too close to each other, and the function simply calls basic_miles2km instead of computing a further value.
But why do we call basic_miles2km? After all, we're dealing with non-typical conversion methods. This is actually a safety mechanism. If Fk1 ≈ Fk (difference less than DBL_EPSILON), the denominator tends to zero → indeterminacy or NaN arises. Binet's formula can also work incorrectly for small n (especially n The condition (Fk1 - Fk float basic_miles2km(float miles) {<br>return miles * 1.609344f;
And at the end, interpolation is performed to find the resulting Fibonacci number: return Fk1 + ((miles - Fk) * ((float)(Fk2 - Fk1) / (Fk1 - Fk)));. If the previous conditions are not met, the function performs linear interpolation to obtain a more accurate Fibonacci number value based on the value of miles. But this function can accumulate errors at large n.
Let's move on to another similar implementation:
float fib_interpolate(float miles) {<br>if (miles 5.0F) {<br>return basic_miles2km(miles);
uint64_t prev_mile = 0;<br>uint64_t prev_km = 1;<br>uint64_t curr_mile = 1;<br>uint64_t curr_km = 2;
while (curr_mile miles) {<br>prev_mile = curr_mile;<br>prev_km = curr_km;
curr_mile = prev_km;<br>curr_km = prev_mile + prev_km;
if (curr_km prev_km || curr_mile prev_mile)...