Chapter 1: Let’s learn Ursa!
Programming tutorials often start by printing text on the screen or showing how to use the computer as a calculator. But you can print text on the screen in a word processor, and use a calculator app for calculation! (Of course, there’s nothing wrong with using Ursa for calculations if you like.)
And as we said earlier, you will spend more time reading code than writing it. So let’s start by reading some code. Here it is:
let collatz = fn(start_n): Num {
var steps = 0
var n = start_n
loop {
if n == 1 { break steps }
n := (if n % 2 == 0 { n / 2 } else { n * 3 + 1 })
steps := steps + 1
}
}
for i in range(10) {
let n = i + 2
print(collatz(n))
}
Don’t worry if this looks incomprehensible: what we’ll do next is break it down. If you’d like a clue about what’s coming, try looking up “collatz”!