n is called fair if the sum of the factorial of its digits is divisible by itself. 145 is one such number : 1 ! + 4 ! + 5 ! = 1 4 5 ≡ 0 ( m o d 1 4 5 ) Find the sum of all the fair numbers less than 1 0 7 .
A positive integer
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
for the seek of optimization only.
let f ( n ) be the sum of the factorial of all the digits of n . we can easily that : f ( n ) ≤ 9 ! × ( l o g ( n ) + 1 ) n ≤ f ( n )
So that n must be less that a certain value(in fact 6028144).
Solution in python 3.4:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
1 2 3 4 5 6 7 8 9 10 |
|
It can be shown that the sequence is finite, with a maximum value of 40585.
Log in to reply
Please provide a reference.
Log in to reply
We're looking for numbers n such that the sum of the factorials of the digits ( f ( n ) ) is greater than or equal to n . The largest value of f ( n ) for a d digit n occurs when n is all 9's ( f ( 9 9 9 9 ) = 4 ∗ 9 ! = 1 4 5 1 5 2 0 ). f ( 9 9 9 9 9 9 9 9 ) = 8 ∗ 9 ! = 2 9 0 3 0 4 0 , a seven digit number. Since any larger sequence of 9's must have a larger digit sum, we conclude that 7 ∗ 9 ! = 2 5 4 0 1 6 0 is an upper bound for the sequence.
I stores the values of factorial of digits from 0 to 9 in an array. This is because in base 1 0 , they are the only existent digits. Since I don't compute them everytime, it makes my code much faster. Here is my code -
1 2 3 4 5 |
|
There is a lot of space for optimization. See Abdelhamid Saadi's answer for example. Also memorization and skip counting in some cases will improve the speed too. I'm working on improving my code. I'll post when it's done!
any quicker solutions for python??
Problem Loading...
Note Loading...
Set Loading...
Fun problem! The following C++ returns the list of numbers that satisfy the given conditions. it can be easily modified to provide the sum of these.