K-Factorials And Factorions
August 18, 2015
We study today a topic from recreational mathematics. Factorions are numbers that are equal to the sum of the factorials of their digits. For instance, 145 is a factorion because 1! + 4! + 5! = 1 + 24 + 120 = 145. There are four factorions to base 10: 1, 2, 145 and 40585.
A double factorial, written n!!, is the product of all integers less than or equal to n that are congruent to n (mod 2). A triple factorial, written n!!!, is the product of all integers less than or equal to n that are congruent to n (mod 3). And so on for higher factorials. Thus, a double factorion is a number that is equal to the sum of the double factorials if its digits, a triple factorion is a number that is equal to the sum of the triple factorials of its digits, and so on. As an example, 81 is a triple factorion because 8!!! + 1!!! = 8*5*2 + 1 = 80 + 1 = 81.
It is also possible to consider factorions to bases other than 10. For instance, there are four factorions to base 6: 1, 2, 25, 26.
Your task is to write functions that allow you to explore the strange world of k-factorials and factorions; use your imagination to think of tasks that interest you. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
“The Loneliness of the Factorions”
whops..
line 14: return False
Scala:
package programmingpraxis
//https://programmingpraxis.com/2015/08/18/k-factorials-and-factorions/
object KFactorial {
def kFactorial(n: Int, k: Int=1): Int = 1 to n filter (_ % k == n % k) product
//> kFactorial: (n: Int, k: Int)Int
def isKFactorion(n: Int, k: Int=1): Boolean = n == n.toString.map(x => kFactorial(x.toString.toInt, k)).sum
//> isKFactorion: (n: Int, k: Int)Boolean
(for(k res0: String = 1factorions: Vector(1, 2, 145, 40585)
//| 2factorions: Vector(1, 2, 3, 107)
//| 3factorions: Vector(1, 2, 3, 4, 81, 82, 83, 84)
//| 4factorions: Vector(1, 2, 3, 4, 5, 49)
//| 5factorions: Vector(1, 2, 3, 4, 5, 6, 39)
//| 6factorions: Vector(1, 2, 3, 4, 5, 6, 7, 29)
//| 7factorions: Vector(1, 2, 3, 4, 5, 6, 7, 8, 19)
//| 8factorions: Vector(1, 2, 3, 4, 5, 6, 7, 8, 9)
//| 9factorions: Vector(1, 2, 3, 4, 5, 6, 7, 8, 9)
//| 10factorions: Vector(1, 2, 3, 4, 5, 6, 7, 8, 9)
}
Test Formatting Scala with scala:
Interesting topic.
Is there a direct way to determine if a number is factorion?
To get the k-factorials, I just implemented the mathematical definition as is in Python as:
And I checked for the kth factorions with: