Last active
January 1, 2016 00:59
-
-
Save cocodrips/8070383 to your computer and use it in GitHub Desktop.
素因数分解したかった
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import math | |
| def prime_decomposition(n): | |
| array = [] | |
| primes = primetable(int(math.ceil(math.sqrt(n)))) | |
| while n > 1: | |
| for p in primes: | |
| if n % p == 0: | |
| n /= p | |
| array.append(p) | |
| break | |
| else: | |
| array.append(n) | |
| break | |
| return array | |
| def primetable(max): | |
| sq = math.sqrt(max) | |
| table = [] | |
| t = range(0, max+1) | |
| t[0] = 0 | |
| t[1] = 0 | |
| p = 2 | |
| while p <= sq: | |
| if t[p] != 0: | |
| j = p + p | |
| while j <= max: | |
| t[j] = 0 | |
| j += p | |
| p += 1 | |
| for p in t: | |
| if p != 0: | |
| table.append(p) | |
| return table | |
| if __name__ == '__main__': | |
| print prime_decomposition(14) |
Author
cocodrips
commented
Dec 26, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment