# CodeChef – Small Factorials

```using System;
using System.Collections.Generic;
using System.Numerics;

public class Program
{
public static void Main()
{
List<BigInteger> data = new List<BigInteger>();

for(int i = 0; i < dataSize; i++)
{
}

foreach(BigInteger n in data)
{
Console.WriteLine(CalculateFactorial(n));
}
}

public static BigInteger CalculateFactorial(BigInteger n)
{
if(n > 1)
{
n = n * CalculateFactorial(n-1);
return n;
}
else
{
return n;
}
}
}```

So here’s a proper factorial calculator as opposed to the previous post entitled ‘Factorials.’ This code actually calculates the factorial of a number. I have to admit though that this code was not accepted by CodeChef because there is a compiling error due to the fact that CodeChef uses an older version of .Net in which the namespace Numerics does not exist. You can do this with doubles fine but the judge on CodeChef doesn’t accept scientific notation. And if you convert the scientific notation with format specifiers, the number still gets rounded up too much. Apparently there is a way to do this problem without Numerics and the BigInteger data type but I’m pretty satisfied with my solution regardless.

Here’s a link to the problem.

# CodeChef – Factorial

```using System;

public class Program
{
public static void Main()
{
double[] data = new double[dataSize];

for(int i = 0; i < dataSize; i++)
{
}

for(int i = 0; i < dataSize; i++)
{
Console.WriteLine(FindTrailingZeros(data[i]));
}
}

public static double FindTrailingZeros(double n)
{
double quotient = 1;
double sumOfQuotients = 0;
int power = 1;

while(true)
{
quotient = n / Math.Pow(5, power);
quotient = Math.Truncate(quotient);
power++;

if(quotient > 0)
{
sumOfQuotients += quotient;
}
else
{
return sumOfQuotients;
}
}
}
}```

Here’s my code for the Factorial problem on CodeChef, though I think the title doesn’t really describe the problem well. You are not supposed to find the factorial of a number but rather the number of trailing zeros of the factorial. My first instinct was to find the factorial, put the numbers in a string, and iterate through the string to check for zeros. The problem with that algorithm though is that factorials are huge! And moreover, an input could be as large as 1 billion which would produce an astronomically large number. So I read the editorial for the problem and they had a little trick for finding trailing zeros of a factorial which was way more efficient.

Here’s a link to the problem.