GetSolutionOfEquation.cs

CSharp · Console

/Program.cs

using System;
					
public class Program
{
	public static int GetSolutionOfEquation(int n)
	{
		// для каждого простого делителя i числа n подсчитываем степень j, с которой он входит в n
		int res = 1;
		for (int i = 2; i <= Math.Sqrt(n); i++)
		{
			int j = 0;
			while (n % i == 0)
			{
				n /= i;
				j++;
			}
			
			res *= (2 * j + 1);
		}
		
		// Если n > 1 по окончанию цикла, 
		// то оно равно простому делителю исходного числа n. 
		// Этот делитель входит в n со степенью 1, 
		// следовательно res необходимо умножить на 2 * 1 + 1 = 3
		if (n > 1)
		{
			res *= 3;
		}
		return res;
	}	
	
	public static void Main()
	{
		Console.WriteLine("UniLecs");
		Console.WriteLine(string.Format("Answer = {0}", GetSolutionOfEquation(2)));  // 3
		Console.WriteLine(string.Format("Answer = {0}", GetSolutionOfEquation(12))); // 15
	}
}
;