Pascal basic algorithm