VB5 algorithm, added  analytical version for debugging