I implemented happy_number function: _happy_set = { '1' } _unhappy_set = set()
def happy_number(n): """ Check if a number is a happy number https://en.wikipedia.org/wiki/Happy_number """ def create_current(n): current_array = sorted([value for value in str(n) if value != '0']) return (current_array, ''.join(current_array)) global _happy_set global _unhappy_set current_run = set() current_array, \ current_string = create_current(n) if current_string in _happy_set: return True if current_string in _unhappy_set: return False while True: current_run.add(current_string) current_array, \ current_string = create_current(sum([int(value) ** 2 for value in current_string])) if current_string in current_run | _unhappy_set: _unhappy_set |= current_run return False if current_string in _happy_set: _happy_set |= current_run return True Besides it need some documentation: is it a good implementation? Or are there things I should do differently? To decide for the values from 1 to 1E8 if it is happy or not, takes 280 seconds. Not to bad I think. Also not very good. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof -- https://mail.python.org/mailman/listinfo/python-list