#!/usr/bin/env python3
from math import sqrt
class primes( object ) :
def __init__ ( self , n) :
# 構造体と違うのは、与えられたデータが一つでも、「計算に使うデータ」を
# 内部的に設定しておける、と言う辺りだ。
self .n = n
self .lst = list ( range ( 2 , n + 1 ) )
def __iter__ ( self ) :
return self
def __next__( self ) :
if self .lst == [ ] :
raise StopIteration
else :
match self .lst :
case [ head, *tail] :
self .lst = tail if head > sqrt( self .n ) \
else [ i for i in self .lst if i % head != 0 ]
return head
class prime_factors( object ) :
def __init__ ( self , n) :
self .n = n
self .N = n
self .primes = list ( primes( n) )
def __iter__ ( self ) :
return self
def __next__( self ) :
while True :
match self .primes :
case [ head, *tail] :
if head > sqrt( self .N ) :
raise StopIteration
elif self .n % head == 0 :
self .n //= head
return head
else :
self .primes = tail
if __name__ == '__main__' :
print ( list ( primes( 120 ) ) ) # 120以下の素数
print ( list ( prime_factors( 48 ) ) ) # 48 を素因数分解
print ( list ( prime_factors( 20100 ) ) ) # 20100 を素因数分解
IyEvdXNyL2Jpbi9lbnYgcHl0aG9uMwoKZnJvbSBtYXRoIGltcG9ydCBzcXJ0CgpjbGFzcyBwcmltZXMob2JqZWN0KToKICAgIGRlZiBfX2luaXRfXyhzZWxmLCBuKToKICAgICAgICAjIOani+mAoOS9k+OBqOmBleOBhuOBruOBr+OAgeS4juOBiOOCieOCjOOBn+ODh+ODvOOCv+OBjOS4gOOBpOOBp+OCguOAgeOAjOioiOeul+OBq+S9v+OBhuODh+ODvOOCv+OAjeOCkgogICAgICAgICMg5YaF6YOo55qE44Gr6Kit5a6a44GX44Gm44GK44GR44KL44CB44Go6KiA44GG6L6644KK44Gg44CCCiAgICAgICAgc2VsZi5uID0gbgogICAgICAgIHNlbGYubHN0ID0gbGlzdChyYW5nZSgyLCBuICsgMSkpCiAgICBkZWYgX19pdGVyX18oc2VsZik6CiAgICAgICAgcmV0dXJuIHNlbGYKICAgIGRlZiBfX25leHRfXyhzZWxmKToKICAgICAgICBpZiBzZWxmLmxzdCA9PSBbXToKICAgICAgICAgICAgcmFpc2UgU3RvcEl0ZXJhdGlvbgogICAgICAgIGVsc2U6CiAgICAgICAgICAgIG1hdGNoIHNlbGYubHN0OgogICAgICAgICAgICAgICAgY2FzZSBbaGVhZCwgKnRhaWxdOgogICAgICAgICAgICAgICAgICAgIHNlbGYubHN0ID0gdGFpbCBpZiBoZWFkID4gc3FydChzZWxmLm4pIFwKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGVsc2UgW2kgZm9yIGkgaW4gc2VsZi5sc3QgaWYgaSAlIGhlYWQgIT0gMF0KICAgICAgICAgICAgICAgICAgICByZXR1cm4gaGVhZAogICAgICAgIApjbGFzcyBwcmltZV9mYWN0b3JzKG9iamVjdCk6CiAgICBkZWYgX19pbml0X18oc2VsZiwgbik6CiAgICAgICAgc2VsZi5uID0gbgogICAgICAgIHNlbGYuTiA9IG4KICAgICAgICBzZWxmLnByaW1lcyA9IGxpc3QocHJpbWVzKG4pKQogICAgZGVmIF9faXRlcl9fKHNlbGYpOgogICAgICAgIHJldHVybiBzZWxmCiAgICBkZWYgX19uZXh0X18oc2VsZik6CiAgICAgICAgd2hpbGUgVHJ1ZToKICAgICAgICAgICAgbWF0Y2ggc2VsZi5wcmltZXM6CiAgICAgICAgICAgICAgICBjYXNlIFtoZWFkLCAqdGFpbF06CiAgICAgICAgICAgICAgICAgICAgaWYgaGVhZCA+IHNxcnQoc2VsZi5OKToKICAgICAgICAgICAgICAgICAgICAgICAgcmFpc2UgU3RvcEl0ZXJhdGlvbgogICAgICAgICAgICAgICAgICAgIGVsaWYgc2VsZi5uICUgaGVhZCA9PSAwOgogICAgICAgICAgICAgICAgICAgICAgICBzZWxmLm4gLy89IGhlYWQKICAgICAgICAgICAgICAgICAgICAgICAgcmV0dXJuIGhlYWQKICAgICAgICAgICAgICAgICAgICBlbHNlOgogICAgICAgICAgICAgICAgICAgICAgICBzZWxmLnByaW1lcyA9IHRhaWwKCmlmIF9fbmFtZV9fID09ICdfX21haW5fXyc6CiAgICBwcmludChsaXN0KHByaW1lcygxMjApKSkgICAgICAgICAgIyAxMjDku6XkuIvjga7ntKDmlbAKICAgIHByaW50KGxpc3QocHJpbWVfZmFjdG9ycyg0OCkpKSAgICAjIDQ4IOOCkue0oOWboOaVsOWIhuinowogICAgcHJpbnQobGlzdChwcmltZV9mYWN0b3JzKDIwMTAwKSkpICMgMjAxMDAg44KS57Sg5Zug5pWw5YiG6KejCg==
compilation info
Traceback (most recent call last):
File "/usr/lib/python3.9/py_compile.py", line 144, in compile
code = loader.source_to_code(source_bytes, dfile or file,
File "<frozen importlib._bootstrap_external>", line 918, in source_to_code
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "./prog.py", line 17
match self.lst:
^
SyntaxError: invalid syntax
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/python3.9/py_compile.py", line 150, in compile
raise py_exc
py_compile.PyCompileError: File "./prog.py", line 17
match self.lst:
^
SyntaxError: invalid syntax
stdout