This one takes a while to run, but that’s no surprise since I used brute force. Nothing too complicated, just iterate 10 million time and keep a counter. But if you use psyco, it does speed things up a bit. Roughly 10 mins.
#Euler92 import psyco psyco.full() class Problem: def digits(self, n): sum = 0 for c in str(n): sum += int(c)*int(c) return sum def Solution(self): count = 0 for i in range (1, 10000001): if i % 100000 == 0: print i, count num = i f1 = 0 f89 = 0 while f89 < 2 and f1 < 2: num = self.digits(num) if num == 89: f89 += 1 if f89 == 2: count += 1 break elif num == 1: f1 += 1 if f1 == 2: break return "Answer = " + str(count) if __name__ == '__main__': P = Problem() print P.Solution()