pitchcontrol
12/11/2018 - 4:39 AM

Разложение числа на простые числа

Given a positive number n > 1 find the prime factor decomposition of n. The result will be a string with the following form : "(p1n1)(p2n2)...(pk**nk)"

public static String factors(int lst)
{
    var primes = new List<string>();
    for (var number = 2; number <= lst; number++)
    {
        var count = 0;
        while (lst % number == 0)
        {
            count++;
            lst /= number;
        }

        if (count == 0) continue;
        primes.Add(String.Format(count > 1 ? "({0}**{1})" : "({0})", number, count));
    }

    return String.Join("", primes);
}