Actually the function would have to be recursive since you have no idea how many loops you would need.