When developing algorithms, it might help to formulate an invariant first and then use it to guide all other steps.
This is the standard practice in formal program derivation.
// w > 0
x := 17;
// w > 0 and x = 17
y := 42;
// w > 0 and x = 17 and y = 42
z := w + x + y
// w > 0 and x = 17 and y = 42 and z > 59
// w + 17 + 42 < 0
x := 17;
// w + x + 42 < 0
y := 42;
// w + x + y < 0
z := w + x + y
// z < 0
In this course, we won't study the topic of formal semantics in detail.
This is done, for example, in the MSc course Programming Languages.
Calculate an algorithm that takes two positive numbers $A$ and $B$ and computes $A \div B$ (quotient) and $A \bmod B$ (remainder).
Obviously, we assume that these operators are not available in our language. Let's assume we only have addition and subtraction available.
Calculate an algorithm that takes two natural numbers $A$ and $B$ and computes $A^B$. More formally:
$$ \ht{A\ge 0 \,\,\wedge\,\, B\ge 0}{\,\,\textrm{Compute $r$}\,\,}{r = A^B} $$
Your algorithm should use addition and multiplication.
The general approach for Hoare-style formal verification:
This is what we will explore in more detail in the next few sessions. The tool we will use is Dafny.