writing algorithm