Fundamentals 6 min read

7 Tiny Code Gems That Pack Massive Power: From Shuffle to Fast Inverse Square Root

This article showcases seven ultra‑compact yet powerful code examples—from a zero‑code deployment tool and a two‑line shuffle algorithm to sleep sort, a one‑line Python AI snippet, a simple tomorrow‑time sleep call, the legendary fast inverse square‑root constant, and the classic hello‑world program.

Programmer DD
Programmer DD
Programmer DD
7 Tiny Code Gems That Pack Massive Power: From Shuffle to Fast Inverse Square Root

1. No Code

Project address: https://github.com/kelseyhightower/nocode. This GitHub project, now with 34k stars, demonstrates a lightweight cross‑platform, fully automatic tool that requires zero source code. It uses an “indescribable” high‑level syntax to provide one‑click detection, compilation, packaging, installation and execution.

2. Shuffle Algorithm

A two‑line implementation that generates a uniformly random permutation of n elements, ensuring each element has equal probability of appearing in any position.

for(int i = n - 1; i >= 0; i--)
    swap(arr[i], arr[rand(0, i)]) // rand(0, i) generates a random integer in [0, i]

3. Sleep Sort

The algorithm creates n threads, each associated with one of the n numbers. Each thread sleeps for a duration proportional to its number, then prints the number when it wakes, resulting in an ordered output.

public class SleepSort {
    public static void main(String[] args) {
        int[] ints = {1,4,7,3,8,9,2,6,5};
        SortThread[] sortThreads = new SortThread[ints.length];
        for (int i = 0; i < sortThreads.length; i++) {
            sortThreads[i] = new SortThread(ints[i]);
        }
        for (int i = 0; i < sortThreads.length; i++) {
            sortThreads[i].start();
        }
    }
}
class SortThread extends Thread {
    int ms = 0;
    public SortThread(int ms) { this.ms = ms; }
    public void run() {
        try {
            sleep(ms*10+10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(ms);
    }
}

4. “AI Core” One‑Liner

A single‑line Python loop that reads input, removes Chinese question particles, and prints the modified text, touted as a “billion‑yuan AI core”.

while True:
    print(input('').replace('吗','').replace('?','!'))

5. Get Tomorrow’s Timestamp

// talent talent
thread.sleep(86400*1000L);

6. Fast Inverse Square Root (0x5f375a86)

This famous magic number originates from the Quake III fast inverse square‑root algorithm. The core operation is: i = 0x5f3759df - (i >> 1); // what the fuck? Researchers later refined the constant to 0x5f37642f and even 0x5f375a86, achieving marginally better accuracy.

7. Hello World

Every programmer has written a “Hello World” program in some language.

Feel free to share other tiny yet powerful code examples in the comments.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

AlgorithmsShuffleFast Inverse Square Rootsleep sortcode-golf
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.