Advertisement
Guest User

素因数分解 (MicroPython)

a guest
Mar 28th, 2020
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.70 KB | None | 0 0
  1. #
  2. #   素因数分解
  3. #
  4. import utime
  5.  
  6. def prime_factorize(n):
  7.     a = []
  8.     while n % 2 == 0:
  9.         a.append(2)
  10.         n //= 2
  11.     f = 3
  12.     while f * f <= n:
  13.         if n % f == 0:
  14.             a.append(f)
  15.             n //= f
  16.         else:
  17.             f += 2
  18.     if n != 1:
  19.         a.append(n)
  20.     return a
  21.  
  22.  
  23. if __name__ == '__main__':
  24.     start_time = utime.ticks_ms()
  25. #   CircuitPythonの場合
  26. #    start_time = time.monotonic_ns() // 1000000
  27.  
  28.     a = prime_factorize(849374738754534871)
  29.  
  30.     end_time = utime.ticks_ms()
  31. #   CircuitPythonの場合
  32. #    end_time = time.monotonic_ns() // 1000000
  33.  
  34.     print(a)
  35.     print("{0:.3f} 秒".format((end_time - start_time) / 1000.0))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement