浙江大学PAT---Basic Level---1007----素数对猜想

题目:http://pat.zju.edu.cn/contests/pat-b-practise/1007

两种计算素数的方法:

<1>最一般的方法,理论合数n至少有一小于sqrt(n)的因子;

<2>筛选法,理论素数的倍数是合数,时间复杂度远远优于方法<1>.

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

namespace PAT_1007
{
    class Program
    {
        static void Main(string[] args)
        {
            //输入数据
            //
            Console.WriteLine("请输入数据:");
            int input,i,count=0;
            int[] prime1;
            bool[] prime2;

            
            //计算小于输入的素数
            //
            Stopwatch sw = new Stopwatch();
            sw.Start();

            input = Int32.Parse(Console.ReadLine());
            prime1 = Prime1(input);
            sw.Stop();
            Console.WriteLine("方法一耗时:{0}", sw.ElapsedTicks);

            sw.Restart();
            prime2 = Prime2(input);
            sw.Stop();
            Console.WriteLine("方法二耗时:{0}", sw.ElapsedTicks);
            
            
            //输出素数
            //
            i = 0;
            Console.WriteLine("素数:");
            while (prime1[i] != 0)
            {
                Console.Write("{0} ", prime1[i++]);
            }
            Console.Write("\n");

            Console.WriteLine("素数:");
            for (i = 0; i <= input; i++)
            {
                if (prime2[i])
                    Console.Write("{0} ", i);
            }
                Console.Write("\n");

            
            //计算素数对
            //
            count = 0;
            i = 1;
            while (prime1[i] != 0)
            {
                if ((prime1[i] - prime1[i - 1]) == 2)
                    count++;
                i++;
            }
            Console.Write("方法一素数对:");
            Console.WriteLine(count);

            count = 0;
            for (i = 2; i <= input; i++)
            {
                if (prime2[i] && prime2[i - 2])
                    count++;
            }
            Console.Write("方法二素数对:");
            Console.WriteLine(count);
            
            Console.ReadKey();
        }

        /// <summary>
        /// 最一般素数求解方法,理论:一个合数至少有一个小于sqrt(n)的因子
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        private static int[] Prime1(int n)
        {
            int[] prime = new int[n];
            int i = 0, j=0,count = 0;

            for (i = 2; i <= n; i++)
            {
                for (j = 2; j <= Math.Sqrt(i); j++)
                    if (i % j == 0) break;
                if (j > Math.Sqrt(i)) prime[count++] = i;  //这个prime[]是int型,跟下面讲的不同。
            }
            return prime;
        }

        /// <summary>
        /// 筛选法计算素数,理论:素数的倍数是合数
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        private static bool[] Prime2(int n)
        {
            bool[] prime = new bool[n + 1];

            for (int i = 0, j = 1; j <= n; i += 2, j += 2)
            {
                prime[i] = false;    //偶数
                prime[j] = true;     //奇数
            }

            for (int i = 3; i <= Math.Sqrt(n); i+=2)
            {
                if (prime[i])
                {
                    for (int j = i+i; j <= n; j += i)
                    {
                        prime[j] = false;
                    }
                }
            }
            prime[1] = false;
            prime[2] = true;

            return prime;
        }
    }
}


你可能感兴趣的:(浙江大学PAT---Basic Level---1007----素数对猜想)