Beispiel #1
0
        internal static List <int> PrimeFactors(int x)
        {
            List <int> pf = new List <int>();

            //if even, loop till not even any more
            while (IsEven(x))
            {
                //Console.WriteLine(x);
                x >>= 1; // shift right is divide by zero
                pf.Add(2);
            }

            // now handle odd
            // start with the smallest odd prime
            // and work upto half of number
            PrimeProvider pp = new PrimeProvider();

            foreach (int p in pp.StepPrimes())
            {
                //Console.WriteLine($"x : {x} :: p: {p}");
                if (p > x)
                {
                    break;
                }
                while (x % p == 0)
                {
                    x /= p;
                    pf.Add(p);
                }
            }
            return(pf);
        }
Beispiel #2
0
        internal static void Test_YieldConcept()
        {
            PrimeProvider pp = new PrimeProvider();

            foreach (int i in pp.Even())
            {
                Console.WriteLine(i);
                if (i > 100)
                {
                    break;
                }
            }
        }
Beispiel #3
0
        internal static void Test_StepPrime()
        {
            PrimeProvider pp = new PrimeProvider();

            foreach (int i in pp.StepPrimes())
            {
                Console.WriteLine($"i : {i}");
                if (i > 100)
                {
                    break;
                }
            }
        }