求素数

using System;
using System.Threading;
namespace ConsoleApplication1
{
    class Test
    {
        private static bool Check(int m)
        {
            int n = (int)(Math.Sqrt(m)) + 2;
            for(int i = 6; i < n; i+= 6)
                if(m % (i-1) == 0 || m % (i+1) == 0)
                    return false;
            return true;
        }
        static void TestNum()
        {
            Int32Collection result = new Int32Collection();
            result.Add(2);
            result.Add(3);
            int m = 30000;
            for(int i = 6; i < m; i+=6)
            {
                if(Check(i-1))
                    result.Add(i-1);
                if(Check(i+1))
                    result.Add(i+1);
            }
            System.Collections.IEnumerator myEnum = result.GetEnumerator();
            while(myEnum.MoveNext())
            {
                Console.Write(myEnum.Current.ToString() + "/t");
            }
            result.Clear();
        }
        static void Main()
        {
            Thread th = new Thread(new ThreadStart(TestNum));
            Thread th2 = new Thread(new ThreadStart(Table));
            th.Start();
            th2.Start();
        }
        static void Table()
        {
            Int32Collection result = Prime.Compute(30000);
            System.Collections.IEnumerator myEnum = result.GetEnumerator();
            while(myEnum.MoveNext())
            {
                Console.Write(myEnum.Current.ToString() + "/t");
            }
            Console.WriteLine("table over!");
            result.Clear();
        }
    }

using System;
using System.Collections;

namespace ConsoleApplication1
{
 /// <summary>
 /// Prime 的摘要说明。
 /// </summary>
    public class Prime
    {
        private static Int32Collection result = new Int32Collection();
        static Prime()
        {
            result.Add(2);
            result.Add(3);
            result.Add(5);
            result.Add(7);
        }
        /// <summary>
        /// 求 range 内的所有素数
        /// </summary>
        /// <param name="range">范围</param>
        /// <returns>Int32集合</returns>
        public static Int32Collection Compute(int range)
        {
            for(int i = 12; i < range; i += 6)
            {
                if(Check(i - 1))
                    result.Add(i - 1);
                if(Check(i + 1))
                    result.Add(i + 1);
            }
            return result;
        }
        /// <summary>
        /// num 是否为素数
        /// </summary>
        /// <param name="num">要判断的数 m</param>
        /// <returns>返回结果</returns>
        private static bool Check(int num)
        {
            int s = Round(num);
            for(int i = 0; i <= s; i++)
                if(num % result[i] == 0)
                    return false;
            return true;
        }
        /// <summary>
        /// 二分法求运算次数
        /// </summary>
        /// <param name="m">求 num 是否素数</param>
        /// <returns>次数</returns>
        private static int Round(int num)
        {
            int sqr = (int)Math.Sqrt(num);
            int big = result.Count;
            int small = 0;
            int post;
            while(big - small > 1)
            {
                post = (big + small)/2;
                if(result[post] > sqr)
                {
                    big = post;
                }
                else
                {
                    small = post;
                }
            }
            return small;
        }
    }
    /// <summary>
    /// Int32集合类
    /// </summary>
    public class Int32Collection :CollectionBase 
    {
        public Int32 this[int index]
        {
            get { return ((Int32) List[index]); }
            set { List[index] = value; }
        }
        public int Add(Int32 value)
        {
            return List.Add(value);
        }
        public int IndexOf(Int32 value)
        {
            return List.IndexOf(value);
        }
        public void Insert(int index, Int32 value)
        {
            List.Insert(index, value);
        }
        public void Remove(Int32 value)
        {
            List.Remove(value);
        }
        public bool Contains(Int32 value)
        {
            return List.Contains(value);
        }

        protected override void OnInsert(int index, object value)
        {
            if(value.GetType() != Type.GetType("System.Int32"))
                throw new ArgumentException("value must be of type Int32", "value");
            base.OnInsert (index, value);
        }
        protected override void OnRemove(int index, object value)
        {
            if(value.GetType() != Type.GetType("System.Int32"))
                throw new ArgumentException("value must be of type Int32", "value");
            base.OnRemove (index, value);
        }
        protected override void OnSet(int index, object oldValue, object newValue)
        {
            if(newValue.GetType() != Type.GetType("System.Int32"))
                throw new ArgumentException("value must be of type Int32", "newValue");
            base.OnSet (index, oldValue, newValue);
        }

        protected override void OnValidate(object value)
        {
            if(value.GetType() != Type.GetType("System.Int32"))
                throw new ArgumentException("value must be of type Int32", "value");
        }
    }

}

以下为VS2005下的测试,速度与2003一样

program.cs:

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace CalculatePrime
{
    class Program
    {
        private int range;
        static void Main(string[] args)
        {
            Program p = new Program();
            p.range = 30000;
            Thread th1 = new Thread(new ThreadStart(p.Com));
            Thread th2 = new Thread(new ThreadStart(p.Table));
            th2.Start();
            th1.Start();

        }
        private void Com()
        {
            Common com = new Common(range);
            List<int> result = com.Compute();
            foreach (int i in result)
            {
                Console.Write(i.ToString() + "/t");
            }
        }
        private void Table()
        {
            PrimeTable table = new PrimeTable(range);
            List<int> result = table.Compute();
            foreach (int i in result)
            {
                Console.Write(i.ToString() + "/t");
            }
            Console.WriteLine("table!");
        }
    }
}
Common.CS

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

namespace CalculatePrime
{
    class Common
    {
        private List<int> result;
        private int range;
        public int Range
        {
            get { return Range; }
        }
        public Common(int range)
        {
            this.range = range;
            result = new List<int>();
            result.Add(2);
            result.Add(3);
        }
        /// <summary>
        /// 计算素数集合
        /// </summary>
        /// <returns>返回集合</returns>
        public List<int> Compute()
        {
            for (int i = 6; i < range; i += 6)
            {
                if (Check(i - 1))
                    result.Add(i - 1);
                if (Check(i + 1))
                    result.Add(i + 1);
            }
            return result;
        }
        /// <summary>
        /// 判断M是否素数
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        private bool Check(int m)
        {
            int n = (int)(Math.Sqrt(m)) + 2;
            for (int i = 6; i < n; i += 6)
                if (m % (i - 1) == 0 || m % (i + 1) == 0)
                    return false;
            return true;
        }
    }
}
PrimeTable.CS

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

namespace CalculatePrime
{
    class PrimeTable
    {
        private List<int> result;
        private int range;
        public int Range
        {
            get { return range; }
        }
        public PrimeTable(int range)
        {
            this.range = range;
            result = new List<int>();
            result.Add(2);
            result.Add(3);
            result.Add(5);
            result.Add(7);
        }
        public List<int> Compute()
        {
            for(int i = 12; i < range; i += 6)
            {
                if(Check(i - 1))
                    result.Add(i - 1);
                if(Check(i + 1))
                    result.Add(i + 1);
            }
            return result;
        }
        /// <summary>
        /// num 是否为素数
        /// </summary>
        /// <param name="num">要判断的数 m</param>
        /// <returns>返回结果</returns>
        private bool Check(int num)
        {
            int s = Round(num);
            for(int i = 0; i <= s; i++)
                if(num % result[i] == 0)
                    return false;
            return true;
        }
        /// <summary>
        /// 二分法求运算次数(不用单写此方法(list提供了二分查找方法))
        /// </summary>
        /// <param name="m">求 num 是否素数</param>
        /// <returns>次数</returns>
        private int Round(int num)
        {
            int sqr = (int)Math.Sqrt(num);
            int big = result.Count;
            int small = 0;
            int post;
            while(big - small > 1)
            {
                post = (big + small)/2;
                if(result[post] > sqr)
                {
                    big = post;
                }
                else
                {
                    small = post;
                }
            }
            return small;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值