日期:2014-05-20  浏览次数:20792 次

【C# 每日一题6】素数个数
输入a b (0<a<=b<10000000)
求a b之间(不算a,b)的素数个数!

input:
3 17
output:
4

因为之间的是5 7 11 13

这个题应该有很多方法,求最优算法,大家想想怎么算最快呢?



------解决方案--------------------
C# code

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Primes
{
   public class Primes
   {
      private long min;
      private long max;

      public Primes()
         : this(2, 100)
      {
      }

      public Primes(long minimum, long maximum)
      {
         if (min < 2)
            min = 2;
         else
            min = minimum;

         max = maximum;
      }

      public IEnumerator GetEnumerator()
      {
         for (long possiblePrime = min; possiblePrime <= max; possiblePrime++)
         {
            bool isPrime = true;
            for (long possibleFactor = 2; possibleFactor <=
               (long)Math.Floor(Math.Sqrt(possiblePrime)); possibleFactor++)
            {
               long remainderAfterDivision = possiblePrime % possibleFactor;
               if (remainderAfterDivision == 0)
               {
                  isPrime = false;
                  break;
               }
            }
            if (isPrime)
            {
               yield return possiblePrime;
            }
         }
      }
   }
}
   class Program
   {
      static void Main(string[] args)
      {
         Primes primesFrom2To1000 = new Primes(2, 1000);
         foreach (long i in primesFrom2To1000)
            Console.Write("{0} ", i);

         Console.ReadKey();
      }
   }

------解决方案--------------------
素数没产生的公式吧...
多核情况下用多个线程探测吧.
------解决方案--------------------
用素数筛,复杂度是O(n)

C/C++ code

#include <algorithm>
#include <cstdio>

template<int MAX_PRIME>
struct PNMaker {
    bool isP[MAX_PRIME]; //标记某个数是不是素数
    int p[MAX_PRIME]; //素数线性表
    //找出[1, N)的所有素数,并且返回素数的个数
    inline int makePrimes(int N) {
        fill(isP, isP + N, true);
        int i, j;
        int top = 0;
        int x;
        isP[0] = isP[1] = false;
        for (i = 2; i < N; ++i) {
            if (isP[i]) p[top++] = i;
            for (j = 0; j < top; ++j) {
                x = p[j] * i;
                if (x >= N) break;
                isP[x] = false;
                if (i % p[j] == 0) break;
            }
        }
        return top;
    }


    /////////////////////////////////////

    int p_num;
    void init() {
        p_num = makePrimes();
    }

    int getNum() { return p_num;}
    bool isPrm(int i) { return isP[i]; }
    int get(int index) { return p[index]; }

    /////////////////////////////////////

    bool isPrmForce(unsigned int p) {
        if (p == 2 || p == 3) return true;
        else if(p % 2 == 0 || p % 3 == 0) return false;
        int step = 4;
        int i;
        for (i = 5; i * i <= p; i += step) {
            if (p % i == 0) return false;
            step = 6 - step;
        }
        return true;
    }
};





using namespace std;
PNMaker<10000005> p;

int main() {
    int a, b;
    scanf("%d %d", &a, &b);
    p.makePrimes(b);
    int cnt = 0;
    for (int i = a + 1; i < b; ++i)
        if (p.isPrm(i))
            ++cnt;
    printf("%d\n", cnt);
    return 0;
}

------解决方案--------------------
探讨

引用:

C# code

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Primes
{
public class Primes
……

------解决方案--------------------