Tuesday, November 13, 2018

TI-84+ and Casio Micropython (fx-CG50): Solving Equations Involving Factorials (Inverse Factorial)

TI-84+ and Casio Micropython (fx-CG50):  Inverse Factorial of Integer

Introduction

The goal is to solve the equation for n:

x = n!

One way to do this is to use a variation of the gamma function which invovles an improper integral:

x = ʃ (t^ n * e^(-n) dt, 0, ∞)

(note that n! = gamma(n+1) )

Or use an approximation formula.

Another approach is to use an iterative method, which does not use calculus.  Hence, if x and n are integers then x can be written as:

x = n! + r

Where r is a remainder.  This method involves successive division.

Loop:

1.  Set n = 2  (since 1! = 1) and set t = x.  The  variable t will be working copy of x.
2.  Divide t by n and store in t, t = t / n
3.  Increase n by 1, n = n + 1
4.  If t is less than or greater than n, repeat steps 2 and 3.
5.  If t = n, then x = n!.  Done.
6.  If t ≠ 1, then do the following.  Set n = n - 1 and r = x - n!.  The answer is x = n! + r.

Example 1:    120 = n!

n = 2, t = 120/2 = 60,  60 > 3
n = 3,  t = 60/3 = 20,  20 > 4
n = 4,  t = 20/4 = 5, 5 = 5

Since 5 = 5, 120 = 5!

Example 2:   177 = n!

n = 2, t = 177/2,  88.5 > 3
n = 3, t = 88.5/3, 29.5 > 4
n = 4,  t = 29.5./4, 7.375 > 5
n = 5, t = 7.375/5, 1.475 < 6.  Stop

r = 177 - 5!  = 57

Hence:  177 = 5! + 57

TI-84 Plus Program INVFACT

"EWS 2018-11-11"
Disp "X = N! + R"
Input "X: ", X
X→T
2→N
Repeat T≤N
T/N→T 
N+1→N
End 
If X-N!=0
Then
Disp N, "!"
Else
N-1→N
X-N!→R
Disp N, "! +", R
End

Casio Micropython (fx-CG 50) Script invfact.py

import math
print("x = n! + r")
x=float(input("x: "))
t=x
n=2
while t>n:
  t=t/n
  n=n+1
f=1
for i in range (1, n+1):
  f=f*i
if x-f==0:
  print(n,"!")
else:
  n=n-1
  f=f/i
  r=int(x-f)
  print(n, "! +",r)

Examples:

24 = 4!

26 = 4! + 2

53 = 4! + 29

Eddie

All original content copyright, © 2011-2018.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.  Please contact the author if you have questions.