标题:素数魔方阵算法该怎样做 ?请帮忙一下解释这代码。。
取消只看楼主
爱国者—
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-4-29
结帖率:0
已结贴  问题点数:20 回复次数:0 
素数魔方阵算法该怎样做 ?请帮忙一下解释这代码。。
#include<stdio.h>
#include<math.h>
#define M 500
void main()
{
    int a[M],n,x,y,h,z,w,k,i,j=0,m,d=0;
    for(m=1;m<=M;m=m+2)                 /*求不大于500的素数*/
    {
        h=sqrt(m);
        for(i=2;i<=h;i++)
            if(m%i==0)break;
            if(i>=h+1)
            {a[j]=m;j++;}
    }
    for(i=4;i<=M-3;i++)               /*寻找合适的x,y,z*/
    {
        n=a[i];
        for(y=2;y<=250;y+=2)
            for(x=y+2;x<=250;x+=2)
            {
                if(x==2*y)continue;
                z=x-y;w=x+y;
                if(n-w<3||n+w>500)break;
                for(k=0,j=3;j<=sqrt(n+w);j+=2)
                {
                    if(n-w>j&&(n-w)%j==0){k=1;break;}
                    if(n-x>j&&(n-x)%j==0){k=1;break;}
                    if(n-y>j&&(n-y)%j==0){k=1;break;}
                    if(n-z>j&&(n-z)%j==0){k=1;break;}
                    if(n+x>j&&(n+x)%j==0){k=1;break;}
                    if(n+y>j&&(n+y)%j==0){k=1;break;}
                    if(n+z>j&&(n+z)%j==0){k=1;break;}
                    if((n+w)%j==0){k=1;break;}
                }
                if(k==0)
                {
                    d++;printf("NO:%d\n",d);
                    printf("%5d%5d%5d\n",n-x,n+w,n-x);
                    printf("%5d%5d%5d\n",n+z,n,n-z);
                    printf("%5d%5d%5d\n",n+y,n-w,n+x);
                    printf("\n\n");
                }
               

            }   
    }
}
搜索更多相关主题的帖子: continue include 
2011-05-19 00:11



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-340373-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.366334 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved