hdarwin

cstutoringcenter.com_Happy Primes

Jan 4th, 2013
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.65 KB | None | 0 0
  1. import math
  2. def sum_of_digits(num):
  3.     sum = 0
  4.     for x in str(num):
  5.         sum += int(x) ** 2
  6.     return sum
  7.  
  8. def happy(num):
  9.     while True:
  10.         num = sum_of_digits(num)
  11.         if num == 4: return False
  12.         if num == 1: return True
  13.  
  14. def isPrime(num):
  15.     if num == 2:return True
  16.     if not num % 2:return False
  17.     for x in xrange(2,int(math.ceil(math.sqrt(num)))+1):
  18.         if not num % x:return False
  19.     return True
  20.  
  21. sum = 0
  22. inc = 2
  23. count = 0
  24. while True:
  25.     if not inc % 10000:print inc
  26.     if isPrime(inc) and happy(inc):
  27.         sum += inc
  28.         count += 1
  29.     if count == 10000:break
  30.     inc += 1
  31. print sum
Advertisement
Add Comment
Please, Sign In to add comment