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.

Pages: 1 2

5 Responses to “K-Factorials And Factorions”

  1. Rutger said

    “The Loneliness of the Factorions”

    def factorial(n, k):
    	result = 1
    	n_mod_k = n % k
    	for i in range(2, n+1):
    		if i % k == n_mod_k:
    			result *= i
    	return result
    
    
    def is_factorion(n, k):
    	try:
    		return n == sum(factorial(int(i), k) for i in list(str(n)))
    	except:
    		return -1
    
    
    
    for k in range(1, 11):
    	for i in range(1000000):
    		if is_factorion(i, k):
    			print "%i-factorion:"%k, i
    
    # output:
    # 1-factorion: 1
    # 1-factorion: 2
    # 1-factorion: 145
    # 1-factorion: 40585
    # 2-factorion: 1
    # 2-factorion: 2
    # 2-factorion: 3
    # 2-factorion: 107
    # 3-factorion: 1
    # 3-factorion: 2
    # 3-factorion: 3
    # 3-factorion: 4
    # 3-factorion: 81
    # 3-factorion: 82
    # 3-factorion: 83
    # 3-factorion: 84
    # 4-factorion: 1
    # 4-factorion: 2
    # 4-factorion: 3
    # 4-factorion: 4
    # 4-factorion: 5
    # 4-factorion: 49
    # 5-factorion: 1
    # 5-factorion: 2
    # 5-factorion: 3
    # 5-factorion: 4
    # 5-factorion: 5
    # 5-factorion: 6
    # 5-factorion: 39
    # 6-factorion: 1
    # 6-factorion: 2
    # 6-factorion: 3
    # 6-factorion: 4
    # 6-factorion: 5
    # 6-factorion: 6
    # 6-factorion: 7
    # 6-factorion: 29
    # 7-factorion: 1
    # 7-factorion: 2
    # 7-factorion: 3
    # 7-factorion: 4
    # 7-factorion: 5
    # 7-factorion: 6
    # 7-factorion: 7
    # 7-factorion: 8
    # 7-factorion: 19
    # 8-factorion: 1
    # 8-factorion: 2
    # 8-factorion: 3
    # 8-factorion: 4
    # 8-factorion: 5
    # 8-factorion: 6
    # 8-factorion: 7
    # 8-factorion: 8
    # 8-factorion: 9
    # 9-factorion: 1
    # 9-factorion: 2
    # 9-factorion: 3
    # 9-factorion: 4
    # 9-factorion: 5
    # 9-factorion: 6
    # 9-factorion: 7
    # 9-factorion: 8
    # 9-factorion: 9
    # 10-factorion: 1
    # 10-factorion: 2
    # 10-factorion: 3
    # 10-factorion: 4
    # 10-factorion: 5
    # 10-factorion: 6
    # 10-factorion: 7
    # 10-factorion: 8
    # 10-factorion: 9
    # [Finished in 108.7s]
    
  2. Rutger said

    whops..

    line 14: return False

  3. Andras said

    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)

    }

  4. Andras said

    Test Formatting Scala with 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<-1 to 10) yield k+"factorions: "+  (1 to 100000 filter(isKFactorion(_, k)))) mkString("\r\n")
                                                      //> 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)
    
    }
    
  5. bitchef said

    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:

    def k_factorial(n,k):
        if n <= 0:
            return 1
        elif n < k:
            return n
        else:
            return n * k_factorial(n-k,k)
    

    And I checked for the kth factorions with:

    def is_factorion(n, k):
        return sum(k_factorial(int(i),k) for i in list(str(n))) == int(n)
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: