Número De Identificação Fiscal or NIF, is the nine digit number which is used to identify a tax paying entity in Portugal. Any NIF can be validated using the mod 11 algorithm with one twist. I will describe what that twist is and how it affects validating a NIF.
When you buy anything in Portugal, in person or online, you give your NIF. This includes everything from the 100€ you spend on your mall haul, to the 2€ you spend on an espresso and a pastel de nata.
This number is relayed often so mistakes are very possible. Possible causes are bit flips during transmission when buying online — or mispronouncing seis as sete. To address this, an algorithm exists to determine if any give series of nine digits is a valid NIF. It can be defined as:
let n_i be the ith digit of the NIF as numbered from right to left. Then a NIF is valid if: