DEV Community

Simon Green
Simon Green

Posted on

Factorials everywhere!

Weekly challenge 153

Challenge, My solutions

Two relatively straight forward tasks this week. Solutions are in Python only as a result.

TASK #1 › Left Factorials

Task

Write a script to compute Left Factorials of 1 to 10. Please refer OEIS A003422 for more information.

My solution

So after reading the linked OEIS page, I was really confused. However we can look at the solution to find a straight forward pattern, the difference between the digits is the factorials, that is S(n) = S(n-1) + (n-1)!. The OEIS list starts the list at 0, but the expected output starts at 1.

Example

$ ./ch-1.py 
1, 2, 4, 10, 34, 154, 874, 5914, 46234, 409114
Enter fullscreen mode Exit fullscreen mode

TASK #2 › Factorions

Task

You are given an integer $n.

Write a script to figure out if the given integer is factorion. A factorion is a natural number that equals the sum of the factorials of its digits.

My solution

For this task, we start with n as a string. I then use num as the integer representation, checking that the value is not a negative. Then it is a matter of using sum(math.factorial(int(x)) for x in n) to compute the sum of all factorials of the digits/ Finally compare that figure with num and print 1 if they are the same or 0 otherwise.

Examples

$ ./ch-2.py 145
1

$ ./ch-2.py 123
0
Enter fullscreen mode Exit fullscreen mode

Discussion (0)