Skip to content

Commit eb20842

Browse files
committed
Merge split file by actions
2 parents 68480bb + e0270e6 commit eb20842

File tree

7 files changed

+187
-5
lines changed

7 files changed

+187
-5
lines changed

.github/workflows/mergefile.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ jobs:
3434
- name: Install dependencies
3535
run: |
3636
python -m pip install --upgrade pip
37-
pip install ruff
37+
pip install ruff isort
3838
- name: Run Ruff
3939
run: ruff format .
40+
- name: import sort
41+
run: isort --dedup code/main.py
4042
- name: Get last commit message
4143
id: last-commit
4244
run: |

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# hidehic0's library
22

3+
[![run unittest](https://github.com/hidehic0/library/actions/workflows/unittest.yml/badge.svg?branch=main)](https://github.com/hidehic0/library/actions/workflows/unittest.yml)
4+
[![CodeQL Advanced](https://github.com/hidehic0/library/actions/workflows/codeql.yml/badge.svg)](https://github.com/hidehic0/library/actions/workflows/codeql.yml)
5+
</br>
6+
37
[![total lines](https://tokei.rs/b1/github/hidehic0/library)](https://github.com/XAMPPRocky/tokei)
48
![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/hidehic0/library)
59
![GitHub repo size](https://img.shields.io/github/repo-size/hidehic0/library)
@@ -13,5 +17,3 @@
1317
ファイルは、分割したのは、libsに、全文はcodeに、置いてあります</br>
1418
</br></br>
1519

16-
[![run unittest](https://github.com/hidehic0/library/actions/workflows/unittest.yml/badge.svg?branch=main)](https://github.com/hidehic0/library/actions/workflows/unittest.yml)
17-
[![CodeQL Advanced](https://github.com/hidehic0/library/actions/workflows/codeql.yml/badge.svg)](https://github.com/hidehic0/library/actions/workflows/codeql.yml)

code/main.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
# pypyjit.set_param("max_unroll_recursion=-1")
3636

3737
sys.setrecursionlimit(5 * 10**5)
38+
from typing import List
3839

3940

4041
# 数学型関数
@@ -149,6 +150,42 @@ def factorization(n):
149150
return result
150151

151152

153+
def factorization_plural(L: List[int]) -> List[List[int]]:
154+
"""
155+
複数の数の素因数分解を行ないます
156+
計算量は、O(N * (√max(L) log log √max(L)))
157+
みたいな感じです
158+
159+
最初に素数を列挙するため、普通の素因数分解より効率がいいです
160+
"""
161+
res = []
162+
primes = eratosthenes(int(max(L) ** 0.5) + 20)
163+
164+
def solve(n):
165+
t = []
166+
for p in primes:
167+
if n % p == 0:
168+
cnt = 0
169+
while n % p == 0:
170+
cnt += 1
171+
n //= p
172+
173+
t.append([p, cnt])
174+
175+
if n != 1:
176+
t.append([n, 1])
177+
178+
if t == []:
179+
t.append([n, 1])
180+
181+
return t
182+
183+
for n in L:
184+
res.append(solve(n))
185+
186+
return res
187+
188+
152189
def simple_sigma(n: int) -> int:
153190
"""
154191
1からnまでの総和を求める関数
@@ -158,7 +195,7 @@ def simple_sigma(n: int) -> int:
158195

159196

160197
# 多次元配列作成
161-
from typing import List, Any
198+
from typing import Any, List
162199

163200

164201
def create_array2(a: int, b: int, default: Any = 0) -> List[List[Any]]:

libs/math_func.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from typing import List
2+
3+
14
# 数学型関数
25
def is_prime(n):
36
"""
@@ -110,6 +113,42 @@ def factorization(n):
110113
return result
111114

112115

116+
def factorization_plural(L: List[int]) -> List[List[int]]:
117+
"""
118+
複数の数の素因数分解を行ないます
119+
計算量は、O(N * (√max(L) log log √max(L)))
120+
みたいな感じです
121+
122+
最初に素数を列挙するため、普通の素因数分解より効率がいいです
123+
"""
124+
res = []
125+
primes = eratosthenes(int(max(L) ** 0.5) + 20)
126+
127+
def solve(n):
128+
t = []
129+
for p in primes:
130+
if n % p == 0:
131+
cnt = 0
132+
while n % p == 0:
133+
cnt += 1
134+
n //= p
135+
136+
t.append([p, cnt])
137+
138+
if n != 1:
139+
t.append([n, 1])
140+
141+
if t == []:
142+
t.append([n, 1])
143+
144+
return t
145+
146+
for n in L:
147+
res.append(solve(n))
148+
149+
return res
150+
151+
113152
def simple_sigma(n: int) -> int:
114153
"""
115154
1からnまでの総和を求める関数

test.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import unittest
22
from libs.grid import coordinate_check, grid_moves
3-
from libs.math_func import is_prime, simple_sigma
3+
from libs.math_func import is_prime, simple_sigma, factorization_plural
44
from libs.utils import lowerlist, upperlist, INF
55
from libs.modint import mod_add, mod_sub
66

@@ -51,6 +51,13 @@ def test_simple_sigma(self):
5151
with self.subTest(i=i):
5252
self.assertEqual(simple_sigma(i), ans)
5353

54+
def test_factorization_plural(self):
55+
test_cases = [([5, 10], [[[5, 1]], [[2, 1], [5, 1]]])]
56+
57+
for l, ans in test_cases:
58+
with self.subTest(l=l):
59+
self.assertEqual(factorization_plural(l), ans)
60+
5461

5562
class TestUtilsVariable(unittest.TestCase):
5663
def test_lowerlist(self):

tests/factorization_plural_new.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# python tests/factorization_plural_new.py 2.58s user 0.01s system 99% cpu 2.591 total
2+
3+
4+
def eratosthenes(n):
5+
"""
6+
n以下の素数を列挙します
7+
計算量は、O(n log log n)です
8+
先程の素数判定法で列挙するよりも、少し速いです
9+
列挙した素数は昇順に並んでいます
10+
アルゴリズムはエラトステネスです
11+
"""
12+
primes = [True] * (n + 1)
13+
primes[0], primes[1] = False, False
14+
i = 2
15+
while i**2 <= n:
16+
if primes[i]:
17+
for k in range(i * 2, n + 1, i):
18+
primes[k] = False
19+
20+
i += 1
21+
22+
return [i for i, p in enumerate(primes) if p]
23+
24+
25+
def factorization_plural(L):
26+
"""
27+
複数の数の素因数分解を行ないます
28+
計算量は、O(N * (√max(L) log log √max(L)))
29+
みたいな感じです
30+
31+
最初に素数を列挙するため、普通の素因数分解より効率がいいです
32+
"""
33+
res = []
34+
primes = eratosthenes(int(max(L) ** 0.5) + 20)
35+
36+
def solve(n):
37+
t = []
38+
for p in primes:
39+
if n % p == 0:
40+
cnt = 0
41+
while n % p == 0:
42+
cnt += 1
43+
n //= p
44+
45+
t.append([p, cnt])
46+
47+
if n != 1:
48+
t.append([n, 1])
49+
50+
if t == []:
51+
t.append([n, 1])
52+
53+
return t
54+
55+
for n in L:
56+
res.append(solve(n))
57+
58+
return res
59+
60+
61+
t = [10**10] * (10**4)
62+
63+
factorization_plural(t)

tests/factorization_plural_old.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# python tests/factorization_plural_old.py 42.42s user 0.00s system 99% cpu 42.515 total
2+
3+
4+
def factorization(n):
5+
"""
6+
nを素因数分解します
7+
計算量は、√Nです(要改善)
8+
複数回素因数分解を行なう場合は、√N以下の素数を列挙したので試し割りした法が速いです
9+
"""
10+
result = []
11+
tmp = n
12+
for i in range(2, int(-(-(n**0.5) // 1)) + 1):
13+
if tmp % i == 0:
14+
cnt = 0
15+
while tmp % i == 0:
16+
cnt += 1
17+
tmp //= i
18+
result.append([i, cnt])
19+
20+
if tmp != 1:
21+
result.append([tmp, 1])
22+
23+
if result == []:
24+
result.append([n, 1])
25+
26+
return result
27+
28+
29+
t = [10**10] * (10**4)
30+
31+
for n in t:
32+
factorization(n)

0 commit comments

Comments
 (0)