refactor calculation to use exponents instead of recursion