Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

distinct prime factors count of a number

       public static int PrimeFactorsCount(int n)
        {
            if(n==1)
            {
                return 0;
            }
            var num = 0;
            if (n % 2 == 0)
            {
                num++;

                while (n % 2 == 0)
                {
                    n /= 2;
                }
            }

            for (int i = 3; i <= Math.Sqrt(n); i += 2)
            {
                if (n % i == 0)
                {
                    num++;

                    while (n % i == 0)
                    {
                        n /= i;
                    }
                }
            }

            if (n > 2)
                num++;
            return num;
       }
 
PREVIOUS NEXT
Tagged: #distinct #prime #factors #count #number
ADD COMMENT
Topic
Name
4+1 =