CodeChef – Small Factorials

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

public class Program
{
	public static void Main()
	{
		int dataSize = int.Parse(Console.ReadLine());
		List<BigInteger> data = new List<BigInteger>();
		
		for(int i = 0; i < dataSize; i++)
		{
			data.Add(BigInteger.Parse(Console.ReadLine()));
		}
		
		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()
	{
		int dataSize = int.Parse(Console.ReadLine());
		double[] data = new double[dataSize];
		
		for(int i = 0; i < dataSize; i++)
		{
			data[i] = double.Parse(Console.ReadLine());
		}
		
		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.

CodeChef-Enormous Input Test

using System;

public class Program
{
   public static void Main()
   {
      string lines = Console.ReadLine();
      string[] splitLines = lines.Split(' ');

      int n = int.Parse(splitLines[0]);
      int k = int.Parse(splitLines[1]);
      int[] t = new int[n];
      int output = 0;

      for(int i = 0; i < n; i++)
      {
         t[i] = int.Parse(Console.ReadLine());
      }

      foreach(int i in t)
      {
         if(i % k == 0)
         {
            output++;
         }
      }

      Console.WriteLine(output);
   }
}

I’m now officially addicted to CodeChef. These problems are so much fun. This is my solution to another beginner problem. In this one, the user inputs two integers separated by a space(hints the split with a ‘ ‘ on line 8). The first integer is ‘n’ and it indicates how many numbers are going to be tested. The second integer is ‘k’ and this is going to be used to find out if each subsequent integer is a multiple of ‘k’.

Here’s a link to the problem.

CodeChef-ATM

using System;

public class Test
{
   public static void Main()
   {
      string line = Console.ReadLine();
      string[] splitLines = line.Split(' ');

      double withdrawal = double.Parse(splitLines[0]);
      double balance = double.Parse(splitLines[1]);

      if(withdrawal % 5 == 0 && balance >= withdrawal + 0.5)
      {
         double updatedBalance = balance - withdrawal - 0.5;
         Console.WriteLine(updatedBalance.ToString("F"));
      }
      else
      {
         Console.WriteLine(balance.ToString("F"));
      }
   }
}

Here’s my solution to the ATM problem on CodeChef.  The user has to input a withdrawal amount and a balance amount on the same line(Why? IDK).  There is an ATM usage fee of $0.50 that must be added to the withdrawal.  The withdrawal of course cannot exceed the balance and must be a multiple of 5.  Simple may this problem be, I actually felt like I learned few things from doing it.

  1. Split() is a really nifty method for when you need to split strings.
  2. Format specifiers, in this case “F”, are really nifty for when you need to specify precision.
  3. Try not to get big-headed even when doing simple tasks.  Next thing you know, you’ll have a logical brain fart, get frustrated, and slip into an existential crisis.

Here’s the link to the problem.

CodeChef

I’ve been looking for a site to practice some programming problems when I came across CodeChef.  It has tons of problems ranging from beginner to advanced.  Some of the more advanced problems are what I would imagine in a coding interview.  You have to make an account but it’s all free.  There are also contests and rankings and things.  It’s pretty convenient too because once you submit your solution, it tells you right away rather you solved it or not as the process is automated.  The following code is my solution to an easy problem I did to test it out.  All it is, is a program to print integers to the console until the number 42 is inputted.  And here’s a link to the problem.

using System;

public class Program
{
   public static void Main()
   {
      int input = 0;

      while(input != 42)
      {
         input = int.Parse(Console.ReadLine());

         if(input != 42)
         {
            Console.WriteLine(input);
         }
      }
   }
}

The IDE on the site has been super buggy for me.  It almost made me look elsewhere for some coding problems.  But I decided to try and just use another online IDE and just copy and paste my solution.  Once I did that, it worked quite nicely.  So I think I’ll stick with this site.  Other than the buggy IDE it seems really good.  The IDE I started using is .NetFiddle.