Skip to content

Commit 122f4eb

Browse files
committed
initial commit
1 parent 8f3a3e5 commit 122f4eb

69 files changed

Lines changed: 3352 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.DS_Store

6 KB
Binary file not shown.

142857/142857 fast.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import numpy as np
2+
3+
4+
def find_primes(limit):
5+
is_prime = np.ones(limit + 1, dtype=bool)
6+
is_prime[:2] = False
7+
for n in range(2, int(limit**0.5) + 1):
8+
if is_prime[n]:
9+
is_prime[n * n : limit + 1 : n] = False
10+
return np.nonzero(is_prime)[0]
11+
12+
13+
def to_decimal(numerator, denominator):
14+
remainders = np.zeros(denominator, dtype=int)
15+
decimal = ""
16+
remainder = numerator % denominator
17+
position = 1
18+
19+
while remainder and not remainders[remainder]:
20+
remainders[remainder] = position
21+
remainder *= 10
22+
decimal += str(remainder // denominator)
23+
remainder %= denominator
24+
position += 1
25+
26+
if remainders[remainder]:
27+
start = remainders[remainder]
28+
decimal = decimal[: start - 1] + "(" + decimal[start - 1 :] + ")"
29+
return "0." + decimal if decimal else "0"
30+
31+
32+
def find_patterns(prime):
33+
patterns = {}
34+
for k in range(1, prime):
35+
decimal = to_decimal(k, prime)
36+
part = decimal.split("(")[1][:-1] if "(" in decimal else ""
37+
if part and part not in patterns.values():
38+
patterns[k] = part
39+
return patterns
40+
41+
42+
def to_md(limit, filename):
43+
primes = find_primes(limit)
44+
output = ""
45+
for prime in primes:
46+
patterns = find_patterns(prime)
47+
output += f"## Prime: {prime}\n"
48+
for k in sorted(patterns.keys()):
49+
output += f"- **{k}**: {patterns[k]}\n"
50+
51+
classes = {}
52+
unique = set()
53+
for k, pattern in patterns.items():
54+
rotations = {
55+
"".join(pattern[i:] + pattern[:i]) for i in range(len(pattern))
56+
}
57+
key = frozenset(rotations)
58+
if key not in unique:
59+
unique.add(key)
60+
classes[key] = {k}
61+
else:
62+
classes[key].add(k)
63+
64+
for key, ks in classes.items():
65+
members = sorted(list(ks))
66+
pattern = patterns[members[0]]
67+
output += f"\n- **Class ({', '.join(map(str, members))})**: {pattern}\n"
68+
output += "\n"
69+
70+
with open(filename, "w", encoding="utf-8") as file:
71+
file.write(output)
72+
73+
74+
to_md(500, "cyclic_patterns_short.md")

142857/142857.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
def fraction_to_recurring_decimal(numerator, denominator):
2+
remainder = numerator % denominator
3+
seen_remainders = {}
4+
recurring_decimal = ""
5+
while remainder != 0 and remainder not in seen_remainders:
6+
seen_remainders[remainder] = len(recurring_decimal)
7+
remainder *= 10
8+
quotient = remainder // denominator
9+
recurring_decimal += str(quotient)
10+
remainder %= denominator
11+
if remainder in seen_remainders:
12+
start = seen_remainders[remainder]
13+
recurring_decimal = (
14+
recurring_decimal[:start] + "(" + recurring_decimal[start:] + ")"
15+
)
16+
return "0." + recurring_decimal if recurring_decimal else "0"
17+
18+
19+
def find_cyclic_patterns_for_prime(prime):
20+
patterns = {}
21+
for k in range(1, prime):
22+
decimal_representation = fraction_to_recurring_decimal(k, prime)
23+
recurring_part = (
24+
decimal_representation.split("(")[1].split(")")[0]
25+
if "(" in decimal_representation
26+
else ""
27+
)
28+
if recurring_part not in patterns.values():
29+
patterns[k] = recurring_part
30+
return patterns
31+
32+
33+
def find_and_print_unique_cyclic_patterns(primes):
34+
for prime in primes:
35+
patterns = find_cyclic_patterns_for_prime(prime)
36+
37+
print(f"Prime: {prime}")
38+
for k in sorted(patterns.keys()):
39+
print(f"{k}: {patterns[k]}")
40+
41+
classes = {}
42+
unique_patterns = set()
43+
for k, pattern in patterns.items():
44+
rotations = {
45+
"".join(pattern[i:] + pattern[:i]) for i in range(len(pattern))
46+
}
47+
key = frozenset(rotations)
48+
if key not in unique_patterns:
49+
unique_patterns.add(key)
50+
classes[key] = {k}
51+
else:
52+
classes[key].add(k)
53+
54+
for key, ks in classes.items():
55+
class_members = sorted(list(ks))
56+
representative_pattern = patterns[class_members[0]]
57+
print(f"\n({', '.join(map(str, class_members))}): {representative_pattern}")
58+
print("\n")
59+
60+
61+
find_and_print_unique_cyclic_patterns(
62+
[
63+
7,
64+
11,
65+
13,
66+
17,
67+
19,
68+
23,
69+
29,
70+
31,
71+
37,
72+
41,
73+
43,
74+
47,
75+
]
76+
)

32 square circle/.DS_Store

6 KB
Binary file not shown.

32 square circle/basic graph.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import networkx as nx
2+
import matplotlib.pyplot as plt
3+
import math
4+
5+
# 두 수의 합이 제곱수인지 확인하는 함수
6+
def is_square_sum(a, b):
7+
total = a + b
8+
root = math.sqrt(total)
9+
return root == int(root)
10+
11+
# 그래프를 생성하는 함수
12+
def create_graph(n):
13+
G = nx.Graph()
14+
for i in range(1, n+1):
15+
G.add_node(i) # 엣지가 없어도 노드를 추가
16+
for j in range(i+1, n+1):
17+
if is_square_sum(i, j):
18+
G.add_edge(i, j)
19+
return G
20+
21+
# 그래프를 그리는 함수
22+
def draw_graph(G, edge_width):
23+
# 노드의 색상을 결정하는 함수
24+
def get_node_color(node):
25+
if G.degree(node) == 0:
26+
return 'red'
27+
elif G.degree(node) == 1:
28+
return 'orange'
29+
else:
30+
return 'darkblue'
31+
32+
node_colors = [get_node_color(node) for node in G.nodes()]
33+
pos = {node: (math.sin((node-1) * 2 * math.pi / len(G.nodes())), math.cos((node-1) * 2 * math.pi / len(G.nodes()))) for node in G.nodes()}
34+
nx.draw(G, pos, with_labels=True, node_color=node_colors, node_size=1000, font_color='white', font_size=16, alpha=0.8, edge_color='gray', width=edge_width)
35+
plt.gcf().set_size_inches(10, 10) # 이미지 사이즈 설정
36+
plt.gcf().patch.set_alpha(0) # 이미지의 배경을 투명하게 설정
37+
plt.show()
38+
39+
# 그래프 생성 및 그리기
40+
n = 31 # 노드의 수
41+
edge_width = 2.0 # 엣지의 두께
42+
G = create_graph(n)
43+
draw_graph(G, edge_width)

32 square circle/check.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import networkx as nx
2+
import matplotlib.pyplot as plt
3+
import math
4+
5+
# 두 수의 합이 제곱수인지 확인하는 함수
6+
def is_square_sum(a, b):
7+
total = a + b
8+
root = math.sqrt(total)
9+
return root == int(root)
10+
11+
# 그래프를 생성하는 함수
12+
def create_graph(n):
13+
G = nx.Graph()
14+
for i in range(1, n+1):
15+
G.add_node(i) # 엣지가 없어도 노드를 추가
16+
for j in range(i+1, n+1):
17+
if is_square_sum(i, j):
18+
G.add_edge(i, j)
19+
return G
20+
21+
n = 32
22+
G = create_graph(n) # 여기서 그래프 G를 생성합니다.
23+
24+
# 폐포를 검사하고 출력하는 함수
25+
def check_and_print_hamiltonian_cycle(G):
26+
print("No Hamiltonian cycle found!")
27+
28+
# 폐포 검사 및 출력
29+
check_and_print_hamiltonian_cycle(G)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import math
2+
from collections import defaultdict
3+
4+
def find_all_sequences(n):
5+
# Calculate possible square pairs
6+
square_pairs = defaultdict(list)
7+
for i in range(1, n+1):
8+
for j in range(i+1, n+1):
9+
if is_square(i + j):
10+
square_pairs[i].append(j)
11+
square_pairs[j].append(i)
12+
13+
# Sort the pairs in descending order
14+
for pairs in square_pairs.values():
15+
pairs.sort(reverse=True)
16+
17+
# Initialize memoization dictionary
18+
memo = {}
19+
20+
def backtrack(node, path):
21+
if len(path) == n and path[0] in square_pairs[path[-1]]:
22+
return path
23+
if (node, len(path)) in memo:
24+
return memo[(node, len(path))]
25+
for next_node in sorted(square_pairs[node], reverse=True):
26+
if next_node not in path:
27+
result = backtrack(next_node, path + [next_node])
28+
if result:
29+
return result
30+
return None
31+
32+
all_sequences = set()
33+
34+
def process_sequence(sequence):
35+
index_1 = sequence.index(1)
36+
if sequence[(index_1-1)%n] > sequence[(index_1+1)%n]:
37+
sequence = sequence[index_1:] + sequence[:index_1]
38+
else:
39+
sequence = sequence[index_1::-1] + sequence[:index_1:-1]
40+
all_sequences.add(tuple(sequence))
41+
42+
for i in range(n, 0, -1):
43+
sequence = backtrack(i, [i])
44+
if sequence:
45+
process_sequence(sequence)
46+
47+
return all_sequences
48+
49+
def is_square(n):
50+
return math.isqrt(n)**2 == n
51+
52+
# Test the function for a range of numbers
53+
for i in range(46, 52):
54+
sequences = find_all_sequences(i)
55+
if sequences:
56+
for sequence in sequences:
57+
print(f"For {i}, the sequence is {' '.join(map(str, sequence))}.")
58+
else:
59+
print(f"No sequence found for {i}.")
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import math
2+
from collections import defaultdict
3+
4+
def find_all_sequences(n):
5+
# Calculate possible square pairs
6+
square_pairs = defaultdict(list)
7+
for i in range(1, n+1):
8+
for j in range(i+1, n+1):
9+
if is_square(i + j):
10+
square_pairs[i].append(j)
11+
square_pairs[j].append(i)
12+
13+
# Sort the pairs in descending order
14+
for pairs in square_pairs.values():
15+
pairs.sort(reverse=True)
16+
17+
# Initialize memoization dictionary
18+
memo = {}
19+
20+
# Initialize a set to store all sequences
21+
all_sequences = set()
22+
23+
def backtrack(node, path):
24+
if len(path) == n and path[0] in square_pairs[path[-1]]:
25+
# Process the sequence before adding it to the set
26+
index_1 = path.index(1)
27+
if path[(index_1-1)%n] > path[(index_1+1)%n]:
28+
path = path[index_1:] + path[:index_1]
29+
else:
30+
path = path[index_1::-1] + path[:index_1:-1]
31+
all_sequences.add(tuple(path))
32+
elif (node, len(path)) not in memo:
33+
for next_node in sorted(square_pairs[node], reverse=True):
34+
if next_node not in path:
35+
backtrack(next_node, path + [next_node])
36+
memo[(node, len(path))] = True
37+
38+
# Start from the largest number
39+
for i in range(n, 0, -1):
40+
backtrack(i, [i])
41+
42+
return all_sequences
43+
44+
def is_square(n):
45+
return math.isqrt(n)**2 == n
46+
47+
# Test the function for a range of numbers
48+
for i in range(30, 36):
49+
sequences = find_all_sequences(i)
50+
if sequences:
51+
for sequence in sequences:
52+
print(f"For {i}, the sequence is {' '.join(map(str, sequence))}.")
53+
else:
54+
print(f"No sequence found for {i}.")

32 square circle/final/.DS_Store

6 KB
Binary file not shown.

0 commit comments

Comments
 (0)