Fermat’s Chirstmas Theorem (素數打表的)

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   io   strong   for   

                                                                         Fermat’s Chirstmas Theorem Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %lluSubmit Status Practice SDUTOJ 2093

Description

In a letter dated December 25, 1640; the great mathematician Pierre de Fermat wrote to Marin Mersenne that he just proved that an odd prime p is expressible as p = a2 + b2 if and only if p is expressible as p = 4c + 1. As usual, Fermat didn’t include the proof, and as far as we know, neverwrote it down. It wasn’t until 100 years later that no one other than Euler proved this theorem.

Input

Your program will be tested on one or more test cases. Each test case is specified on a separate input line that specifies two integers L, U where L ≤ U < 1, 000, 000The last line of the input file includes a dummy test case with both L = U = −1.

Output

L U x ywhere L and U are as specified in the input. x is the total number of primes within the interval [L, U ] (inclusive,) and y is the total number of primes (also within [L, U ]) that can be expressed as a sum of squares.

Sample Input

10 2011 19100 1000-1 -1

Sample Output

10 20 4 211 19 4 2100 1000 143 69
#include<stdio.h>  #include<string.h>  #define N 1000005int prime[100005];  int flag[1000005];  int e;  void getP()  // 素數打表,找出素數存棧  {  int i, j;  e = 0;  memset(flag, 0, sizeof(flag) ); //標記初始化for ( i=2; i<N; i++)  {  if ( flag[i]==0 ) {prime[e++] = i; //進棧  }for ( j=0; j<e && i*prime[j]<N; j++ )  {  flag[ i * prime[j] ] = 1;  }  }  }  int main()  {  int l,u,x,y;  getP(); int i;while(scanf("%d %d",&l,&u))  {    if(l==-1 && u==-1)              break;  x=0;  y=0;for( i=0; i<e; i++)  {  if(prime[i]>=l && prime[i]<=u )  {  x++;  if(prime[i]%4==1) {                    y++;  }}  if(prime[i]>u)                  break;  }  if(l<=2 && u>=2){y++;}printf("%d %d %d %d\n",l, u, x, y );  }  return 0;  }  

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.