Although the function below is adequate for simple purposes. It lacks the advanced algorithm to…