Exemplo n.º 1
0
        public string ShowFactors(int inputNumber)
        {
            Eratosthenes eratosthenes = new Eratosthenes();

            IEnumerable <int> factors = GetPrimeFactors(inputNumber, eratosthenes);

            string finalFactors = "";

            foreach (int i in factors)
            {
                finalFactors += $"{i.ToString()}";
            }
            return(finalFactors);      // Outputs "2 2 2 3 5"
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            Eratosthenes eratosthenes = new Eratosthenes();

            IEnumerable <int> factors = GetPrimeFactors(50, eratosthenes);

            string itesting = "";

            foreach (int i in factors)
            {
                //Console.Write("{0} ", i);   // Outputs "2 2 2 3 5"
                itesting += $"{i.ToString()} ";
            }

            WriteLine(itesting);
        }
Exemplo n.º 3
0
        private static IEnumerable <int> GetPrimeFactors(int value, Eratosthenes eratosthenes)
        {
            List <int> factors = new List <int>();

            foreach (int prime in eratosthenes)
            {
                while (value % prime == 0)
                {
                    value /= prime;
                    factors.Add(prime);
                }

                if (value == 1)
                {
                    break;
                }
            }

            return(factors);
        }