Skip to content

Instantly share code, notes, and snippets.

@cocodrips
Last active January 1, 2016 00:59
Show Gist options
  • Select an option

  • Save cocodrips/8070383 to your computer and use it in GitHub Desktop.

Select an option

Save cocodrips/8070383 to your computer and use it in GitHub Desktop.
素因数分解したかった
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)
@cocodrips
Copy link
Author

yuizumi
おおお、ありがとうございます(´;ω;`)
ちゃんと直してみたいと思います!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment