title: Solving the Kempner Function in Python - why is this concurrent function continuing after the base case is reached?
body: <p>I've been doing Python puzzles and one I have been doing is using a concurrent function to solve the Kempner Function in Python.</p>
  <p>The Kempner Function, applied to a composite number, permits to find the smallest integer greater than zero which factorial is exactly divided by the number.</p>
  <h3>For example:</h3>
  <br />
  <p><strong>kempner(6) ➞ 3</strong><br /></p>
  <p><strong>1! = 1 % 6 &gt; 0</strong><br />
  <strong>2! = 2 % 6 &gt; 0</strong><br />
  <strong>3! = 6 % 6 === 0</strong></p>
  <p><strong>kempner(10) ➞ 5</strong></p>
  <p><strong>1! = 1 % 10 &gt; 0</strong><br />
  <strong>2! = 2 % 10 &gt; 0</strong><br />
  <strong>3! = 6 % 10 &gt; 0</strong><br />
  <strong>4! = 24 % 10 &gt; 0</strong><br />
  <strong>5! = 120 % 10 === 0</strong><br /></p>
  <p>There are various ways of doing this, and one of the solutions I have seen is this:</p>
  <pre><code>def kempner(n, i=1, total=1):
      if total % n == 0:
          return max(1, i-1)
      else:
          return kempner(n, i+1, total*i)
  </code></pre>
  <p>I understand the gist of what this is doing, however when I run it through debug mode and see what the variables are doing I can see that when the base condition is reached (<code>if total % n ==0</code>) and <code>return max(1, i-1)</code> is returned then everything in the <code>else</code> clause will continue to run until the function returns to its starting condition (e.g. for <code>kempner(10)</code> then <code>n = 10</code>, <code>i = 1</code>, <code>total = 1</code>). Why does it do that? Surely it should stop its recurrence if the base condition has been reached?</p>
  <p>This is a fairly abstract issue and is obviously a blind spot in my knowledge. If anyone has any insight I would be grateful.</p>