C#编程:求出10000以内所有四位数是素数的个数cnt,再把所有满足此条件的四位数依次存入数组b中。求告知

外加要求:对数组b的四位数按从小到大的顺序进行排序,最后把结果输出。

https://zhidao.baidu.com/question/758417906051140284.html

using System;
using System.Collections.Generic;
using System.Linq;

class Test
{
    static void Main()
    {
        int[] array = num(1000, 10000).ToArray();
        Console.WriteLine("Count: {0}", array.Length);
        foreach (int num in array)
            Console.Write(num + " ");
        Console.WriteLine();
    }

    // https://stackoverflow.com/a/1510186/9606292
    static IEnumerable<int> num(int m, int n) =>
        Enumerable.Range(0, (int)Math.Floor(2.52 * Math.Sqrt(n) / Math.Log(n))).Aggregate(
            Enumerable.Range(2, n - 1).ToList(),
            (result, index) =>
            {
                var bp = result[index]; var sqr = bp * bp;
                result.RemoveAll(i => i >= sqr && i % bp == 0);
                return result;
            }
        ).Where(x => x > m);
}

温馨提示:答案为网友推荐,仅供参考
相似回答