Mojo is an emerging programming language that combines Python’s ease of use with C++’s speed. It is particularly suited for AI, machine learning, and high-performance applications. In this guide, you will work on five beginner-friendly Mojo projects to grasp essential programming concepts.
Objective: Get familiar with Mojo’s syntax and basic structure by writing a simple program.
fn main():
print("Hello, Mojo!")
main()
fn
)
fn
.main()
is the entry point of the program.print()
)
print()
function displays text output on the screen."Hello, Mojo!"
.fn main():
let name = "Alice"
print(f"Hello, {name}!")
main()
Objective: Learn recursion, loops, and performance optimizations.
fn fibonacci(n: Int) -> Int:
if n <= 0:
return 0
elif n == 1:
return 1
else:
return fibonacci(n - 1) + fibonacci(n - 2)
fn main():
let num: Int = 10 # Generate the 10th Fibonacci number
print(f"Fibonacci({num}) = {fibonacci(num)}")
main()
Recursion
fibonacci(n)
calls itself to compute previous numbers in the sequence.Base Case (if n <= 0
)
n=0
and n=1
.Performance Issue:
Using memoization to store computed values:
fn fibonacci_memo(n: Int, memo: List[Int]) -> Int:
if memo[n] != -1:
return memo[n]
if n <= 0:
return 0
elif n == 1:
return 1
else:
memo[n] = fibonacci_memo(n - 1, memo) + fibonacci_memo(n - 2, memo)
return memo[n]
fn main():
let num: Int = 10
let memo = [-1] * (num + 1) # Initialize memoization list
print(f"Fibonacci({num}) = {fibonacci_memo(num, memo)}")
main()
Next Steps:
Objective: Learn about functions, user input, and error handling.
fn add(a: Float, b: Float) -> Float:
return a + b
fn subtract(a: Float, b: Float) -> Float:
return a - b
fn multiply(a: Float, b: Float) -> Float:
return a * b
fn divide(a: Float, b: Float) -> Float:
if b == 0:
print("Error: Division by zero!")
return 0
return a / b
fn main():
let a: Float = 10.5
let b: Float = 5.2
print(f"Addition: {add(a, b)}")
print(f"Subtraction: {subtract(a, b)}")
print(f"Multiplication: {multiply(a, b)}")
print(f"Division: {divide(a, b)}")
main()
b == 0
.Objective: Learn loops, conditions, and performance optimizations.
fn is_prime(n: Int) -> Bool:
if n < 2:
return False
for i in range(2, n):
if n % i == 0:
return False
return True
fn main():
let num: Int = 17
if is_prime(num):
print(f"{num} is a prime number.")
else:
print(f"{num} is not a prime number.")
main()
Loop (for i in range(2, n)
)
2
to n-1
, checking divisibility.Checking for Prime
n
is divisible by any number, it’s not a prime.Checking only up to sqrt(n):
fn is_prime_optimized(n: Int) -> Bool:
if n < 2:
return False
for i in range(2, Int(math.sqrt(n)) + 1):
if n % i == 0:
return False
return True
Next Steps:
Objective: Practice arithmetic operations and data handling.
fn celsius_to_fahrenheit(celsius: Float) -> Float:
return (celsius * 9.0/5.0) + 32.0
fn main():
let celsius: Float = 25.0
let fahrenheit = celsius_to_fahrenheit(celsius)
print(f"{celsius}°C is equal to {fahrenheit}°F")
main()
These five beginner-friendly projects introduce core Mojo programming concepts:
✅ Functions & Recursion
✅ Loops & Conditions
✅ Error Handling
✅ Performance Optimization