Prime Factors In C#

后端 未结 6 1862
无人共我
无人共我 2020-12-09 04:41

I want to create a program in C# 2005 which calculates prime factors of a given input. i want to use the basic and simplest things, no need to create a method for it nor arr

6条回答
  •  一生所求
    2020-12-09 05:23

    using static System.Console;
    
    namespace CodeX
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                for (int i = 0; i < 20; i++)
                {
                    if (IsPrime(i))
                    {
                        Write($"{i} ");
                    }
                }
            }
    
            private static bool IsPrime(int number)
            {
                if (number <= 1) return false;  // prime numbers are greater than 1
    
                for (int i = 2; i < number; i++)
                {
                    // only if is not a product of two natural numbers
                    if (number % i == 0)       
                        return false;
                }
    
                return true;
            }
        }
    }
    

提交回复
热议问题