loner_li 机试题 打印出2至99之间的所有素数(即不能被任何数整除的数)。

  class Program
    {
        public  static void Main(string[] args)
        {
            int i;
            for (i = 2; i <= 99; i++)
            {
                if (IsSushu(i))
                {
                    Console.Write("{0}",i);
                }
            }
            Console.ReadLine();
        }
        static bool IsSushu(int n)
        {
            bool b = true;
            if (n == 1 || n == 2)
            {
                b = true;
            }
            else
            {
                int i, s;
                s = (int)System.Math.Sqrt(n);
                for ( i = s; i > 1; i--)
                {
                    if (n%i==0)
                    {
                        b = false;
                        break;
                    }
                }
            }
            return b;
        }
    }

你可能感兴趣的:(loner_li 机试题 打印出2至99之间的所有素数(即不能被任何数整除的数)。)