python 2.7.1

Python adventures

Throughout my geeky math adventures I have began to learn the programming language of Python. Coming from a PHP, Javascript and C background, the transition was relatively easy.

I’d like to share a very simple function definition with you. All it does is tell you if a number is a perfect square (e.g. 4, 9, 16, 25, 36) or not. It uses existing methods. But it’s the fastest method I’ve found so far. I can compute 50,000,000 numbers in under 50 seconds on my Macbook.

from math import sqrt

def is_perfect_square(n):
    return sqrt(n).is_integer()