2019-03-21 11:52:35 +05:30
---
id: 5a23c84252665b21eecc7ee0
title: Left factorials
challengeType: 5
2019-08-05 09:17:33 -07:00
forumTopicId: 302302
2019-03-21 11:52:35 +05:30
---
## Description
2020-03-30 11:23:18 -05:00
2019-03-21 11:52:35 +05:30
< section id = 'description' >
< b > Left factorials< / b > , $ !n $, may refer to either < i > subfactorials< / i > or to < i > factorial sums< / i > . The same notation can be confusingly seen used for the two different definitions. Sometimes, < i > subfactorials< / i > (also known as < i > derangements< / i > ) may use any of the notations:
< ul >
2019-05-23 13:57:59 +09:00
< li > $!n`$< / li >
< li > $!n$< / li >
< li > $n¡$< / li >
2019-03-21 11:52:35 +05:30
< / ul >
(It may not be visually obvious, but the last example uses an upside-down exclamation mark.) This task will be using this formula for < b > left factorial< / b > :
$ !n = \sum_{k=0}^{n-1} k! $
where $!0 = 0$
< / section >
## Instructions
2020-03-30 11:23:18 -05:00
2019-03-21 11:52:35 +05:30
< section id = 'instructions' >
Write a function to calculate the left factorial of a given number.
< / section >
## Tests
2020-03-30 11:23:18 -05:00
2019-03-21 11:52:35 +05:30
< section id = 'tests' >
2020-03-30 11:23:18 -05:00
```yml
2019-03-21 11:52:35 +05:30
tests:
- text: < code > leftFactorial</ code > should be a function.
2020-03-30 11:23:18 -05:00
testString: assert(typeof leftFactorial == 'function');
2019-03-21 11:52:35 +05:30
- text: < code > leftFactorial(0)</ code > should return a number.
2020-03-30 11:23:18 -05:00
testString: assert(typeof leftFactorial(0) == 'number');
2019-03-21 11:52:35 +05:30
- text: < code > leftFactorial(0)</ code > should return < code > 0</ code > .
2020-03-30 11:23:18 -05:00
testString: assert.equal(leftFactorial(0), 0);
2019-03-21 11:52:35 +05:30
- text: < code > leftFactorial(1)</ code > should return < code > 1</ code > .
2020-03-30 11:23:18 -05:00
testString: assert.equal(leftFactorial(1), 1);
2019-03-21 11:52:35 +05:30
- text: < code > leftFactorial(2)</ code > should return < code > 2</ code > .
2020-03-30 11:23:18 -05:00
testString: assert.equal(leftFactorial(2), 2);
2019-03-21 11:52:35 +05:30
- text: < code > leftFactorial(3)</ code > should return < code > 4</ code > .
2020-03-30 11:23:18 -05:00
testString: assert.equal(leftFactorial(3), 4);
2019-03-21 11:52:35 +05:30
- text: < code > leftFactorial(10)</ code > should return < code > 409114</ code > .
2020-03-30 11:23:18 -05:00
testString: assert.equal(leftFactorial(10), 409114);
2019-03-21 11:52:35 +05:30
- text: < code > leftFactorial(17)</ code > should return < code > 22324392524314</ code > .
2020-03-30 11:23:18 -05:00
testString: assert.equal(leftFactorial(17), 22324392524314);
2019-03-21 11:52:35 +05:30
- text: < code > leftFactorial(19)</ code > should return < code > 6780385526348314</ code > .
2020-03-30 11:23:18 -05:00
testString: assert.equal(leftFactorial(19), 6780385526348314);
2019-03-21 11:52:35 +05:30
```
< / section >
## Challenge Seed
2020-03-30 11:23:18 -05:00
2019-03-21 11:52:35 +05:30
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
function leftFactorial(n) {
// Good luck!
}
```
< / div >
< / section >
## Solution
2020-03-30 11:23:18 -05:00
2019-03-21 11:52:35 +05:30
< section id = 'solution' >
```js
function leftFactorial(n) {
2020-03-30 11:23:18 -05:00
if (n == 0) return 0;
if (n == 1) return 1;
// Note: for n>=20, the result may not be correct.
// This is because JavaScript uses 53 bit integers and
// for n>=20 result becomes too large.
let res = 2,
fact = 2;
for (var i = 2; i < n ; i + + ) {
res += fact;
fact *= i + 1;
}
return res;
2019-03-21 11:52:35 +05:30
}
```
2019-03-28 09:35:41 +01:00
< / section >