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.
using System; public static class RecursiveFactorials { 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; } }
def factorial(n):# your code goes herereturn- using System;
- public static class RecursiveFactorials
- {
- 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;
- }
- }
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)); } } }
import codewars_test as test# TODO Write testsimport solution # or from solution import example- using NUnit.Framework;
- using System;
# test.assert_equals(actual, expected, [optional] message)@test.describe("Example")def test_group():@test.it("test case")def test_case():test.assert_equals(factorial(5), 120)test.assert_equals(factorial(6), 720)test.assert_equals(factorial(3), 6)- 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));
- }
- }
- }