zoukankan      html  css  js  c++  java
  • c#实现:返回n到m之间的所有素数

     class Program
        {
            static void Main(string[] args)
            {
                Sushu();
            }
            public static void Sushu()
            {
                Console.WriteLine("请输入求素数的起始数字用','隔开:");
                string y = Console.ReadLine();
                Console.WriteLine("这个范围内的素数有:{0}", CalPrim(int.Parse(y.Split(',')[0]), int.Parse(y.Split(',')[1]), ""));   
            }
            public static string CalPrim(int x, int y, string sushu)
            {
                bool check = true;//是素数
                for (int i = 2; i < x; i++)
                {
                    if (x % i == 0)
                    {
                        check = false;
                    }
                }

                if (check == true && x < y)
                {
                    return CalPrim(x + 1, y, sushu += x + "、");
                }
                else if (check == false && x < y)
                {
                    return CalPrim(x + 1, y, sushu);
                }
                else
                {
                    return sushu;
                }
            }
            public static string GetSuShu2(int x, int y, string sushu)
            {
                int count = 0;
                for (int i = 1; i<=x; i++)
                {
                    if (x%i==0)
        {
             count++;
        }
                    if(count>2)
                        return CalPrim(x+1,y,sushu);
                }
                if(x<y){
                    return CalPrim(x+1,y,sushu
                        +=x+"、");
                }
                else{
                    return sushu;
                }
            }
        }

  • 相关阅读:
    MVC中使用jquery的浏览器缓存问题
    3 工厂方法模式
    2 简单工厂模式
    1 单例模式
    Sqlserver With as
    Memcache的使用
    mysql的分区和分表
    MySQL主从复制与读写分离
    SqlServer 表分区
    SqlServer 2012 AlwaysOn
  • 原文地址:https://www.cnblogs.com/bby2014210552/p/9787520.html
Copyright © 2011-2022 走看看