import sys from happyc import digit_square_sum import pandas # Basic solution, with c for squaring and summing digits # 19:00 $ time python c_happy.py 1000000 # 143071 # # real 0m3.460s # user 0m3.424s # sys 0m0.025s def happy(max_number): return sorted(map(lambda x: is_happy(x), range(1, max_number+1)), key=lambda t: t[1]) def is_happy(x): seen = set() current = x while current not in seen: if x == 766: print(current) seen.add(current) current = digit_square_sum(current) return x, len(seen), current == 1 if __name__ == '__main__': counts = happy(1459) df = pandas.DataFrame(counts, columns=('number', 'steps', 'happy')) grouped = df.groupby(['steps', 'happy']) print(grouped.count())