Advertisement
aarono

C# Prime Factorisation

Feb 5th, 2013
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.68 KB | None | 0 0
  1. public static int[] Factorise(params int[] input)
  2. {
  3.     List<int> output = new List<int>();
  4.     bool performedAny = false;
  5.  
  6.     for (int i = 0; i < input.Length; i++)
  7.     {
  8.         int number = Math.Abs(input[i]);
  9.         bool performedThis = false;
  10.  
  11.         for (int j = number - 1; j > 1; j--)
  12.         {
  13.             if (number % j == 0)
  14.             {
  15.                 output.Add(j);
  16.                 output.Add(number / j);
  17.  
  18.                 performedThis = true;
  19.                 performedAny = true;
  20.  
  21.                 break;
  22.             }
  23.         }
  24.  
  25.         if (!performedThis)
  26.         {
  27.             output.Add(number);
  28.         }
  29.     }
  30.  
  31.     output.Sort();
  32.     int[] outArray = output.ToArray();
  33.  
  34.     if (input.Length > 0 && input[0] < 0)
  35.     {
  36.         outArray[0] = -outArray[0];
  37.     }
  38.  
  39.     return performedAny ? Factorise(outArray) : outArray;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement