Advertisement
Guest User

Primes.py

a guest
Apr 10th, 2015
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.85 KB | None | 0 0
  1. __author__ = 'Ygo'
  2.  
  3. import collections
  4. import time
  5.  
  6.  
  7. i = n = int(input("Enter number to factor: "))
  8.  
  9. time_start = int(round(time.time() * 1000))
  10.  
  11. current_factor = 2
  12.  
  13. factors = []
  14.  
  15. while current_factor < n:
  16.     if n % current_factor == 0:
  17.         factors.append(current_factor)
  18.         n = n / current_factor
  19.     else:
  20.         current_factor += 1
  21.  
  22. factors.append(current_factor)
  23.  
  24. # print(factors)
  25.  
  26. factors_grouped = collections.OrderedDict()
  27.  
  28. for factor in factors:
  29.     factors_grouped[factor] = factors.count(factor)
  30.  
  31. # print(factors_grouped)
  32.  
  33. out = ""
  34.  
  35. for key, value in factors_grouped.items():
  36.     out += "{0}^{1} ".format(key, value)
  37.  
  38. time_to_complete = int(round(time.time() * 1000)) - time_start
  39.  
  40. print("The prime factors of {0} are:".format(i))
  41. print(out)
  42. print("Operation took {0} milliseconds".format(time_to_complete))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement