using System;
using System.IO;
using System.Runtime.InteropServices;
namespace EnumPrime
{
    class EnumPrime
    {
        [DllImport("kernel32.dll")]
        extern static short QueryPerformanceCounter(ref long x);
        [DllImport("kernel32.dll")]
        extern static short QueryPerformanceFrequency(ref long x);        static void Main()
        {
            uint num = 0U, top = 0U, n = 0U;
            long begintime = 0L, endtime = 0L, freqtime = 0L, resulttime = 0L;
            string filename;
            do
            {
                Console.Write("请输入上限:    ");
                try
                {
                    top = Convert.ToUInt32(Console.ReadLine());
                }
                catch
                {
                    Console.WriteLine("请正确输入数字!");
                    continue;
                }
                if (top < 2)
                    Console.WriteLine("请输入>=2的值");
            } while (top < 2);
            Console.Write("请输入保存位置:");
            filename = Console.ReadLine();
            StreamWriter oufile = null;
            try
            {
                oufile = new StreamWriter(filename);
            }
            catch
            {
                oufile = null;
            }
            QueryPerformanceCounter(ref begintime);
            PrimeTable[0] = 3;
            for (num = 2; num <= top; ++num)
            {
                if (TestPrime(num))
                {
                    if (oufile != null)
                        oufile.Write("{0},", num);
                    n++;
                }
            }
            QueryPerformanceCounter(ref endtime);
            QueryPerformanceFrequency(ref freqtime);
            Console.WriteLine("在<={0}时共有{1}个素数", top, n);
            if (oufile != null)
            {
                oufile.WriteLine();
                oufile.WriteLine("在<={0}时共有{1}个素数", top, n);
                oufile.Close();
                Console.WriteLine("结果保存在:    {0}", filename);
            }
            resulttime = (endtime - begintime) * 1000 / freqtime;
            Console.WriteLine("处理时间:      {0}时{1}分{2}.{3:000}秒", resulttime / 3600000, (resulttime / 60000) % 60, (resulttime / 1000) % 60, resulttime % 1000);
            Console.ReadLine();
        }
        static uint[] PrimeTable = new uint[6543];
        static ushort TableIndex = 0;
        static bool TestPrime(uint n)
        {
            uint i = 0;
            if (n == 1)
                return false;
            else if (n == 2)
                return true;
            if (n % 2 == 0)
                return false;
            for (i = 0; PrimeTable[i] * PrimeTable[i] <= n; i++)
            {
                if (n % PrimeTable[i] == 0)
                    return false;
            }
            if (n <= 65521)
                PrimeTable[TableIndex++] = n;
            return true;
        }
    }
}

解决方案 »

  1.   

    计时函数如果java不能调用Windows api的话可以改成别的
      

  2.   

    客气,你小子只要把分都给我就行了。
    C#能P/Invoke,Java可以JNI 
    但是肯定达不到这种精度了 【QueryPerformanceCounter】-------------------------------------------------------------
      

  3.   


    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */package EnumPrime;
    import java.io.*;/**
     *
     * @author Administrator
     */
    public class EnumPrime
    {    /**
         * @param args the command line arguments
         */
        public static void main(String[] args)
        {
            // TODO code application logic here
            int num = 0, top = 0, n = 0;
            long begintime = 0, endtime = 0, resulttime = 0;
            String filename = "";
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            do
            {
                System.out.print("请输入上限:    ");
                try
                {
                    top = Integer.parseInt(br.readLine());
                }
                catch(Exception e)
                {
                    System.out.println("请正确输入数字!");
                    continue;
                }
                if (top < 2)
                    System.out.println("请输入>=2的值");
            } while (top < 2);
            System.out.print("请输入保存位置:");
            PrintWriter oufile = null;
            try
            {
                filename = br.readLine();
                oufile = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename),"GBK")));
            }
            catch(Exception e)
            {
                oufile = null;
            }
            begintime = System.currentTimeMillis();
            PrimeTable[0] = 3;
            for (num = 2; num <= top; ++num)
            {
                if (TestPrime(num) == 1)
                {
                    if (oufile != null)
                        oufile.printf("%d,", num);
                    n++;
                }
            }
            endtime = System.currentTimeMillis();
            System.out.printf("在<=%d时共有%d个素数\n", top, n);
            if (oufile != null)
            {
                oufile.println();
                oufile.printf("在<=%d时共有%d个素数\n", top, n);
                oufile.close();
                System.out.printf("结果保存在:    %s\n", filename);
            }
            resulttime = endtime - begintime;
            System.out.printf("处理时间:      %d时%d分%d.%3d秒\n", resulttime / 3600000, (resulttime / 60000) % 60, (resulttime / 1000) % 60, resulttime % 1000);
        }
        static int[] PrimeTable = new int[6543];
        static short TableIndex = 0;
        static int TestPrime(int n)
        {
            int i = 0;
            if (n == 1)
                return 0;
            else if (n == 2)
                return 1;
            if (n % 2 == 0)
                return 0;
            for (i = 0; PrimeTable[i] * PrimeTable[i] <= n; i++)
            {
                if (n % PrimeTable[i] == 0)
                    return 0;
            }
            if (n <= 65521)
                PrimeTable[TableIndex++] = n;
            return 1;
        }
    }