Search code examples
code-golf

Code Golf: Prime Factors of a Number


What is the shortest way, by character count, to find prime factors in any number?

Example Input: 1806046

Example Output: 2x11x11x17x439

Example Calculator


Solution

  • C#, 69

    x is input number

    int i=2;while(x>1)if(x%i++==0){x/=--i;Console.Write(i+(x>1?"x":""));};
    

    With includes:

    using system;
    namespace nameSP
    {
       class Program
       {
         static void Main(string[] args)
         { 
            int i=2;while(x>1)if(x%i++==0){x/=--i;Console.Write(i+(x>1?"x":""));};
         }
       }
    }