evgeni_tsn

Untitled

Sep 23rd, 2015
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.89 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace _01.Prime_Factorization
  5. {
  6.     class Program
  7.     {
  8.         static void Main()
  9.         {
  10.             int n;
  11.             while (true)
  12.             {
  13.                 n = int.Parse(Console.ReadLine());
  14.                 if (n >= 2) break;
  15.             }
  16.             Console.Write("{0} = ", n);
  17.             Console.WriteLine(FindPrimes(n));
  18.         }
  19.  
  20.         private static string FindPrimes(int n)
  21.         {
  22.             List<int> primes = new List<int>();
  23.             int counter = 2;
  24.             do
  25.             {
  26.                 if (n % counter == 0)
  27.                 {
  28.                     primes.Add(counter);
  29.                     n /= counter;
  30.                     continue;
  31.                 }
  32.                 counter++;
  33.             }
  34.             while (n != 1);
  35.             return string.Join(" * ", primes);
  36.         }
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment