The Rice and the Chessboard: A Programming Story
Here's an engaging story that explains this exponential growth program, inspired by the famous "Wheat and Chessboard" legend:
The Emperor's Reward
Once in ancient India, a poor farmer named Paddy saved an emperor's life. The grateful emperor asked Paddy to name his reward.
Paddy, being wise, made a humble request:
"Give me 1 grain of rice on the first day, double it every day for 64 days, and place them on a chessboard (which has 64 squares)."
The emperor laughed, thinking it was a small request. But soon, he realized his mistake...
How the Java Program Represents This Story
public class Paddy {
public static void main(String[] args) {
int day = 1; // Start from Day 1
int paddy = 1; // Initial rice grains (1 grain on Day 1)
while (day <= 64) { // Repeat for 64 days (chessboard squares)
paddy = paddy * 2; // Double the rice grains each day
day = day + 1; // Move to the next day
System.out.println(paddy); // Print grains for that day
}
}
}
Output Explanation
The program prints the exponentially increasing rice grains each day:
2 (Day 1: 1 × 2 = 2)
4 (Day 2: 2 × 2 = 4)
8 (Day 3: 4 × 2 = 8)
16 (Day 4: 8 × 2 = 16)
...
9,223,372,036,854,775,808 (Day 64: A number so big it bankrupts the empire!)
Key Learning Points
-
Exponential Growth → Each day, rice doubles (
paddy = paddy * 2
). -
Loop Concept (
while
loop) → Runs for 64 days (chessboard squares). -
Overflow Issue → On Day 64,
int
can't hold the value (useBigInteger
for real calculations). - Real-World Lesson → Small, repeated growth leads to massive results (compound interest, viral spread, etc.).
Why This Story Matters
This is similar to:
- Compound interest in banking
- Viral growth in biology
- Moore's Law in computing
Fun Fact: By Day 64, the total rice would exceed 18 quintillion grains, enough to cover Earth in rice!
Would you like a modified version using BigInteger
for accurate results? 😊