Functions

def and return, decomposition; GCD and a primality check up to the square root.

A function is declared with the word def; the result is returned with return. Functions let you split a solution into understandable parts and reuse code. Below is Euclid's algorithm for the greatest common divisor. It will serve you many more times (the standard library has a ready math.gcd, but a competitive programmer must understand how it works).
Python

GCD: your own function and math.gcd. Enter: 12 8

Note the line a, b = b, a % b — Python can swap values without a temporary variable. Functions returning True/False are convenient for checks. When testing a number for primality it is enough to try divisors up to the square root of n: if n has a divisor greater than the root, its paired divisor is necessarily smaller than the root.
Python

Primality check up to the square root. Enter: 97

Task: write a function digit_sum(n) that returns the sum of a number's digits (peel digits off with n % 10 and n //= 10). Then solve the attached problems "GCD of Two Numbers" and "Prime Check" — and mark the lesson as completed.

Practice problems

Доска