The factorial of a positive integer n, denoted by n!, is the product of all positive integers less then or equal than to n:
n! = n(n-1)(n-2)...(2)(1)
5! = (5)(4)(3)(2)(1) = 120
By convenction, 0! equals to 1.
Calculate the initial number of a given facturial result.
using System; public static class ReverseFactorial { public static int Kata(long n) { if(n == 0) return 1; for (long i = 2; n / i > 1; i++) { n /= i; } return (int) n; } }
- using System;
public static class RecursiveFactorials- public static class ReverseFactorial
- {
- public static int Kata(long n)
- {
int i = 1;if (n != 0){while (n / i != 1){n /= i;i++;}} else{n = i;}return (int) n;- if(n == 0) return 1;
- for (long i = 2; n / i > 1; i++)
- {
- n /= i;
- }
- return (int) n;
- }
- }
using NUnit.Framework; using System; namespace Solution { [TestFixture] public class SolutionTest { [Test] public void SimpleTests() { Assert.AreEqual(1, ReverseFactorial.Kata(0)); Assert.AreEqual(1, ReverseFactorial.Kata(1)); Assert.AreEqual(2, ReverseFactorial.Kata(2)); Assert.AreEqual(3, ReverseFactorial.Kata(6)); Assert.AreEqual(5, ReverseFactorial.Kata(120)); Assert.AreEqual(20, ReverseFactorial.Kata(2432902008176640000)); } } }
- using NUnit.Framework;
- using System;
- namespace Solution {
- [TestFixture]
- public class SolutionTest
- {
- [Test]
- public void SimpleTests()
- {
Assert.AreEqual(1, RecursiveFactorials.Kata(0));Assert.AreEqual(1, RecursiveFactorials.Kata(1));Assert.AreEqual(2, RecursiveFactorials.Kata(2));Assert.AreEqual(3, RecursiveFactorials.Kata(6));Assert.AreEqual(5, RecursiveFactorials.Kata(120));Assert.AreEqual(20, RecursiveFactorials.Kata(2432902008176640000));- Assert.AreEqual(1, ReverseFactorial.Kata(0));
- Assert.AreEqual(1, ReverseFactorial.Kata(1));
- Assert.AreEqual(2, ReverseFactorial.Kata(2));
- Assert.AreEqual(3, ReverseFactorial.Kata(6));
- Assert.AreEqual(5, ReverseFactorial.Kata(120));
- Assert.AreEqual(20, ReverseFactorial.Kata(2432902008176640000));
- }
- }
- }