php中文网 | cnphp.com

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 355|回复: 0

求本原元

[复制链接]

2616

主题

2623

帖子

9286

积分

管理员

Rank: 9Rank: 9Rank: 9

UID
1
威望
0
积分
6548
贡献
0
注册时间
2021-4-14
最后登录
2024-4-20
在线时间
665 小时
QQ
发表于 2022-7-22 07:28:11 | 显示全部楼层 |阅读模式
#include <stdio.h>
#include <string.h>

int y[100000] = {} ;

int sort(int n)  
{
           int cnt=0;
           for(long i=2;i*i<=n;i++) {
                   while(n%i==0) {
                           y[cnt++]=i;
                           n/=i;
                   }
           }
           if(n!=1) {//如果n是一个合数,那么它的分解素因子只可能有一个大于sqrt(n)的;
                   //原因:设d能整除n,则n=d*n/d,则min(d,n/d)<=sqrt(n)。
                   y[cnt++]=n;
           }
        return cnt;
}

unsigned int fast_pow ( unsigned long x, unsigned long y, unsigned long m ){
    unsigned long temp;

    if ( y == 1 )
        return x;

    else if ( (unsigned long)y % 2 == 0 ) {
        temp = fast_pow(x,y/2,m); // 用临时变量减少重复的运算
        return (temp*temp)%m;
    }

    else {
        temp = fast_pow(x,(y-1)/2,m);
        return (((temp*temp)%m)*x)%m;
    }

}


int main()
{
//        printf("输入两个数,判断两个数之间的素数\n");
//        int num1 = 16718909, num2=16718909, count = 0;
        //int num1 = 16718909, num2=16726971, count = 0;
        int num1 = 16718909, num2=16776971, count = 0;
//        scanf("%d %d",&num1,&num2);
        int foundbenyuan = 0;
        printf("素数分别为:\n");
        for (int i=num1;i<=num2;i++)
        {
                int bian = 2;
                for (int j = 2; j < num2; j++)
                {
                        if (i % j == 0 && i != j)
                        {
                                //printf("它不是素数%d\n",i);
                                bian = 1;
                                break;
                        }
                }
                if(bian==2)
                {
                        printf("\n%d ",i);
                        int b = sort(i - 1);
                //        printf("\nyinzi %d个\n",b);
                        for (int a = 112;a <= 127;a ++)
                        {
                               
                                foundbenyuan = 1;
                                for(int tmp = b;tmp;tmp--)
                                {
                                        int q = y[tmp-1];
                                        int p = (i - 1)/q;
                                        int c = fast_pow(a,p,i);
                                //        printf("q:%d ",fast_pow(a,i-1,i));
                                        if((a%=i) == 1)
                                        {
                                                foundbenyuan = 0;
                                                break;
                                        }
                                }
                                if(foundbenyuan)
                                        printf("<%d>",a);
                        }
                        count++;
                        if(!(count%8))
                                printf("\n");
                }
        }
        printf("\n素数共有%d个\n",count);

        return 0;
}





上一篇:MSTS电源检测
下一篇:c++ 动态数组.h
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|php中文网 | cnphp.com ( 赣ICP备2021002321号-2 )51LA统计

GMT+8, 2024-4-20 22:59 , Processed in 0.175302 second(s), 45 queries , Gzip On.

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2020, Tencent Cloud.

申明:本站所有资源皆搜集自网络,相关版权归版权持有人所有,如有侵权,请电邮(fiorkn@foxmail.com)告之,本站会尽快删除。

快速回复 返回顶部 返回列表