E' piuttosto semplice, basta utilizzare un contatore e l'operatore modulo.