Most problems here work with integers — whole numbers, possibly negative. You compute with five operators:
+add,-subtract,*multiply/divide%modulo — the remainder after division
Modulo is the one beginners underuse, so meet it now: 17 % 5 is 2 because 17 divided by 5
is 3 with remainder 2. Two everyday uses you will reach for constantly:
- even or odd:
n % 2is0for even,1for odd. - last digit of a number:
n % 10.
def solve(n):
# is n even? return the word the statement asks for
return "even" if n % 2 == 0 else "odd"
long long solve(long long n) {
return n % 10; // the last digit of n
}
Remember the read -> compute -> print loop: these operators are your compute vocabulary. Every formula in this module is built from these five symbols.