Programming

Finding the Remainder of \(9^{2018} \mod 7\) - Understanding Modular Patterns

When you first see an expression like

\[ 9^{2018} \mod 7, \]

it looks impossible to compute. The number \(9^{2018}\) is unimaginably large — far beyond what any calculator can display.

Fortunately, modular arithmetic has a wonderful property: even enormous powers often fall into small repeating cycles. Once you notice the pattern, the problem becomes surprisingly simple.

This post walks you through the reasoning step by step.

Understanding Miri and Stacked Borrows in Rust — Why One Example Is UB and the Other Isn’t

Rust’s borrow checker guarantees memory safety at compile time, but some rules—especially around raw pointers—are too subtle for the compiler alone. That’s where Miri comes in. In this post, we’ll walk through two tiny Rust programs: one that Miri accepts, and one that Miri flags as Undefined Behavior. The difference comes down to how Stacked Borrows tracks pointer permissions.

Measuring the True Cost of Div (32‑bit vs 64‑bit with Rust and Inline Asm)

Modern CPUs are fast—but some instructions still hide surprising costs. One of the most misunderstood is DIV. Is 32‑bit division faster than 64‑bit? Does instruction width matter anymore on x86‑64?

To answer this properly, we need more than wall‑clock timers. We need cycle counters, instruction retirement statistics, serialization barriers, and tight control over CPU affinity.

HIR MIR LIR (LLVM IR)

Time to time, and especially for learning purposes, it’s useful to inspect the internal representations that the Rust compiler generates. Below are some notes on how to do that.