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

  • 相关阅读:
    UOJ #276. 【清华集训2016】汽水
    Luogu P4585 [FJOI2015]火星商店问题
    Luogu P5416 [CTSC2016]时空旅行
    NOIP 2011 提高组初赛错题简析
    Luogu P4068 [SDOI2016]数字配对
    UOJ Easy Round #5
    Codechef September Challenge 2019 Division 2
    Project Euler Problem 675
    AtCoder Grand Contest 037
    拿2k的前端开发都会做些什么?
  • 原文地址:https://www.cnblogs.com/bby2014210552/p/9787520.html
Copyright © 2011-2022 走看看