Week 153: task 1: Left factorials
We are referred to the OEIS for a definition, which is given as:
!n = Sum_{k=0..n-1} k!
A little thought shows that can also be expressed as:
!n = !(n-1) + (n-1)!
which is pleasingly symmetrical and also (marginally) more efficient when we're generating the numbers sequentially.
But even with my rather verbose style of coding and generating the output in the required format my solution is only 9 lines of code.
Task 2: factorions
I was tempted to submit
say 'no';
which is nearly always correct. According to my algorithm 1, 2, 145 and 40585 are the only factorions under 100k, and these nice chaps at OEIS state that these are indeed the only 4. So
say $n =~ m/^(1|2|145|40585)$/ ? 'yes' : 'no';
would do. However, given my habit of producing the output which Mohammad has specified I did it properly, for example
Input: 40585
Output: 1 since 4! + 0! + 5! + 8! + 5! => 24 + 1 + 120 + 40320 + 120 = 40585
Input: 57778
Output: 0 since 5! + 7! + 7! + 7! + 8! => 120 + 5040 + 5040 + 5040 + 40320 = 55560 <> 57778
No comments:
Post a Comment