DEV Community

Simon Green
Simon Green

Posted on

2 2

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

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay