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;
                }
            }
        }

  • 相关阅读:
    一个人是否靠谱,闭环很重要(深度)
    远程通信的几种选择(RPC,Webservice,RMI,JMS的区别)
    如何量化考核技术人的 KPI?
    ECharts
    Spring IO Platform介绍
    百亿级日访问量的应用如何做缓存架构设计?
    大型分布式系统中的缓存架构
    Delphi实现屏幕截图、窗口截图、指定区域截图
    Delphi窗体重绘API
    GdiPlus 一个给 Delphi 提供的新的 GDI+ 接口很好用!
  • 原文地址:https://www.cnblogs.com/bby2014210552/p/9787520.html
Copyright © 2011-2022 走看看