Optimal Self-Consistency for Efficient Reasoning with Large Language Models