please give me your solution. i have tried to find the optimized answer on google but i dint find any optimized code.
Q:find sum of prime factors of given number. for example if n=18 answer would be 2+3=5

below is my code:

class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            Console.WriteLine("Please provide Number : ");
            int num=Convert.ToInt32(Console.ReadLine());
            int sumRet = p.Prime_Fact(num);
            Console.WriteLine("Desired Result :{0} ",sumRet);

        }

        public int Prime_Fact(int n)
        {
            int originalNum=n;
            int sum=0;
            int primeLoop = 2;
            int check;
           g1: if (primeLoop < originalNum/2)
            {
                if (n % primeLoop == 0)
                {
                    sum = sum + primeLoop;
                    n = n / primeLoop;
                }
            g2: primeLoop++;
                if ((check = Prime_Fact(primeLoop)) == 0)
                {
                    goto g1;
                }
                else
                {
                    goto g2;
                }
            }


            return sum;
        }
    }

i need to optimize it. it is not passing some test cases also. test cases are not given to me. Please give me your suggetions.

If this is supposed to be C# it will not compile.
For a starter change &quot; in " and &lt; in <

Edited 5 Years Ago by ddanbe: n/a

This article has been dead for over six months. Start a new discussion instead.