Advertisement
Statev

FactorialFunction

Jan 16th, 2012
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace _10.FactorialFunction
  7. {
  8.     class FactorialFunction
  9.     {
  10.         public static string Sum(string number1, string number2)
  11.         {
  12.             string result = "";
  13.             int maxLength = Math.Max(number1.Length, number2.Length);
  14.             byte resultDigit = 0;
  15.             bool carryOne = false;
  16.             for (int i = 0; i < maxLength; i++)
  17.             {
  18.                 if (carryOne)
  19.                 {
  20.                     resultDigit++;
  21.                 }
  22.                 if (number1.Length - i > 0)
  23.                 {
  24.                     resultDigit += byte.Parse(number1[number1.Length - i - 1].ToString());
  25.                 }
  26.                 if (number2.Length - i > 0)
  27.                 {
  28.                     resultDigit += byte.Parse(number2[number2.Length - i - 1].ToString());
  29.                 }
  30.                 if (resultDigit > 9)
  31.                 {
  32.                     carryOne = true;
  33.                     resultDigit -= 10;
  34.                 }
  35.                 else
  36.                 {
  37.                     carryOne = false;
  38.                 }
  39.                 result = resultDigit + result;
  40.                 resultDigit = 0;
  41.             }
  42.             if (carryOne)
  43.             {
  44.                 result = "1" + result;
  45.             }
  46.             return result;
  47.         }
  48.  
  49.         static string Multiplication(string number, byte digit)
  50.         {
  51.             string result = "";
  52.             byte resultDigit = digit;
  53.             byte carryNumber = 0;
  54.             for (int i = number.Length - 1; i > -1; i--)
  55.             {
  56.                 resultDigit *= byte.Parse(number[i].ToString());
  57.                 resultDigit += carryNumber;
  58.                 if (resultDigit > 9)
  59.                 {
  60.                     carryNumber = (byte)(resultDigit / 10);
  61.                     resultDigit = (byte)(resultDigit % 10);
  62.                 }
  63.                 else
  64.                 {
  65.                     carryNumber = 0;
  66.                 }
  67.                 result = resultDigit.ToString() + result;
  68.                 resultDigit = digit;
  69.             }
  70.             if (carryNumber > 0)
  71.             {
  72.                 result = carryNumber.ToString() + result;
  73.             }
  74.             return result;
  75.         }
  76.  
  77.         static string Multiplication(string firstNumber, string secondNumber)
  78.         {
  79.             string result = "0";
  80.             string medialResult;
  81.             for (int i = 0; i < secondNumber.Length; i++)
  82.             {
  83.                 byte secondNumberDigit = byte.Parse(secondNumber[
  84.             secondNumber.Length - i - 1].ToString());
  85.                 medialResult = Multiplication(firstNumber, secondNumberDigit);
  86.                 medialResult = medialResult + new string('0', i);
  87.                 result = Sum(result, medialResult);
  88.             }
  89.             return result;
  90.         }
  91.  
  92.         static string Factorial(int n)
  93.         {
  94.             string result = "1";
  95.             for (int i = 2; i <= n; i++)
  96.             {
  97.                 result = Multiplication(result, i.ToString());
  98.             }
  99.             return result;
  100.         }
  101.  
  102.         static void Main(string[] args)
  103.         {
  104.             for (int i = 1; i < 101; i++)
  105.             {
  106.                 Console.WriteLine("{0}: {1}", i, Factorial(i));
  107.             }
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement