Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
1.67/5 (3 votes)
See more:
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:

C#
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.
Posted
Updated 28-May-11 21:29pm
v4
Comments
[no name] 29-May-11 0:16am    
What do you mean by "optimized code" and why should we do your homework for you?
Member 7962726 29-May-11 3:06am    
Dear mark,

i have done the coding but higher values are taking time to processed. I think i am missing something in my code. Please check it that how can i make it faster? thank you

There are many ways to do that, for example you can get the prime factors looping through all the numbers from 2 to the square root of 'n' then if that number divides n you add it to another variable,
another way to do it is using the sieve of erastotenes and then you search in the sieve from 2 to the square root of 'n' and add it to a variable 'sum' if the number divides 'n' and it's prime
a link from wikipedia
http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes[^]
 
Share this answer
 
v2
Comments
Kim Togo 29-May-11 0:54am    
Good advice. My 5.
We're not doing your homework for you. YOU have to write the code. When you get stuck, you can ask specific questions about what you're having a problem with.
 
Share this answer
 
Comments
Member 7962726 29-May-11 3:03am    
Dear dave,

i have posted my code and its problem. Please give me your expert comments. thanks
Dave Kreskowiak 29-May-11 11:11am    
You can down vote me all you want. It's not going to get me to write your code for you.

But, I will say, now that you've posted your code, that your factorizing code needs a complete rewrite. Your Prime_Fact code should return exactly what its name implies, prime factors, not a sum of them. It's returning a single integer value when it should be return an array or collection of them.

When you have the array or collection, then it's trivial to add them together.

Also, if you want to handle large values, I'd start by using long values in the factorizer instead of integers.

I'd also put my factorizer in its own class, probably starting something like this:

public class FactorizerLong : List<long>
{
public FactorizerLong(long value)
{
this.Factorize(value);
}

private void Factorize(long value)
{
// Factorizing code goes here

// When you find a value, add it to the collection like this:
base.Add(newValue);
}
}

And then I would seriously look into the Sieve of Eratosthenes. You can rewrite your sieze without using a single goto statement.
Dave Kreskowiak 29-May-11 11:25am    
You may alsio want to Google for "find prime factors". You'll find what you're looking for pretty easily.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900