www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 13371] New: std.math.factorial

https://issues.dlang.org/show_bug.cgi?id=13371

          Issue ID: 13371
           Summary: std.math.factorial
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: Phobos
          Assignee: nobody puremagic.com
          Reporter: bearophile_hugs eml.cc

I suggest to add to Phobos a commonly useful function similar to this (also
present in the Python standard library):



import std.traits: isAssignable;

T factorial(T)(in T n)
if (__traits(compiles, T(1) < T(1)) &&
    __traits(compiles, T(1) + 1) &&
    __traits(compiles, T(1) * T(1)) &&
    isAssignable!T)
in {
    assert(n >= 0);
} body {
    auto result = T(1);
    for (auto i = T(1); i <= n; i = i + 1)
        result = result + result * i;
    return result;
}


Aternatively the index 'i' can be of type integer or uint. In this case the
template constraints needs to be modified.

--
Aug 24 2014