zoukankan      html  css  js  c++  java
  • Primes

    Description

    A pretty straight forward task, calculate the number of primes between 2 integers.

    Given 2 integers A ≤ B < 105 what’s the number of primes in range from A to B inclusive.

    Note: A prime number is a positive integer greater than 1 and is divisible by 1 and itself only. For N to be prime it is enough to test the divisibility of numbers less than or equal to square root of N.

    Input

    As many as 1000 lines, each line contains 2 integers A and B separated by a space. Input is terminated when A = B = -1 (Do not process this line).

    Output

    For every line in input – except for the last line where A = B = -1 - print the number of prime numbers between A and B inclusive.

    Sample Input

    0 9999
    1 5
    -1 -1

    Sample Output

    1229
    3
     
     
     
    #include<stdio.h>
    #include<math.h>
    #include<string.h>
    int a[100001] = {0};
    int main()
    {
        int  b, n, m;
        int i, j, t;
        a[0]=1;
        a[1] = 1;
        for(i = 2; i<= 100001; i++)
                        if(!a[i])
                        for( j = 2; j*i <= 100001; j++)
                          a[i*j] = 1;
        while( scanf( "%d%d", &n, &m) == 2 )
        {
             
               int count = 0,k = 0;
               if( n == -1 && m == -1 )
                   break;
                   else
                   {
                       if(n<0)
                       n=0;
                       for( i = n; i <= m; i++ )
                            if( a[i] == 0 )
                            count++;
                             printf( "%d\n", count);
                      }
        }
    }
  • 相关阅读:
    《Java技术》第四次作业
    Java第三次作业——面向对象基础(封装)
    《Java技术》第二次作业
    #《Java技术》第一次作业
    Java第十次作业
    Java第九次作业
    Java第八次作业
    Java第七次作业
    Java第6次作业
    Java第五次作业
  • 原文地址:https://www.cnblogs.com/zsj576637357/p/2264675.html
Copyright © 2011-2022 走看看