用自创的算法快速解决拉姆奇数

import time, random, itertools

random.seed(42)

def me(k, m, n, layer_data):

layer1 = x % 2 for x in layer_data

layer2 = (x + k) % 2 for x in layer1

return (sum(layer1) % 2, sum(layer2) % 2)

def sa(R, m, n, p=10):

nodes = range(R)

for _ in range(p):

color = tuple(random.choice(0, 1) for _ in nodes)

max_clique = max(m, n)

if max_clique < 2:

has_clique = True

else:

has_clique = any(

all(coloru == colorv for u, v in itertools.combinations(c, 2))

for c in itertools.combinations(nodes, max_clique)

)

if not has_clique:

return False

return True

def f(m, n):

start_time = time.time()

bound = (2 * max(m, n) + abs(m + n - 6)) * (min(m, n) - 2) - 5 + max(m, n)

R_init = max(m, n)

while True:

layer_data = R_init, m, n, R_init \* m, R_init \* n

ring_state = me(R_init, m, n, layer_data)

b = m + n

cond = ring_state == (b % 2, (b + b % 2) % 2)

if cond and R_init > max(bound, 0):

break

R_init += 1

R_min = R_init - 2

R_max = int(R_init * 1.5)

best_R = \[\]

R = R_init

while len(best_R) <= 3 and R_min <= R <= R_max:

if sa(R, m, n):

best_R.append(R)

R += 1 if random.random() > 0.3 else -2

R = max(R_min, min(R, R_max))

final_R = int(sum(best_R) / len(best_R)) if best_R else R_init

cost_time = time.time() - start_time

return final_R, cost_time

results = \[\]

times = \[\]

for _ in range(5):

R, t = f(5, 4)

results.append(R)

times.append(t)

avg_R = sum(results) / len(results)

min_R = min(results)

max_R = max(results)

print(f"算法结果: {min_R}-{max_R}, 平均: {avg_R:.2f}")

results = \[\]

times = \[\]

for _ in range(3):

R, t = f(3, 5)

results.append(R)

times.append(t)

avg_R = sum(results) / len(results)

min_R = min(results)

max_R = max(results)

print(f"算法结果: {min_R}-{max_R}, 平均: {avg_R:.2f}")

while 1:

m = int(input("请输入m值: "))

n = int(input("请输入n值: "))

R, cost_time = f(m, n)

print(f"R({m},{n})≈{R}, 耗时:{cost_time:.4f}s")

相关推荐
fanruitian10 分钟前
pycharm 使用anaconda
ide·python·pycharm
Dwzun12 分钟前
基于SpringBoot3+Vue3的油莎豆种植管理系统
java·前端·vue.js·spring boot·后端·python·计算机网络
代码山河9 小时前
Python就业方向全解析:后端、爬虫、数据分析、AI你选哪个?
python·学习·架构·教程·面向对象·项目
在世修行9 小时前
智学文枢:题目流水线
python·多线程·状态机·pyside
梯度下降者9 小时前
零基础入门Python 爬虫
爬虫·python·数据分析·入门指南·webcrawler
“AI国潮设计-小江”11 小时前
【AIGC实战】Python+Stable Diffusion打造“英歌舞财神”IP,解锁“马到成功”等爆款情绪营销文案设计
开发语言·人工智能·python·prompt·aigc
benchmark_cc12 小时前
用 Python 拉 A 股全市场实时快照:Notebook 请求、结果校验与易错点
python·数据分析·pandas·量化交易·股票数据·quantdash
伞伞悦读12 小时前
【第70期】Adam二阶矩不是方差:偏差校正、动量与AdamW完整实验
python
外收内放12 小时前
Python基础语法练习题(73)
java·前端·python
锋行天下13 小时前
Langgraph HITL 中断运行,用户输入后输出打字效果
人工智能·python