Showing posts with label factorial. Show all posts
Showing posts with label factorial. Show all posts

Saturday, April 29, 2023

TI-74 and Python (Casio fx-9750GIII): Probability

TI-74 and Python (Casio fx-9750GIII):  Probability



Today's program illustrates an approach from both the BASIC and Python programming languages. 


The program offers the user to calculate common probability problems:



1)  n!:  factorial of an integer

2)  nCr:  combinations without replacement

3)  nPr:  permutations

4)  nHr:  combinations with replacement

5) bday:  probability that n people do not share a category in common from a size of categories (i.e. birthday problem:  How many people in a room don't share a birthday?)



TI-74 BASICALC Program


200 REM probability

202 INPUT "1)n! 2)nCr 3)nPr 4)nHr 5)bday "; I

204 IF I<5 THEN INPUT "n? "; N

206 IF I>1 AND I<5 THEN INPUT "r? "; R

208 ON I GOTO 220,240,260,280,300


220 X=N

222 GOSUB 320

224 PRINT "n! = "; F: PAUSE

226 GOTO 340


240 Z=1: FOR K=(N-R+1) TO N: Z=Z*K: NEXT K

242 X=R: GOSUB 320

244 Z=Z/F

246 PRINT "nCr = "; Z: PAUSE

248 GOTO 340


260 Z=1: FOR K=(N-R+1) TO N: Z=Z*K: NEXT K

262 PRINT "nPr = "; Z: PAUSE

264 GOTO 340


280 Z=1: FOR K=N TO (N+R-1): Z=Z*K: NEXT K

282 X=R: GOSUB 320: Z=Z/F

284 PRINT "nHr = "; Z: PAUSE

286 GOTO 340


300 INPUT "# categories? "; R

302 INPUT "Sample size? "; N

304 Z=1: FOR K=1 TO (N-1): Z=Z*(1-K/R): NEXT K

306 PRINT "P(all unique) = "; Z: PAUSE

308 GOTO 340


320 F=1

322 FOR K=1 TO X: F=F*K: NEXT K

324 RETURN


340 INPUT "Again? 0:No, 1:Yes "; A

342 IF A=1 THEN 202

344 IF A=0 THEN STOP

346 GOTO 340



Python Program:  prob.py 


Program completed with the Casio fx-9750GIII.


# probability

from math import *


def fact(x):

  f=1

  for k in range(1,x+1):

    f*=k    

  return f



fc=1


while fc!=0:

  print("1)n! 2)nCr")

  print("3)nPr 4)nHr")

  print("5)bday")

  ch=int(input())

  if ch<5:

    n=int(input("n? "))

  if ch>1 and ch<5:

    r=int(input("r? "))

  if ch==1:

    f=fact(n)

    print("n! = "+str(f))

  if ch==2:

    f=fact(n)/(fact(n-r)*fact(r))

    print("nCr = "+str(f))

  if ch==3:

    f=fact(n)/fact(n-r)

    print("nPr = "+str(f))

  if ch==4:

    f=fact(n+r-1)/(fact(r)*fact(n-1))

    print("nHr = "+str(f))

  if ch==5:

    r=int(input("# categories? "))

    n=int(input("Sample size? "))

    f=1

    for k in range(n):

      f*=1-k/r

    print("p(all unique)=")

    print(str(f))

  print("Again? 0)no 1)yes")

  fc=int(input())    


Examples


1) n!  

13! = 6227020800


2) nCr

n = 13, r = 6; result:  1716


3) nPr

n = 13, r = 6; result:  1235520


4) nHr

n = 13, r = 6; result:  18564


5) bday

# categories: 13

Sample size: 6

Result:  P(all unique) = .2559703523



Wishing you an excellent day.   More BASIC and Python programming coming your way tomorrow!


Eddie


All original content copyright, © 2011-2023.  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. 


Sunday, March 5, 2023

Radio Shack EC-4000 (equiv. of TI-57): Program Library

Radio Shack EC-4000 (equiv. of TI-57):   Program Library





Radio Shack EC-4000/TI-57:  Horner's Method


Let's start with a demonstration of Horner's Method, which allows a quick evaluation of polynomials.   For a quadratic polynomial:


p(x) = a * x^2 + b * x + c = x * (a * x + b) + c



The program demonstrates an example polynomial:


p(x) = 3 * x^2 - 4 * x + 9 = x * (3 * x -  4) + 9



Step:  Key Code [ Key ]

00: 32, 1 [ STO 1 ]

01:  55 [ × ]

02:  03  [ 3 ]

03:  65  [ - ]

04:  04 [ 4 ]

05:  85  [ = ]

06:  55 [ × ]

07:  33, 1 [ RCL 1 ]

08:  75  [ + ]

09:  09  [ 9 ]

10:  85 [ = ]

11:  81  [ R/S ]

12:  71  [ RST ]


Examples:

p(0) = 9

p(5) = 64

p(9) = 216


Horner's Method can apply to higher order polynomials.  




Radio Shack EC-4000/TI-57:  Permutation and Factorial



This is to add two of the common probability functions that are missing with on the calculator.  


nPr = n! / (n - r)!


If n = r, then nPn = n!


Store n in R0 (register 0), r in R1 (register 1), then run the program (RST, R/S).


Other registers used:  R2:  nPr,  R7:  n-r+1 


Note that R7 is also the t-register.  


Step:  Key Code [ Key ]

00:  01  [ 1 ]

01:  32, 2  [ STO 2 ]

02:  33, 0  [ RCL 0 ]

03:  65   [ - ]

04:  33, 1  [ RCL 1 ]

05:  75  [ + ]

06:  01  [ 1 ]

07:  85  [ = ]

08:  32, 7 [ STO 7 ]

09:  86, 5 [ LBL 5 ]

10:  33, 0 [ RCL 0 ]

11:  39, 2  [ Prd 2 ]

12:  01  [ 1 ]

13:  -34, 0 [ INV SUM 0 ]

14:  33, 0 [ RCL 0 ]

15:  76  [ x≥t ]

16:  51, 5 [ GTO 5 ]

17:  33, 2 [ RCL 2 ]

18:  81  [ R/S ]

19:  71  [ RST ]



Examples:

n = 6, r = 6:  6P6 = 6! = 720

n = 5, r = 3:  5P3 = 60

n = 11, r = 6:  11P6 = 332,640

n = 52, r = 5:  52P5 = 3.118752 * 10^8



Radio Shack EC-4000/TI-57:  Snell's Law


There are two subroutines in this program listing.  Subroutines accessed by the [ SBR ] key.  


Snell's Law describes, among other things, the relationship between index of refraction of a medium (n) and refraction angle of the direction of light (Θ) is stated by:


n1 * sin(Θ1) = n2 * sin(Θ2)



Subroutine 1:  Solve for n2.  n2 = (n1 * sin(Θ1)) / sin(Θ2)


Subroutine 2:  Solve for Θ2.  Θ2 = arcsin( n1 * sin(Θ1) / n2 )


The registers used are:  R1 = n1, R2 = n2,  R3 = Θ1, R4 = Θ2


Step:  Key Code [ Key ]


// Solve for n2

00:  86, 1  [ LBL 1 ]

01:  50  [ DEG ]

02:  33, 1 [ RCL 1 ]

03:  55  [ × ]

04:  33, 3  [ RCL 3 ]

05:  28  [ SIN ]

06:  45  [ ÷ ]

07:  33, 4 [ RCL 4 ]

08:  28  [ SIN ]

09:  85  [ = ]

10:  32, 2 [ STO 2 ]

11:  81  [ R/S ]


// Solve for Θ2

12:  86, 2  [ LBL 2 ]

13:  50  [ DEG ]

14:  33, 1 [ RCL 1 ]

15:  55  [ × ]

16:  33, 3 [ RCL 3 ]

17:  28  [ SIN ]

18:  45  [ ÷ ]

19:  33, 2 [ RCL 2 ]

20:  85 [ = ]

21:  -28  [ INV SIN ]

22:  32, 4 [ STO 4 ]

23:  81 [ R/S ]


Examples:


Solve for n2:  n1 = 1.000293 (air), Θ1 = 30°, Θ2 = 19°

1.000293 STO 1

30 STO 3

19 STO 4

GSB 1

Result:  n2:  1.5362267


Solve for Θ2:  n1 = 1.000293 (air), Θ1 = 30°, n2 = 1.333 (water)

1.000293 STO 1

30 STO 3

1.333 STO 2

GSB 2

Result:  Θ2:  22.036902



Radio Shack EC-4000/TI-57:  Pseudorandom Number Generation


This program attempts to generate random numbers using the generator:


x_n+1 = frac(997 * x_n + π)


The random numbers are between 0 and 1.  An initial seed will be required 


The calculator does not have fraction and integer parts, so they have to be simulated:


int(x) ≈ x + 1E10 - 1E10

frac(x) = x - int(x), will require a register 


See steps 10 to 23.  


Due to the internal 10 digits and the simulations, expect possible roundoff errors as random numbers are continuously generated.  


Step:  Key Code [ Key ]

00:  55 [ × ]

01:  09  [ 9 ]

02:  09  [ 9 ]

03:  07 [ 7 ]

04:  75  [ + ]

05:  30  [ π ]

06:  85  [ = ]

07:  32, 6 [ STO 6 ]

08:  75  [ + ]

09:  01  [ 1 ]

10:  42  [ EE ]

11:  01  [ 1 ] 

12:  00 [ 0 ]

13:  65  [ - ]

14:  01  [ 1 ]

15:  42  [ EE ]

16:  01  [ 1 ] 

17:  00 [ 0 ]

18:  85 [ = ]

19:  -42  [  INV EE ]

20:  -34,6  [ INV SUM 6 ]

21:  33, 6 [ RCL 6 ]

22:  81  [ R/S ]

23:  71  [ RST ]



Example:


Seed:  0.98

Generation:

0.2015927

0.1294647

0.2178986

0.3864470

0.4292517



Until next time,


Eddie 


All original content copyright, © 2011-2023.  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. 


Sunday, November 28, 2021

HP Prime: Graphs of Permutation and Combination

HP Prime:  Graphs of Permutation and Combination


Definitions


Permutation:

perm(n, x) = n! ÷ (n - x)!


Combination:

comb(n, x) = n! ÷ (x! × (n - x)!)


Combination with Repeated Choices allowed:

comb(n + x - 1, x) = (n + x - 1)! ÷ (x! × (n - 1)!)


For the following graphs, I use screen shots with the HP Prime Virtual Calculator.


Discrete Graphs  


Let n and x be positive integers.






Continuous Graphs


N is still a positive integer while x is a positive real number.   The factorial can be represent as the Gamma function as  x! = Γ(x+1).  








3D-Graphs


The variables  x and y take real values.




Eddie 

All original content copyright, © 2011-2021.  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. 


Monday, September 6, 2021

Swiss Micros DM42: Subfactorial and Numworks Update (16.3)

Swiss Micros DM42: Subfactorial


Happy Labor Day!


This is a request by Marko Draisma and gratitude to Mr. Draisma.


Calculating the Subfactorial


A common,  and perhaps the most straight forward, formula to calculate the subfactorial is:  


!n = n! × Σ((-1)^k ÷ k!, k=0 to n)


Yes, the subfactorial is written with the exclamation point first.  The subfactorial finds all the possible arrangements of a set of objects where none of the objects end up in their original position.


For example, when arranging the set {1, 2, 3, 4} the subfactorial counts sets such as {2, 1, 4, 3} and {3, 4, 1, 2} but not {1, 4, 3, 2}.  For the positive integers:   !n < n!.


I am going to present two programs.  The first will use the formula stated above.


The second uses this formula, which will not require recursion or loops:


!n = floor[ (e + 1/e) × n! ] - floor[ e × n! ]


Note: Since the N! function on the DM42 accepts only positive integers, we can use the IP (integer part) to simulate the floor function.


integer(x) = { floor(x) if x ≥ 0,  ceiling(x) if x < 0


The following programs can be used on Free42, HP 42S, or Swiss Micros DM42.


Swiss Micros DM42 Program:  Subfactorial Version 1


This is a traditional route.  Registers used:


R01:  k,  counter

R02:  sum register 

R03:  n!, later !n


Program labels can start with symbols on the 42S.


01  LBL "!N"

02  STO 01

03  N!

04  STO 03

05  0

06  STO 02

07  RCL 01

08  1E3

09  ÷

10  STO 01

11  LBL 00

12  RCL 01

13  IP

14  ENTER

15  ENTER

16  -1

17  X<>Y 

18  Y↑X

19  X<>Y

20  N!

21  ÷

22  STO+ 02

23  ISG 01

24  GTO 00

25  RCL 02

26  RCL× 03

27  STO 03

28  RTN


Swiss Micros DM42 Program:  Subfactorial Version 2


I only put 2 in the label to distinguish the two programs.  


01  LBL "!N 2"

02  N!

03  ENTER

04  ENTER

05  1

06  E↑X

07  ENTER

08  1/X

09  +

10  ×

11  IP

12  X<>Y

13  1

14  E↑X

15  ×

16  IP

17  -

18  RTN



Examples


!2 = 1

!3 = 2

!4 = 9

!5 = 44

!9 = 133,496

!14 ≈ 3.2071E10


Sources


"Calculus How To:  Subfactorial"   College Help Central, LLC .https://www.calculushowto.com/subfactorial/ Retrieved September 5, 2021. 



Weisstein, Eric W. "Subfactorial." From MathWorld--A Wolfram Web Resource. https://mathworld.wolfram.com/Subfactorial.html  Retrieved September 5, 2021


Numworks 16.3 Update

Numworks recently updated its firmware to Version 16.3.  Find details of the changes and additions here:

https://my.numworks.com/firmwares

All original content copyright, © 2011-2021.  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. 


Sunday, April 18, 2021

Swiss Micros DM16L: Advanced Boolean and Factorial (up to 20)

Swiss Micros DM16L:   Advanced Boolean and Factorial (up to 20)


Introduction


The program listing, for the Swiss Micros DM16L and Hewlett Packard HP 16C,  will assign the following functions to the labels:


A:  NAND:   nand(x, y) = not(x and y)

B:  NOR:   nor(x, y) =  not(x or y)

C:  XNOR:  xnor(x, y) = (not x and not y) or (x and y)

D:  Implication:  y → x = (not y) or x

E:  Factorial:  x!  (x is a positive integer up to 20, 64 word size


NAND, NOR, NXOR, and Implication are advanced Boolean functions.  You can check out program code for the HP Prime here:


http://edspi31415.blogspot.com/2020/03/hp-prime-advanced-boolean-functions.html


Program - DM16L/HP 16C


// NAND

001 43,22,A LBL A

002 42,20   AND

003 42,30   NOT

004 43,21   RTN


// NOR

005 43,22,b LBL B

006 42,40   OR

007 42,30  NOT

008 43,21  RTN


// XNOR

009 43,22,C LBL C

010 44,1    STO 1

011 42,30  NOT

012 34      x<>y

013 44,2    STO 2

014 42,30   NOT

015 42,20   AND

016 45,1    RCL 1

017 45,2    RCL 2

018 42,20  AND

019 42,40   OR

020 43,21   RTN


// Implication

021 43,22,d LBL D

022 34      x<>y

023 42,30   NOT

024 42,40   OR

025 43,21   RTN


// Factorial

026 43,22,E LBL E

027 44,32  STO I

028 1      1

029 44,1    STO 1

030 43,22,1 LBL 1

031 45,1    RCL 1

032 45,32   RCL I

033 20      ×

034  44,1    STO 1

035 43,23   DSZ

036 22,1    GTO 1

037 45,1    RCL 1

038 43,21   RTN


Examples


Let:

R1 = 1011 0001

R2 = 1100 1100


Set up: 2-16-0000 (2's complement, 16 bits)


RCL 2, RCL 1, GSB A NAND(R2, R1):  0111 1111*

RCL 2, RCL 1, GSB B NOR(R2, R1): 0000 0010*

RCL 2, RCL 1, GSB C XNOR(R2, R1): 1000 0010*

RCL 2, RCL 1, GSB D R2 → R1:  1100 1110* 

* only the last eight bits 


Set word size to 64:

0 [ f ] [ STO ] (WSIZE)


5 GSB E 5! = 120

9 GSB E 9! = 362880*


* if the word size is insufficient, it will not show 362880 but a weird result as an overflow


12 GSB E   (64 word size)

12! = 4 79001600

Window 1:  4

Window 0:  79001600


20 GSB E

20! = 243 29020081 76640000

Window 2: 243

Window 1: 29020081

Window 0: 76640000


Source for the Advanced Boolean Functions:


John W. Harris and Horst Stocker.  Handbook of Mathematics and Computation Science  Springer:  New York, NY.  2006.  ISBN 978-0-387-94746-4



Eddie


All original content copyright, © 2011-2021.  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. 


Saturday, October 31, 2020

Breaking Down the Factorial

Breaking Down the Factorial


Factorial: It's Not Just For Integers


Let n be a positive number, where n > 0.   n! can be rewritten as:


n! 

= n * (n - 1)!

= n * (n - 1) * (n - 2)!

= n * (n - 1) * (n - 2) * (n - 3)!

...

= n * (n - 1) * (n - 2) * (n - 3) * ... * k


where 0 ≤ k ≤ 1.   Note that 0! = 1.   Keep the loop multiplying n, n - 1, n - 2, n - 3, etc. until you a multiplying a number between 0! and 1! to the total.


For certain k:


0.25! ≈ 0.9064024771

0.50! = ≈ 0.8862269255

0.75! ≈ 0.9190625268

1! = 1


Examples


3! = 3 * 2 * 1! = 3 * 2 * 1 = 6

3.25! = 3.25 * 2.25 * 1.25 * 0.25! = 9.140625 * 0.25! ≈ 8.285085142

3.5! = 3.5 * 2.5 * 1.5 * 0.5! = 13.125 * √π ÷ 2 ≈ 11.6317284

3.75! = 3.75 * 2.75 * 1.75 * 0.75! = 18.046875 * 0.75! ≈ 16.58620654


4! = 4 * 3 * 2 * 1! = 4 * 3 * 2 * 1 = 24

4.25! = 4.25 * 3.25 * 2.25 * 1.25 * 0.25! = 38.847652625 * 0.25! ≈ 35.21161185

4.5! = 4.5 * 3.5 * 2.5 * 1.5 * 0.5! = 59.0625 * √π ÷ 2 ≈ 52.3427778

4.75! = 4.75 * 3.75 * 2.75 * 1.75 * 0.75! = 85.72265625 * 0.75! ≈ 78.78448106


Factorial Values of 0 to 1


Below is a chart are the values for 0 to 1, along with several approximation polynomials.  The value and polynomials have been determined using LibreOffice's Calc application.  








Happy Halloween, 

Eddie

All original content copyright, © 2011-2020.  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. 


Saturday, September 5, 2020

Casio fx-9750GIII: Combination Matrices

 Casio fx-9750GIII:  Combination Matrices

Introduction

The program COMBMTRX creates an aligned combination triangle with rows 0 to W-1 and columns 0 to W-1.  The matrix is a square matrix with size W x W.  


1.  Pascal 

n NCR k


2.  Catalan 

(n - k + 1) / (n + 1) * [ n+k NCR n ]


3.  Borel

1 / (n + 1) * [ 2n+2 NCR n-k ] * [ n+k NCR n ]


4.  User Function

f(n, k,  h(n,k) NCR g(n,k))


"Blank" spaces will be filled with 0.  


Casio fx-9750GIII Program:  COMBMTRX

(320 bytes)


Note:  On the program screen, nCr will be symbolized as a bold C


"2020-08-11 EWS"

"COMBINATION MATRIX"

"SIZE"? → W

Identity W → Mat A

Menu "SELECT TYPE","PASCAL",1,"CATALAN",2,"BOREL",3,"USER",4

Lbl 1

"(N)nCr(K)" → fn1

Goto 5

Lbl 2

"(N-K+1)÷(N+1)×(N+K)nCr(N)" → fn1

Goto 5

Lbl 3

"1÷(N+1)×(2N+2)nCr(N-K)×(N+K)nCr(N)" → fn1

Goto 5

Lbl 4

"F(N,K)"? → fn1

Goto 5

Lbl 5

For 1 → I To W

For 1 → J To I

I-1 → N

J-1 → K

fn1 → Mat A[I,J]

Next

Next

Mat A


Examples


Size: 5


Option 1:  Pascal


[[ 1 0 0 0 0 ]

 [ 1 1 0 0 0 ]

 [ 1 2 1 0 0 ]

 [ 1 3 3 1 0 ]

 [ 1 4 6 4 1 ]]


Option 2:  Catalan


[[ 1 0 0 0 0 ]

 [ 1 1 0 0 0 ]

 [ 1 2 2 0 0 ]

 [ 1 3 5 5 0 ]

 [ 1 4 9 14 14 ]]


Option 3:  Borel


[[ 1 0 0 0 0 ]

 [ 2 1 0 0 0 ]

 [ 5 6 2 0 0 ]

 [ 14 28 20 5 0 ]

 [ 42 120 135 70 14 ]]


Option 4:  User

f = (N+K+1)nCr(K) ÷ (N=1)


(in fraction form)


[[ 1 0 0 0 0 ]

 [ 1/2 3/2 0 0 0 ]

 [ 1/3 4/3 10/3 0 0 ]

 [ 1/4 5/4 15/4 35/4 0 ]

 [ 1/5 6/5 21/5 56/5 126/5 ]]


 Source:


Cai, Yue and Yan, Catherine.  "Coutning with Borel's Triangle"  Elsevier B.V. Discrete Mathematics.   November 15, 2018  https://arxiv.org/pdf/1804.01597.pdf

Also:  https://doi.org/10.1016/j.disc.2018.10.031

Retrieved August 9, 2020

Eddie

All original content copyright, © 2011-2020.  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. 


Tuesday, January 1, 2019

Celebrating the New Year With 2019: 19 Ways to Mathematically Express 2019

Celebrating the New Year With 2019

HAPPY NEW YEAR!!!



19 Ways to Mathematically Express 2019

2019 = 3 * 673  (Prime Factorization)

2019 = 2^10 + 2^9 + 2^8 + 2^7 + 2^6 + 2^5 + 2 + 1

2019 = 3 + 2 * (3^6 + 3^5 + 3^3 + 3^2)

2019 = 42^2 + 4^4 - 1

2019 = 41^2 + 7^3 - 5

2019 = 4^5 + 3^6 + 2^8 + 10

2019 = 11^3 + 5^4 + 2^6 - 1

2019 = 12^3 + 17^2 + 2

2019 = int(10^3.3) + int(e^3) + 2^2   (e = 2.718281828...,  int: integer function)

2019 = 2 * (10^3 + 9.5)

2019 = 3^7 - 13^2 + 1

2019 = 4 * 505 - 1

2019 = 2 * 6! + 4 * 5! + 16 * 3! + 3

2019 = (10^4 + 19 * 5) / (2^2 + 1)

2019 = (13^4 - 12^3 - 9^3 + 12^2 - 1) / (2^2 + 3^2)

2019 = (2^13 + 3^8 + 4^5 + 5^3 + 6^3 + 34) / 8

2019 = 50 * 19 + 50 * 18 + 9 * 17 + 16

2019 = 25 * 19 + 25 * 18 + 26 * 13 + 71 * 11

2019 = √(4 * 10^6 + 7 * 10^4 + 6 * 10^3 + 3 * 10^2 + 6 * 10 + 1)




Eddie

All original content copyright, © 2011-2019.  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.

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.


Thursday, January 18, 2018

App Review: SciPro Calculator (Apple iOS)

App Review:  SciPro Calculator (Apple iOS)

Title:  SciPro Calc
Author:  Jerry Montgomery, Digital Ambience, Inc
Platform:  iOS
Price: Free (as of 1/18/2018)
Version:  1.2.5  (2017)



Portrait Orientation:  Basic Calculator

In the portrait orientation, SciPro Calculator is a basic function calculator.  The screen is impressive.  The black display clearly shows the large blue numbers and function indicators.  There is only 1 memory, but you get M+, M-, MC, and MR all on separate keys, which is nice.  You can turn the AC key into an AC/backspace key in Basic mode throughout the options.

Scroll down will give you access to the calculator’s history, allowing to view and copy results, even share them by email. 

Landscape Orientation:  Scientific and Programmer Calculator

Scientific Calculator



The calculator follows a post-fix algebraic system (think of a Texas Instruments TI-30 Xa or a Casio fx-260 II as examples).  The SciPro Calculator app can handle        numbers as high as 10^9864, which is very impressive.  

Functions include:

* trigonometry and their inverses (sine, cosine, tangent)
* hyperbolic functions
* factorial, which handles all real numbers not just integers (nice!)
* logarithms and exponentials, base e, 2, and 10
* square root, cube root, power, root
* modulus (see note below)
* Two angle modes: degrees, radians

Modulus Function

The modulus function works well on positive numbers.  Comparing results using negative numbers to an HP Prime:

78 mod -11:

HP Prime:  -10
SciPro Calculator:  1


-78 mod 11:

HP Prime: 10
SciPro Calculator: -1

The scientific app does lack fractions and statistics.  Other than that, it’s a good standard scientific calculator.

Programmer Calculator



The programmer calculator is a Boolean math calculator with four integer representations:  Binary (64 bits), Octal, Decimal, and Hexadecimal.  Depending on whatever mode you are in, the numeric keyboard highlights only the appropriate keys, so there is no accidently typing A in Decimal mode or 9 in Octal mode.



The ENC key turns on a unique feature, binary numbers which will give you the ASCII character, Unicode character, and the color determined by the RGB color.  This is most useful when the calculator is Hexadecimal representation.  You can use the arithmetic and the other functions to work with colors.  Sweet!

Functions featured in the Programmer Calculator:

*  Byte and Word Flip
*  Bit count and shift
*  Random integer
*  Boolean operators:  AND, OR, NOR, XOR
*  Modulus
*  1’s and 2’s compliments (Yes, there is a difference)

The programmer calculator can handle integers as high as:  18,446,744,073,709,551,615.

Caution:  No Sign Bit

Be aware that there are no sign bits with this calculator, everything is positive integer representation.  Hence this why 0 – 1 returns 18,446,744,073,709,551,615 (64 1s in Binary) instead of -1. 

Impressions

I like the unique feature of the programmer calculator having the ability to look up ASCII, Unicode, and RGB codes.  It’s a fun feature.

You probably won’t have to worry if you have to work with large numbers (at least numbers 10^100 and beyond), the SciPro Calculator app can handle it.

The blue numbers against the black screen is really nice, big, and readable, regardless of orientation of your iPhone or iPod Touch.

This is a nice calculator to have, especially for quick calculations.


Eddie

This blog is property of Edward Shore, 2018

Spotlight: Akron Brass FireCalc Pocket Computer

Spotlight: Akron Brass FireCalc Pocket Computer Welcome to a special Monday Edition of Eddie’s Math and Calculator blog. Thi...