当前位置:   article > 正文

找出素数并升序排列c语言,升序素数和降序素数 - 编程擂台 - 数学研发论坛 - Powered by Discuz!...

编写两个函数,将n个正整数中的素数按升序排序

#include   

#include   

#include   "gmp.h"

FILE *pf=fopen("data.txt","w");

const   int  L=8;

int     count=3;

unsigned prime[]={

7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,

73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,

179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,

283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,

419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,

547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,

661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,

811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,

947,953,967,971,977,983,991,997

};

unsigned p_prime[]={

7,11,31,41,43,53,61,71,73,83,97,211,311,331,

421,431,433,443,521,541,557,631,641,643,653,

661,733,743,751,761,773,811,821,853,863,877,

881,883,887,911,941,953,971,977,983,991,997

};

mpz_t   Hash[10][L], Prime_Product, com_factor;

void    Init( ){

int i, j;

for( i=0; i<10; ++i ){

for( j=0; j

mpz_init_set_ui( Hash[i][j], (i==0)? 0 : i );

}

for( i=1; i<10; ++i ){

for( j=1; j

mpz_mul_ui( Hash[i][j], Hash[i][j-1], 10 );

}

mpz_init_set_ui( Prime_Product, 1 );

mpz_init( com_factor );

for( i=0; i

mpz_mul_ui( Prime_Product, Prime_Product, prime[i] );

}

void    Clear( ){

mpz_clear( Prime_Product );

mpz_clear( com_factor );

for( int i=0; i<10; ++i ){

for( int j=0; j

mpz_clear( Hash[i][j] );

}

fclose( pf );

}

void    Specail_Print(  ){

fprintf(pf,"2\n3\n5\n");

for( int k=0; k

fprintf(pf,"%u\n",p_prime[k]), ++count;

}

void     combination(   int   n,   int   m   ){

mpz_t num;

mpz_init_set_ui( num, 0 );

unsigned  *c=new unsigned[m+2], j, dig_sum=m;

for( j=1; j<=m; ++j )  c[j]=j-1, mpz_add( num, num, Hash[1][j-1] );

c[m+1]=n;

R2:

if( c[1]%2==0 && c[1]+1!=5 && dig_sum%3!=0 ){

mpz_gcd( com_factor, num, Prime_Product );

if( mpz_cmp_ui( com_factor, 1 )==0 && mpz_probab_prime_p( num, 1 ) ){

mpz_out_str( pf, 10, num );

fprintf(pf,"\n");

++count;

}

}

if( m&1 )

if( c[1]+1

++c[1];

mpz_add_ui( num, num, 1 );

++dig_sum;

goto R2;

}

else{

j=2;  goto R4;

}

else

if( c[1]>0 ){

--c[1];

mpz_sub_ui( num, num, 1 );

--dig_sum;

goto R2;

}

else{

j=2;  goto R5;

};

R4:  if( c[j]>=j ){

if( j<=m )

mpz_sub( num, num, Hash[c[j]-j+2][j-1] ), dig_sum-=c[j]-j+2;

if( j-1<=m )

mpz_sub( num, num, Hash[c[j-1]-j+3][j-2] ), dig_sum-=c[j-1]-j+3;

c[j]=c[j-1];

c[j-1]=j-2;

if( j<=m )

mpz_add( num, num, Hash[c[j]-j+2][j-1] ), dig_sum+=c[j]-j+2;

if( j-1<=m )

mpz_add( num, num, Hash[c[j-1]-j+3][j-2] ), dig_sum+=c[j-1]-j+3;

goto R2;

}

else

++j;

R5:  if( c[j]+1

if( j<=m )

mpz_sub( num, num, Hash[c[j]-j+2][j-1] ), dig_sum-=c[j]-j+2;

if( j-1<=m )

mpz_sub( num, num, Hash[c[j-1]-j+3][j-2] ), dig_sum-=c[j-1]-j+3;

c[j-1]=c[j];

c[j]=c[j]+1;

if( j<=m )

mpz_add( num, num, Hash[c[j]-j+2][j-1] ), dig_sum+=c[j]-j+2;

if( j-1<=m )

mpz_add( num, num, Hash[c[j-1]-j+3][j-2] ), dig_sum+=c[j-1]-j+3;

goto R2;

}

else{

++j;

if( j<=m )    goto R4;

}

delete []c;

mpz_clear( num );

}

int   main(int   argc,   char   *argv[])

{

Init( );

Specail_Print( );

int time=GetTickCount();

for( int i=4; i<=L; ++i )

combination(   i+8,  i  );

Clear( );

printf("total : %d\n",count);

printf("%d ms\n",GetTickCount()-time);

return   0;

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/328677
推荐阅读
相关标签
  

闽ICP备14008679号