Euler Problem 92

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()

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s