A Small Project: Sharpening All Your Knowledge
Final project: FizzBuzz, the Fibonacci sequence, and a prime number check — combining variables, expressions, loops, and functions.
A Small Project: Sharpening All Your Knowledge
You've learned all the ingredients: variables, types, operators, strings, if, for, while, and functions. Now it's time to put them into practice in three small projects — classics that every programmer has gone through.
Project 1: FizzBuzz
The rules: for the numbers 1 to 20 —
- multiples of 3 → print
Fizz - multiples of 5 → print
Buzz - multiples of 3 and 5 → print
FizzBuzz - otherwise → print the number
for i 1..21 {
if i % 15 == 0 {
print_str("FizzBuzz\n");
} else {
if i % 3 == 0 {
print_str("Fizz\n");
} else {
if i % 5 == 0 {
print_str("Buzz\n");
} else {
print(i);
}
}
}
}The core logic is in the remainder % and the nested branches — two concepts you learned in lessons 4 and 6.
Project 2: The Fibonacci Sequence
The Fibonacci sequence: each number is the sum of the two preceding ones — 0, 1, 1, 2, 3, 5, 8, ...
let a = 0;
let b = 1;
let berikutnya = 0;
for i in 0..10 {
print(a);
berikutnya = a + b;
a = b;
b = berikutnya;
}Output:
0
1
1
2
3
5
8
13
21
34The key: keeping the last two values and shifting them each round. Try tracing the first two or three rounds with a pencil — understand every step.
Project 3: Prime Number Check
A prime number: divisible only by 1 and itself.
fn is_prima(n) {
let hasil = 1;
let pembagi = 2;
while pembagi < n {
if n % pembagi == 0 {
hasil = 0;
}
pembagi = pembagi + 1;
}
hasil
}
for i 2..20 {
if is_prima(i) {
print_str("prima: ");
print(i);
}
}Output:
prima: 2
prima: 3
prima: 5
prima: 7
prima: 11
prima: 13
prima: 17
prima: 19Note: if n % pembagi == 0 then n is not prime — we mark it by setting hasil to 0. This is the "flag" pattern that's very common in programming.
Extra Challenges
- Factorization: print all the prime factors of
84(hint: combineis_primawith%). - Perfect numbers: find a number below
100whose divisors (excluding itself) sum to the number itself.6 = 1 + 2 + 3— 6 is the first perfect number. - Star pyramid: print a 5-row pyramid of the character
*— each row one longer than the previous (print_str("*\n"),"**\n", ...). Hint: use nestedforloops and an accumulating string variable (baris = baris + "*").
After This Course
You now know the basics of writing AHA! Lang code! The next steps:
- Expand the syntax: read the Language Tour — a complete overview of all language constructs.
- Advanced functions: see the Functions and Strings pages.
- Builtin reference: Builtins.
- How to install: Getting Started.
- The architecture behind the scenes: Architecture — how this compiler is built with LLVM.
If you're a Python user curious about how AHA! Lang compares with Python, read our blog article: AHA! Lang vs Python.
Happy coding! 🎉