Fermat’s Chirstmas Theorem
来源:互联网 发布:tcp ip协议 端口 编辑:程序博客网 时间:2024/05/01 16:07
Fermat’s Chirstmas Theorem
题目描述
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, never
wrote it down. It wasn’t until 100 years later that no one other than Euler proved this theorem.
To illustrate, each of the following primes can be expressed as the sum of two squares:
5 = 22 + 12
13 = 32 + 22
17 = 42 + 12
41 = 52 + 42
Whereas the primes 11, 19, 23, and 31 cannot be expressed as a sum of two squares. Write a program to count the number of primes that can be expressed as sum of squares within a given interval.
输入
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, 000
The last line of the input file includes a dummy test case with both L = U = −1.
输出
L U x y
where 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.
示例输入
10 2011 19100 1000-1 -1
示例输出
10 20 4 211 19 4 2100 1000 143 69
题意为找所给区间中的素数以及可以表示成两个数平方和的素数个数
#include <iostream>#include<stdio.h>#include<math.h>#include<string.h>#include<algorithm>#include<queue>#include<set>#include<string>using namespace std;int Max=1000000;int prime[1000000];int flag[1000000];int num;void sushu(){ int i,j; num=0; memset(flag,0,sizeof(flag)); for(i=2;i<Max;i++) { if(flag[i]==0) prime[num++]=i; for(j=0;j<num&&i*prime[j]<Max;j++) { flag[i*prime[j]]=1; if(i%prime[j]==0) break; } }}int main(){ int l,u,x,y,i,j; sushu(); while(scanf("%d%d",&l,&u)!=EOF) { x=0;y=0; if(l==-1&&u==-1) break; for(i=0;i<num;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++;//注意:2既是素数也可以表示为两个数的平方和 printf("%d %d %d %d\n",l,u,x,y); }}
0 0
- Fermat’s Chirstmas Theorem
- Fermat’s Chirstmas Theorem
- Fermat’s Chirstmas Theorem
- Fermat’s Chirstmas Theorem
- Fermat’s Chirstmas Theorem(素数筛)
- POj 3511 Fermat’s Chirstmas Theorem
- 素数判断----E -Fermat’s Chirstmas Theorem
- SDUT Fermat’s Chirstmas Theorem(素数筛)
- Fermat’s Chirstmas Theorem——素数筛
- SDUTOJ 2093 Fermat’s Chirstmas Theorem 素数筛
- poj3511--Fermat's Christmas Theorem
- Test Prime Using Fermat's Little Theorem
- Fermat's little theorem(费马小定理)
- POJ 3511 Fermat's Christmas Theorem 可能会
- Joking with Fermat's Last Theorem UVA
- uva 12665 - Joking with Fermat's Last Theorem
- uva 12665 - Joking with Fermat's Last Theorem 题解
- poj 3511 Fermat's Christmas Theorem 筛素数
- 【Linux】GDB调试演示过程
- 函数模板
- Android系统编译时遇到的几个.mk的疑惑。
- LightOJ 1216 圆锥体积
- hdu 4925 Apple Tree--2014 Multi-University Training Contest 6
- Fermat’s Chirstmas Theorem
- java多线程的实现
- Eclipse启动Tomcat错误:Several ports (8080, 8009) required by Tomcat v6.0 Server at localhost are already
- Eclipse + CVS 之 如何将文件加入版本控制和脱离版本控制
- C#接口的作用
- LeetCode OJ算法题(五十二):Maximum Subarray
- 划分树
- 新浪微博接口
- 函数指针数组