0.210秒,用Miller-Ribin檢驗素數(shù)
在oj上是15ms
#include
#include
#include
#include
int a,b;
int mpow( int s,int t,int m )
{
long long f,p;
if ( t == 0 )
 return 1;
f = mpow( s,t >> 1,m );
if ( t & 1 )
{
 p = s * f * f;
 return p % m;
}
p = f * f;
return p % m;
}
int Miller_Ribin( int s )
{
int i,p;
for ( i = 0; i < 3; i++ )
{
 p = rand()% ( s - 2 ) + 2;
 if ( mpow( p,s - 1,s ) != 1 )
break;
}
return i == 3;
}
void work( int c )
{
int p = ( c - 1 ) >> 1,i,j,s = 1,r,t1,t2;
if ( c == 1 )
{
 if ( a = 3 )
printf("3\n");
 if ( a = 5 )
printf("5\n");
 if ( a = 7 )
printf("7\n");
 return ;
}
for ( i = 0; i < p; i++ )
 s *= 10;
for ( i = 1; i < 10; i += 2 )
{
 for ( j = 0; j < s; j++ )
{
r = i * s + j;
t1 = j / 10;
t2 = p - 1;
while ( t2 )
{
 r = r * 10 + ( t1 % 10 );
 t1 /= 10;
 t2--;
}
r = r * 10 + i;
if ( r < a )
 continue;
if ( r > b )
 return ;
if ( Miller_Ribin( r ) )
 printf("%d\n",r);
}
}
}
int main( )
{
int s,e;
srand( time( NULL ) );
int i,p,c;
// scanf("%d%d",&a,&b);
a = 3; b = 1000000000;
p = 1;
c = 0;
while ( p < a )
{
 c++;
 p *= 10;
}
p /= 10;
while ( p < b )
{
 if ( c == 2 )
printf("11\n");
 if ( c & 1 )
work( c );
 c++;
 p *= 10;
}
printf("force program runs %.3lfs\n",clock( ) / (double)
用c語言如何判斷素數(shù)
用c語言如何判斷素數(shù)
其他人氣:921 ℃時間:2020-03-04 05:26:50
優(yōu)質(zhì)解答
我來回答
類似推薦
- 用C語言如何判斷素數(shù)
- can you speak English中文什么意思
- 兩個容器,一個容器為15升,另一個為8升,如何利用他們從一桶油中倒出6升來?
- Idon't is.pig翻譯成中文
- 幾個俗語的意思或者是它比喻什么?
- -8x的立方=27 解方程
- It's a good idea for sb to do sth.所有知識點
- It is not difficult to get to school 為什么既有be動詞又
- 桃樹比梨樹多3分之1,梨樹就比桃樹少3分之1,對還是錯
- Please try( )any sound.The baby is sleeping in bed.
- 大自然中的現(xiàn)象,比如樹的年輪不僅記載年齡還可以知道天氣
- 比較傳統(tǒng)的春聯(lián)詞語.
猜你喜歡
- 1請各位英語高手幫我總結(jié)下各類詞的用法:
- 2英語中的狀語和語文中的狀語用法一樣嗎?
- 3一物體在一高度釋放,若在下落的一瞬間被子彈擊中,其落地時間與自由下落時相同;若在下落到一半高度時被擊中,時間要變短,為什么?
- 4直徑5mm 長度2500mm的黃銅求重量
- 5The little boy wants a white T-shirt.改為一般疑問句
- 6奧數(shù)題(移動一根火柴棒)
- 7“裁”字查字典去掉部首查幾畫
- 8某電器商場原有電視機和洗衣機工720臺,電視機售出1/2后,剩下的電視機和原有洗衣機
- 9誰能找一篇找一篇簡單一點的2分鐘左右的英語新聞
- 10一千萬加一千萬等于幾
- 11方程x平方-2根號2x+2=0
- 12解方程:5分之19乘5分之21-4x=25分之139