Filkolev

Prime Factorization

May 27th, 2015
286
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class PrimeFactorization
  5. {
  6.     public static void Main()
  7.     {
  8.         int numberToFactorize = int.Parse(Console.ReadLine());
  9.         Console.Write("{0} = ", numberToFactorize);
  10.  
  11.         int primeMultiple = 1;
  12.  
  13.         List<int> primeFactors = new List<int>();
  14.  
  15.         while (numberToFactorize != 1)
  16.         {
  17.             primeMultiple++;
  18.  
  19.             while (numberToFactorize % primeMultiple == 0)
  20.             {
  21.                 primeFactors.Add(primeMultiple);
  22.                 numberToFactorize /= primeMultiple;
  23.             }
  24.         }
  25.  
  26.         Console.WriteLine(string.Join(" * ", primeFactors));
  27.     }
  28. }
RAW Paste Data