Guest User

Untitled

a guest
Jun 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1.  
  2. from math import sqrt
  3.  
  4. sum1to = __import__("001 - sum multiples of 3 and 5").sum1to
  5. factorize = __import__("003 - largest prime factor of composite").factorize
  6.  
  7. def num_factors(n):
  8. """num_factors(int) -> int
  9.  
  10. Return the number of factors (prime or not) for a given number.
  11. """
  12. factors = factorize(n)
  13. num = 1
  14. for x, exp in factors:
  15. num *= (exp+1)
  16. return num
  17.  
  18. def triangle_with_min_num_factors_slower(min_num_factors):
  19. """triangle_with_min_num_factors_slower(int) -> (int, int, int)
  20.  
  21. Find the first triangle number that has at least min_num_factors factors.
  22. This method uses the prime factorizer from a previous question to find
  23. all of the prime factors for each triangle number and by extension the
  24. number of factors for the number.
  25. """
  26. i = 1
  27. factor_count = 1
  28. num = 1
  29.  
  30. while factor_count < min_num_factors:
  31. i = i+1
  32. num = sum1to(i)
  33. factor_count = num_factors(num)
  34. else:
  35. return i, num, factor_count
  36.  
  37. def triangle_with_min_num_factors_slow(min_num_factors):
  38. """triangle_with_min_num_factors_slower(int) -> (int, int, int)
  39.  
  40. Find the first triangle number that has at least min_num_factors factors.
  41. This method works by noticing that half of the factors for a given number
  42. will be contributed by a number's square root.
  43. """
  44. i = 0
  45. num = 0
  46. num_factors = 0
  47.  
  48. while num_factors < 500:
  49. i = i+1
  50. num += i
  51. num_factors = 0
  52. root = int(sqrt(num))
  53.  
  54. for n in xrange(1, root+1):
  55. if (num % n) is 0:
  56. num_factors += 2
  57.  
  58. if root**2 is num:
  59. num_factors -= 1
  60. else:
  61. return i, num, num_factors
  62.  
  63. def triangle_with_min_num_factors_fast(min_num_factors):
  64. """triangle_with_min_num_factors_slower(int) -> (int, int, int)
  65.  
  66. Find the first triangle number that has at least min_num_factors factors.
  67. This method is similar to the one that used prime factorization; however,
  68. this method uses memoization in order to avoid doing redundant factorization
  69. by noticing that the sum from 1 to n is n*(n+1) / 2.
  70. """
  71. i = 1
  72. factor_count = 0
  73. factor_table = {1:1}
  74.  
  75. while factor_count < min_num_factors:
  76. i += 1
  77. j = i+1
  78.  
  79. if (i & 1) is 0:
  80. factor_count = factor_table[i / 2]
  81. else:
  82. j /= 2
  83. factor_count = factor_table[i]
  84.  
  85. factor_table[j] = num_factors(j)
  86. factor_count *= factor_table[j]
  87. else:
  88. return i, sum1to(i), factor_count
  89.  
  90. if __name__ == "__main__":
  91. num, triangle, num_factors = triangle_with_min_num_factors_fast(500)
  92.  
  93. print """First triangle number with >= 500 factors is triangle(%d)=%d
  94. %d has %d factors.""" % (num, triangle, triangle, num_factors)
Add Comment
Please, Sign In to add comment