Unable to write a squarefree algorithm
<p>I'm trying to write a function that returns true, if an integer is <a href="https://en.wikipedia.org/wiki/Square-free_integer" rel="nofollow">Square-Free</a> - this is what I've tried:</p>

<pre><code>def squarefree(n):
    for i in range (2,n-1):
        if n%(i**2)==0:
            return False
        else:
            return True
</code></pre>

<blockquote>
  <p>In mathematics, a square-free, or quadratfrei (from German language) integer, is an integer which is divisible by no other perfect square than 1. For example, 10 is square-free but 18 is not, as 18 is divisible by 9 = 32. The smallest positive square-free numbers are</p>
</blockquote>

<p><strong>Your loop ends on its first run.</strong> It checks only for divisibility by the first <code>i</code>, i.e.  by 4, what is caused by your <code>else</code> statement that makes the function return whether this specific <code>i</code> is a divisor or not.</p>

<p>You should wait until it ends to decide if <code>n</code> is square free:</p>

<pre><code>def squarefree(n): 
    for i in range (2, round(n**0.5)): 
        if n % (i**2) == 0: 
            return False 
    return True
</code></pre>

<p>That way, if the program finds that <code>n</code> fails this condition, it returns <code>False</code> immediately. But, if no square ruins this loop, then the program will output <code>True</code>, as it ends without "interruptions".</p>

<p>Remember - not every <code>if</code> must have an <code>else</code> attached. Also, consider improving by running only in the range of <code>sqrt(n)</code>, also called <code>round(n ** 0.5)</code>, since every higher number will surely be off modulo.</p>

python