You are not logged in. Please login at www.codechef.com to post your questions!

×

Small factorials in C#

I spent some time looking but was unable to find a successful submission for the Small Factorials practice written in C#. When I submit the following code it comes up with "Wrong Answer" despite it working on my desktop and being tested using files as mentioned in the FAQ section. The test cases presented in the explanation also seem to work correctly.

My hypothesis is that the test cases are using a very large factorial number, e.g. 25!,50! or 67!, which is causing my program to output an incorrect number. I am using the ulong data type which is the second to largest in .NET. I can't use the BigInteger type because it isn't included in the default library types.

If I am incorrect could someone please point out the flaw in my code or let me know where I made a mistake?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Small_Factorials
{
class Program
 { 
static void Main(string[] args) 
        {
            int iterations = 0;
            ulong factorialTotal = 0;
            string interateString = string.Empty;
            string numberString = string.Empty;

            interateString = Console.ReadLine();
            int.TryParse(interateString, out iterations);

            for (int i = iterations; i > 0; i--)
            {
                numberString = Console.ReadLine();

                factorialTotal = CalculateFactorial(numberString);

                Console.WriteLine(factorialTotal.ToString());
            }
        }
        static ulong CalculateFactorial(string number)
        {
            ulong countDown = 0;
            ulong factorialTotal = 1;

            ulong.TryParse(number, out countDown);

            for(ulong i = 1; i <= countDown; i++)
            {
                factorialTotal *= i;
            }
            return factorialTotal;
        }
    }
}

Thank you.

asked 13 Jun '12, 03:35

praetorian's gravatar image

0★praetorian
1111
accept rate: 0%

edited 22 Jun '12, 14:45

admin's gravatar image

0★admin ♦♦
19.3k348495534

statement says: "1<=n<=100" so there can be test case where n = 100, so your hypothesis is correct. I do not know if you can use BigInteger here, in Java you can. Another approach is to implement multiplication as you learned it in school:

  12
x 34
====
  48
 36
====
 408
(13 Jun '12, 19:53) betlista ♦♦3★

Why not using BigIntegers ? Are you sure it isn't in a reachable namespace ?

http://msdn.microsoft.com/fr-fr/library/system.numerics.biginteger.aspx

link

answered 22 Jun '12, 23:13

cyberax's gravatar image

2★cyberax ♦
3.4k21955
accept rate: 20%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×178
×32

question asked: 13 Jun '12, 03:35

question was seen: 6,484 times

last updated: 22 Jun '12, 23:13