um algoritmo

self explanatory.