当前位置:网站首页>Chapter 16: Constructing the Magic Square for Prime Numbers of Order n(5,7)
Chapter 16: Constructing the Magic Square for Prime Numbers of Order n(5,7)
2022-07-31 07:09:00 【Stock_Four】
// 构建n(5,7)Magic square of order prime numbers
int main()
{
int i, j, d, f, k, ml, m2, n, h, v, t, y, z;
long s, min;
int a[8][8], b[100], p[30000];
printf("请输入阶数n(n = 5 or 7) : ");
scanf("%d", &n);
printf("请输入区间 ml, m2:");
scanf("%d,%d", &ml, &m2);
if (ml % 2 == 0)
ml++;
for (i = ml; i <= m2; i = i + 2);
{
t = 1;
z = (int)sqrt(i);
for (j = 3; j <= z; j = j + 2)
if (i % j == 0)
{
t = 0;
break;
}
if (t == 1) p[i] = 1; //若[ml,m2]中奇数i为素
}
for (i = 1; i<= n; i++) //Square No1Columns are assigned initial values
a[i][1]=i*10+i;
for(i=1;i<=n;i++)
for (j = 2; j <= n; j++) // Construct mouth order diagonal positive according to the law
{
h = i + (n + 1) / 2 * (j - 1);
v = i + j - 1;
if (h > n) h = h % n;
if (h == 0) h = n;
if (v > n) v = v % n;
if (v == 0) v = n;
a[h][j] = i * 10 + v;
}
y = 0;
min = 100000;
for (d = 4; d <= (m2 - ml) / 4; d = d + 2) //双重d,i枚举
{
k = 0;
for (i = ml; i <= m2 - 4 * d; i = i + 2)
{
for (f = 1, j = 0; j <= n - 1; j++)
f* p[i + j * d];
if (f == 1) // 找到一个从iThe starting tolerance is d the prime segment
{
t = 1;
for (j = 1; j <= k; j++) //Elements where the same term occurs are excludedn段
if ((i - b[j]) % d == 0 && (i - b[j]) / d < n) t = 0;
if (t == 1) {
k++; b[k] = i;
}
}
}
if (k >= n) //达到ntime elementnsegment to form a prime magic square
{
y++;
for (s = 0, j = 1; j <= n; j++)
s += b[j];
s = s + (n - 1) * n / 2 * d;
if (s < min) min = s;
printf(" No%d:幻和为%d,(d=%d):\n" ,y, s, d);
for (i = 1; i <= n; i++) // Outputs a magic square of mouth-order prime numbers
{
for (j = 1; j <= n; j++)
printf(" %5d ",b[a[i][j] / 10] + (a[i][j] % 10 - 1) * d);
printf("\n");
}
if (s < min)min = s;
}
}
if (y > 1)
printf("以上%dThe minimum value of the magic sum in a prime magic square is %ld\n", y, min);
}
在这里插入代码片
边栏推荐
- 4-1-7 二叉树及其遍历 家谱处理 (30 分)
- DirectExchange交换机简单入门demo
- 试题 历届真题 错误票据【第四届】【省赛】【B组】
- 银河麒麟v10 sp1 安装 PostgreSQL 11.16
- 英语翻译软件-批量自动免费翻译软件支持三方接口翻译
- Koa框架的基本使用
- DDL+DML+DQL
- @ConfigurationProperties和@EnableConfigurationProperties
- mysql索引失效的常见9种原因详解
- 零样本学习&Domain-aware Visual Bias Eliminating for Generalized Zero-Shot Learning
猜你喜欢
随机推荐
Markdown中的数学符号
Debian 10 iptables (防火墙)配置
MySQL笔记下
浅层了解欧拉函数
NFS共享存储服务
nohup原理
如何在uni-app中选择一个合适的UI组件库
How to choose a suitable UI component library in uni-app
深度解析 z-index
LVM和磁盘配额
基本正则表达式元字符,字符,次数,锚定分组
安装gstreamer开发依赖库到项目sysroot目录
gstreamer的caps event和new_segment event
编辑时过滤当前节点及根据限制的层数过滤数据
Install and use uView
【TA-霜狼_may-《百人计划》】美术2.3 硬表面基础
Debian 10 dhcp 服务配置
搭建zabbix监控及邮件报警(超详细教学)
Oracle入门 11 - Linux 开关机及系统进程命令
Gradle剔除依赖演示