POj 3511 Fermat’s Chirstmas Theorem

E - Fermat’s Chirstmas Theorem
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

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, never
wrote 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, 000
The last line of the input file includes a dummy test case with both L = U = −1.

Output

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.

Sample Input

10 20
11 19
100 1000
-1 -1

Sample Output

10 20 4 2
11 19 4 2
100 1000 143 69
 
   
 
   
 
   
 
   
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
const int MAXN = 1000011;
bool flag[MAXN];
int primes[MAXN], pi;

void sai()
{
    int i, j;
    pi = 0;
    memset(flag, false, sizeof(flag));
    flag[1] = 1;
    for (i = 2; i < MAXN; i++)
    {
        if (!flag[i])
            primes[pi++] = i;
        for (j = 0; (j < pi)  && (i * primes[j] < MAXN); j++)
        {
            flag[i * primes[j]] = true;
            if (i % primes[j] == 0)
                break;
        }
    }
}

int main()
{
    sai();
    int i;
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n == -1 && m == -1)
        {
            break;
        }
        int count = 0,sum = 0;
        for(i=0;i<=m;i++)
        {
            if(primes[i]>=n && primes[i]<=m)
            {
                count++;
                if(primes[i]%4 == 1)
                {
                    sum++;
                }
            }
            else if(primes[i]>m)
            {
                break;
            }
        }

        
        if(n<=2 && m>=2)
        {
            sum++;
        }
        printf("%d %d %d %d\n",n,m,count,sum);
    }
    return 0;
}



 
   
 
   
 
   
 
   
 
  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叶孤心丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值