[Issue 13371] New: std.math.factorial
via Digitalmars-d-bugs
digitalmars-d-bugs at puremagic.com
Sun Aug 24 09:29:05 PDT 2014
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 at puremagic.com
Reporter: bearophile_hugs at 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.
--
More information about the Digitalmars-d-bugs
mailing list