Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace _10.FactorialFunction
- {
- class FactorialFunction
- {
- public static string Sum(string number1, string number2)
- {
- string result = "";
- int maxLength = Math.Max(number1.Length, number2.Length);
- byte resultDigit = 0;
- bool carryOne = false;
- for (int i = 0; i < maxLength; i++)
- {
- if (carryOne)
- {
- resultDigit++;
- }
- if (number1.Length - i > 0)
- {
- resultDigit += byte.Parse(number1[number1.Length - i - 1].ToString());
- }
- if (number2.Length - i > 0)
- {
- resultDigit += byte.Parse(number2[number2.Length - i - 1].ToString());
- }
- if (resultDigit > 9)
- {
- carryOne = true;
- resultDigit -= 10;
- }
- else
- {
- carryOne = false;
- }
- result = resultDigit + result;
- resultDigit = 0;
- }
- if (carryOne)
- {
- result = "1" + result;
- }
- return result;
- }
- static string Multiplication(string number, byte digit)
- {
- string result = "";
- byte resultDigit = digit;
- byte carryNumber = 0;
- for (int i = number.Length - 1; i > -1; i--)
- {
- resultDigit *= byte.Parse(number[i].ToString());
- resultDigit += carryNumber;
- if (resultDigit > 9)
- {
- carryNumber = (byte)(resultDigit / 10);
- resultDigit = (byte)(resultDigit % 10);
- }
- else
- {
- carryNumber = 0;
- }
- result = resultDigit.ToString() + result;
- resultDigit = digit;
- }
- if (carryNumber > 0)
- {
- result = carryNumber.ToString() + result;
- }
- return result;
- }
- static string Multiplication(string firstNumber, string secondNumber)
- {
- string result = "0";
- string medialResult;
- for (int i = 0; i < secondNumber.Length; i++)
- {
- byte secondNumberDigit = byte.Parse(secondNumber[
- secondNumber.Length - i - 1].ToString());
- medialResult = Multiplication(firstNumber, secondNumberDigit);
- medialResult = medialResult + new string('0', i);
- result = Sum(result, medialResult);
- }
- return result;
- }
- static string Factorial(int n)
- {
- string result = "1";
- for (int i = 2; i <= n; i++)
- {
- result = Multiplication(result, i.ToString());
- }
- return result;
- }
- static void Main(string[] args)
- {
- for (int i = 1; i < 101; i++)
- {
- Console.WriteLine("{0}: {1}", i, Factorial(i));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement