Showing posts with label Micropython. Show all posts
Showing posts with label Micropython. Show all posts

Saturday, August 8, 2026

Python: Combination Functions (Micropython)

Python: Combination Functions (Micropython)


This is a set of combination functions. Programmed with a Casio fx-CG 100 but should work on any calculator with Python.


The script is freeware.


Functions included:


 fact(n): Factorial of the non-negative integer n. Some math modules include a factorial function, like Numworks. This function will be used in all the functions on this script.


ncr(n,r): Combination


npr(n,r): Permutation


nhr(n,r): Combination, repetitions are allowed


catalan(n): nth Catalan Number


narayana(n,k): Narayana Number (n, k are positive integers)


binpdf(n,k,p): Binomial Probability:

n: trials

k: number of successes

p: probability of success


bndcdf(n,k,p): Cumulative Binomial Probability – Lower Tail

n: trials

k: number of successes (from 0 to k)

p: probability of success



Script: combo.py

'''

combinatorics 4/12/2026

Edward Shore


round to nearest integer:

int(f+.5)

'''


from math import *


# factorial positive integers

def fact(n):

  f,i=1,1

  while i<=n:

    f*=i

    i+=1

  return int(f+.5)


# a lot of functions will use fact


# combination

def ncr(n,r):

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

  return int(f+.5)


# permutation

def npr(n,r):

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

  return int(f+.5)


# combination w/repetitions

def nhr(n,r):

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

  return int(f+.5)


# catalan numbers

def catalan(n):

  f=fact(2*n)/(fact(n)**2*(n+1))

  return int(f+.5)


# narayana numbers

def narayana(n,k):

  f=1/n*ncr(n,k)*ncr(n,k-1)

  return int(f+.5)


# binomial probability

def binpdf(n,k,p):

  # p=prob

  f=ncr(n,k)*p**k*(1-p)**(n-k)

  return f


# bin lower tail

def bincdf(n,k,p):

  # 0 to k, p=prob

  s=0

  for i in range(k+1):

    s+=binpdf(n,i,p)

  return s



Eddie


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

Casio fx-CG 100 Python: Infinite Series and Fresnel Integrals

Casio fx-CG 100 Python: Infinite Series and Fresnel Integrals



Introduction



Many functions, such as the Fresnel Integrals, the Zeta function, and the error function, can be calculated using infinite polynomials by the sum:



f(x) = Σ( g(x, n) from n = 0 to n = ∞) or

f(x) = Σ( g(x, n) from n = 1 to n = ∞)



where:

f(x): the function to be calculated

g(x, n): the infinite series representative of (x)



g(x, n) often includes the factorial function n!, where n starts at either 0 or 1 to infinity. While I was programming on fx-CG 100, I did not find a factorial function in the math module that is included in the MicroPython. So I included a definition of the factorial function as:



# factorial function

def fact(n):

  f=1

  if n<=1:

    return 1

  else:

    for i in range(2,n+1):

      f*=i

  return f



While this function does not check for negative integers, it gives the default values 0! = 1! = 1.


The main program sets up a sum, ask for initial values, and set up a check variable w = g(x,n), with a high initial value to “fail” the loop test. The loop template is set up as:


w=initial value

n=0 (start value of n)

while abs(w)>=1e-20: (1e-20 is a tolerance value, which can be adjusted)

  w=g(x,n)

  s+=w

  n+=1



An Example: Cosine of x Squared



Radians mode is used and is the default angle mode in Python.



cos(x²) = 1 – x^4 ÷ 2 + x^8 ÷ 24 – x^12 ÷ 720 + x^16 ÷ 40320 - …

= Σ( x^(4*n) * (-1)^n ÷ (2n)!, n = 0 to n = ∞)



In this case, g(x,n) = x^(4*n) * (-1)^n ÷ (2 * n)!

I set accuracy tolerance of 1 * 10^-20 but formatted the answer to 12 decimal places.


Casio fx-CG 100 Micropython: cossq.py



# cosine of x squared by series

# template of infinite series

# math module so pi is allowed



from math import *



# factorial function

def fact(n):

  f=1

  if n<=1:

    return 1

  else:

    for i in range(2,n+1):

      f*=i

    return f



# main program

# setup sum

s=0

# ask for x

print("cos(x**2) by series")

x=eval(input("x radians: "))

# series

# set term artificially high

w=100

# set counter at beginning

n=0

# series loop

while abs(w)>=1e-20:

  w=x**(4*n)*(-1)**(n)/fact(2*n)

  s+=w

  n+=1

# answer

print("{0:.12f}".format(s))



Examples:

Example 1: Input: x = 0.5, Result: 0.968912421711

Example 2: Input: x = 1.7, Result: -0.968517164228



Fresnel Integrals



There are two Fresnel integrals.



Cosine Fresnel Integral:

C(x) = ∫( cos( t^2) dt, t = 0 to t = x)

This integral is represented by the infinite series:

C(x) = Σ( x^(4*n + 1) * (-1)^n ÷ ((2* n)! * (4 * n + 1)), n = 0 to n = ∞)



Sine Fresnel Integral:

S(x) = ∫( sin( t^2) dt, t = 0 to t = x)

This integral is represented by the infinite series:

S(x) = Σ( x^(4*n + 3) * (-1)^n ÷ ((2* n + 1)! * (4 * n + 3)), n = 0 to n = ∞)



Casio fx-CG 100 Micropython: fresnel.py



# Fresnel Integrals

# template of infinite series

# Eddie W. Shore, 11/23/2025



from math import *



# factorial function

def fact(n):

  f=1

  if n<=1:

    return 1

  else:

    for i in range(2,n+1):

      f*=i

    return f



# main program

# c: cosine, s=sine

c=0

s=0

# ask for x

x=eval(input("x radians: "))

# series

# set term artificially high

w=100

# set counter at beginning

n=0

# series loop

while abs(w)>=1e-20:

  a=(-1)**n*x**(4*n+1)/(fact(2*n)*(4*n+1))

  b=(-1)**n*x**(4*n+3)/(fact(2*n+1)*(4*n+3))

  w=max(a,b)

  c+=a

  s+=b

  n+=1

# answer

print("x:{0:.12f}".format(x))

print("Fresnel Cosine:\n{0:.12f}".format(c))

print("Fresnel Sine:\n{0:.12f}".format(s))


Examples:

Example 1: x = 0.4,

C(0.4) ≈ 0.398977212913, S(0.4) ≈ 0.021294355570

Example 2: x = 3.1,

C(3.1) ≈ 0.605132097879, S(3.1) ≈ 0.785491219284

Eddie


All original content copyright, © 2011-2026. 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, May 15, 2022

Python: Financial Functions (2nd Edition)

Python:  Financial Functions (2nd Edition)


This is an update to the python file, which I first released on May 17, 2020:


https://edspi31415.blogspot.com/2020/05/numworkscasio-micropythonpython.html


I am able to transfer and test the python file to a Numworks calculator through the online editor, and TI-84 Plus CE Python through the TI Connect CE.  Because only the math module is used, the python file can be run in most, if not all calculators with Python, as well as Python 3.  


What is included?


*  time value of money calculations

*  net present value and internal rate of return

*  net present value (xnpv) and internal rate of return (xirr) when periods between flows are not consistent, a 365 day-year is assumed

*  simple interest:  calculating interest, total, and solving for principal

*  profit calculations: cost-sell-markup

*  adding sales tax

*  percent change

*  present and future value uniform stream factors

*  compound interest calculations of a single stream:  solve for present value, future value, number of periods, and periodic interest

*  days between dates

*  specific applications:  monthly payment, PITI, qualifying loan amount, sinking fund, expressing a list of amounts as a percent of the sum, prorating an amount among a list of flows


Download the Python file and the instructions (pdf file) here:

https://drive.google.com/file/d/1l7Xg9dM-RHfekKkU7A1Wjay76-yVc56S/view?usp=sharing


Size: about 3,800 bytes.  


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. 


Sunday, October 10, 2021

n = a^m + b

 n = a^m + b



Calculators:  Swiss Micros DM41X, HP-32S/32SII, Casio fx-9750 GIII


n = a^2 + b


The following program breaks down a positive integer n into n = a^2 + b, where a is √n rounded to the nearest integer in attempt to minimize abs(b).    


Examples:

  

800 = 28^2 + 16

662 = 26^2 - 14

525 = 23^2 - 4


This technique is useful, especially with the HP 32S and HP 32SII.  Normally, real numbers take up 9.5 bytes of memory on the HP 32S series.  Integers 0 - 100 only consume 1.5 bytes of memory for the original HP 32S and the range is extended to 0 - 254 for the HP 32SII.  


When the program terminates, b is on the X stack while a is on the Y stack.  


HP 41C/Swiss Micros DM41X Program:  SQPLUS


01 LBL^T SQPLUS

02 FIX 0

03 ENTER 

04 SQRT

05 RND

06 X^2

07 -

08 LASTX

09 SQRT

10 X<>Y

11 FIX 4

12 RTN


HP 32S and HP 32SII Program: SQPLUS


S01 LBL S

S02 FIX 0

S03 ENTER

S04 SQRT

S05 RND

S06 x^2

S07 -

S08 LASTx

S09 SQRT

S10 x<>y

S11 ALL

S12 RTN


Example:  684


Result:

Y:  26

X:  8 

(26^2 + 8 = 684)


Additional Powers


Now let's extend the problem to include higher powers.  


n = a^m + b


We can estimate a by:


n ≈ a^m

ln n ≈ ln(a^m)

ln n ≈ m * ln a

ln n / m ≈ ln a


a ≈ e^(ln n / m)


Round a to the nearest integer.  Use this a to find b.


n = a^m + b


b = n - a^m


The script brkpower.py (225 bytes) calculates a and b for powers m = 2 to 6.  This script was created on the Casio fx-9750GIII.


Casio fx-9750GIII Micro python Script:  brkpower.py


# 2021-07-17 ews

from math import *

print("n = a**m + b")

n=float(input("n? "))

m=2.0

while m<7:

  a=int(exp(log(n)/m)+.5)

  b=n-a**m

  s="{:.0f} = {:.0f}**{:.0f} + {:.of}".format(n,a,m,b)

  print(s)

  m+=1


Example:  n = 860


Results:

860 = 29**2 + 19         (860 = 29^2 + 19) 

860 = 10**3 + -140     (860 = 10^3 - 140, and so on)

860 = 5**4 + 235

860 = 4**5 + -164

860 = 3**6 + 131


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, April 11, 2020

Casio fx-CG 50 Update and Mental Math: Squaring a Number Ending in 5

Casio fx-CG 50 Update and Mental Math:   Squaring a Number Ending in 5

Casio fx-CG 50 Update

Casio released an update to the operating software for the fx-CG 50, version 3.40.   This update, among other things, expands the number of commands in Casio's Micropython.

There is a new module casioplot module has commands to draw pixels in any color and strings. 

The link to get the updated software is here:  https://edu.casio.com/download/index.php

Click on Graphing Models.

I have not had a chance to try the new drawing commands yet, it's on my list of things to do. 

Mental Math:   Squaring a Number Ending in 5

Squaring a positive integer whose last digit is 5 will result in the answer whose last digits are 25.

Why is this true?

Let n be a positive integer whose last digit is 5.   (n = 15, 25, 35, etc.). 

Let m = n - 5, then n = m + 5.   Note that m is a multiple of 10.

Then:

n^2 = (m + 5)^2
= m^2 + 2 * m * 5 + 25
= m^2 + 10 * m + 25
= (m^2 + 10 * m) + 25
= m * (m + 10) + 25

Since m is a multiple of 10, m^2 and 10 *  m are multiples of 100.  Because of this fact, squaring integers ending in 5 is used in mental math.

Examples

Example 1:  Calculate 45^2.

n = 45 = 40 + 5    (m = 40)
= (40 + 5)^2
= 40 * (40 + 10) + 5^2
= 40 * 50 + 25
= 2000 + 25
= 2025

45^2 = 2025

Example 2:  Calculate 165^2

n = 165 = 160 + 5    (m = 160)
= (160 + 5)^2
= 160 * (160 + 10) + 25
= 160 * 170 + 25
=  27200 + 25
=  27225

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.

Monday, July 29, 2019

TI Nspire CX and Casio Micropython: Recursion Polynomials

TI Nspire CX and Casio Micropython:  Recursion Polynomials

Introduction

This blog entry deals with how to calculate functions defined as a recursion polynomial.  An example:

f_n(x) = 2 * f_n-1(x) + 1    where f_0(x) = x^2

We'll cover the TI Nspire CX (which is programmed in Basic) and the Casio fx-CG50's Micropython Module.

TI Nspire CX

The programs for the TI Nspire are in Basic (or some flavor of it), so it can easily be adapted for many programming and graphing calculators.  A possible template could be:

Input "X: ", x
Input "N: ", n
Local f, g, h, i 
//  f represents the function f_n
// g represents f_n-1
// h represents f_n-2
If n = 0: Then Return f_0(x):  Stop: IfEnd
If n = 1: Then Return f_1(x):  Stop: IfEnd
Else // if n > 1
For i =2 to n do
f := f_(x,g,h,i)   // i is used in placed for n
h:= g   // move values for the next loop
g:= f
Next
Return f  // final result

This is just one example on how to tackle this. 

Example 1: 
f_n = f_n-1 * (x + 1) - f_n-2
f_0 = 1
f_1 = x

Define recur1(n,x)=
Prgm
:Local f,g,h,i
:If n=0 Then
:  Disp 1
:ElseIf n=1 Then
:   Disp x
:ElseIf n>1 Then
:   h:=1
:   g:=x
:   For i,2,n
:     f:=g*(x+1)-h
:     h:=g
:     g:=f
:   EndFor
:   Disp f
:EndIf
:EndPrgm

Example 2:  Hermite Polynomial
H_n = 2 * x * H_n-1 - (2 * n - 2) * H_n-2
H_0 = 1
H_1 = 2 * x

Define numhermite(n,x)=
Prgm
:© numerical hermite polynomial
:Local f,g,h,i
:If n=0 Then
:  Disp 1
:ElseIf n=1 Then
:  Disp 2*x
:ElseIf n>1 Then
:  h:=1
:  g:=2*x
:  For i,2,n
:    f:=2*x*g-(2*i-2)*h
:    h:=g
:    g:=f
:  EndFor
:  Disp f
:EndIf
:EndPrgm

Example 3:  Parabolic Cylinder Function (aka Weber Function)
D_v+2 = x * D_v+1 - v * D_v
D_0 = e^(-x^2/4)
D_1 = x * e^(-x^/4)

Define parabcylin(v,x)=
Prgm
:© parabolic cylinder polynomial
:Local f,g,h,i
:If v=0 Then
:  Disp e^(((−x^(2))/(4)))
:ElseIf v=1 Then
:  Disp x*e^(((−x^(2))/(4)))
:ElseIf v>1 Then
:  h:=e^(((−x^(2))/(4)))
:  g:=x*e^(((−x^(2))/(4)))
:  For i,2,v
:    f:=x*g-(i-1)*h
:    h:=g
:    g:=f
:  EndFor
:  Disp f
:EndIf
:EndPrgm

Casio fx-CG 50 Micropython

Here is an approach using Python:

import math
N = float(input("N = "))
X = float(input("X = "))
if N==0:
  print(f_0)
elif N==1:
  print(f_1)
elif N>1:
  H = f_0
  G = f_1
  for i in range (2,N+1):
     F = f_n
     H = G
     G = F
 print(F)

Example 1: 
f_n = f_n-1 * (x + 1) - f_n-2
f_0 = 1
f_1 = x

recursio.py:
import math
N=float(input("N = "))
X=float(input("X = "))
if N==0:
  print(1)
elif N==1:
  print(X)  
else:
  H=1
  G=X
  for i in range(2,N+1):
    F=G*(X+1)-H
    H=G
    G=F
  print(F)
   
Example 2:  Hermite Polynomial
H_n = 2 * x * H_n-1 - (2 * n - 2) * H_n-2
H_0 = 1
H_1 = 2 * x

hermite.py:
import math
N=float(input("N = "))
X=float(input("X = "))
if N==0:
  print(1)
elif N==1:
  print(2*X)
else:
  H=1
  G=2*X
  for i in range(2,N+1):
    F=2*X*G-(2*i-2)*H
    H=G
    G=F
  print(F) 


Example 3:  Parabolic Cylinder Function (aka Weber Function)
D_v+2 = x * D_v+1 - v * D_v
D_0 = e^(-x^2/4)
D_1 = x * e^(-x^/4)

import math
N=float(input("N = "))
X=float(input("X = "))
if N==0:
  print(math.exp(-X**2/4))
elif N==1:
  print(X*math.exp(-X**2/4))
else:
  H=math.exp(-X**2/4)
  G=X*math.exp(-X**2/4)
  for i in range(2,N+1):
    F=X*G-(i-1)*H
    H=G
    G=F
  print(F) 
 

Numeric Examples to Try:

Example 1:
n = 2, x = 0.5,  result: -0.25
n = 3, x = -1.7, result: 1.567

Example 2:
n = 2, x = 2.2, result:  17.36
n = 4, x = -0.8, result: -12.1664

Example 3:
n = 0, x = 6.7, result: 1.3369921208E-5
n = 2, x = 0.3, result: 5.86807637482E-4

Happy Programming!

Source:

Keith Oldham, Jan Myland, Jerome Spainer.  An Atlas of Functions.  2nd Edition Springer:  New York.  2009  ISBN 978-0-387-48806-6

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.

Thursday, May 2, 2019

HP Prime and Casio fx-CG50 MicroPython: √(x + √(x + √(x + ... )))

HP Prime and Casio fx-CG50 MicroPython:  √(x + √(x + √(x + ...  )))

Introduction

Made with LibreOffice Math


The program SQRADD attempts to find a calculation to:

√(x + √(x + √(x + ...  )))

That is:

1.  Start with a number x  (any positive number, we're assuming that we are in the Real Number domain)
2.  Take the square root
3.  Add x to the result
4.  Take the square root of the result
5.  Repeat steps 3 and 4 forever. (or until a condition is satisfied, such as machine accuracy is reached)


HP Prime Program SQRADD

The loop repeats until the difference between two terms is less than machine accuracy, 1E-12 (since HP Prime has floating point accuracy of 12 digits).  Output:  {x, answer, number of iterations it took to get machine accuracy}

EXPORT SQRADD(X)
BEGIN
// 2019-04-28 EWS
// √(X+√(X+√(X+...)))

// setup
LOCAL T,U,N:=1;
T:=√X;
U:=√(X+T);

// loop
WHILE ABS(U-T)≥1E−12 DO
T:=U;
U:=√(X+T);
N:=N+1;
END;

// display results {X,U,N}
RETURN {X,U,N};
END;

Casio fx-CG50 MicroPython Script sqradd.py

Use the Python mode of the Casio fx-CG50 (software versions 3.2 or greater).  For more information of this procedure, please see: http://edspi31415.blogspot.com/2018/10/casio-fx-cg50-version-320-python-comes.html

# 2019-04-28 EWS

import math

# sqrt(X+sqrt(X+...))
# declare variables
# as float
N=1.0
X=float(input("X="))
T=float(math.sqrt(X))
U=float(math.sqrt(X+T))

# loop
# Casio Micropython
# has precision of 15
while abs(U-T)>=1e-15:
  T=U
  U=math.sqrt(X+T)
  N+=1

# results
print("X= "+str(X))
print("Result= ")
print(str(U))
print("Iterations= "+str(N))

Procedure on a Four-Function or Desktop Calculator

1.  Clear the memory (usually [ MRC ] [ MRC ] )
2.  Enter x and store it in memory  (x  [ M+ ] )
3.  Take the square root [ √ ]
4.  Repeat this loop (until machine accuracy is reached, that is, the the result doesn't "change"):  [ + ] [ MR ] [ = ] [ √ ]

Results

HP Prime

x = 1,  Result:  1.61803398875, 23 iterations
x = 2, Result:  2,  20 iterations
x = 3,  Result: 2.30277563773, 18 iterations
x = 4, Result: 2.56155281281, 17 iterations
x = 5, Result: 2.79128784748, 17 iterations
x = 6, Result: 3,  16 iterations
x = 7, Result: 3.19258240357, 16 iterations
x = 8, Result: 3.37228132327, 15 iterations
x = 9, Result:  3.54138126514, 14 iterations
x = 10, Result: 3.70156211871, 14 iterations
x = 11, Result: 3.85410196624, 13 iterations
x = 12, Result: 4,  14 iterations
x = 20, Result: 5, 12 iterations
x = 30, Result: 6, 12 iterations
x = 42, Result: 7, 11 iterations
x = 56, Result: 8, 11 iterations

Casio fx-CG50 MicroPython

x = 1, Result:  1.618033988749895, 30 iterations
x = 2, Result: 2,  26 iterations
x = 3, Result: 2.302775637731994, 24 iterations
x = 4, Result: 2.56155281280883, 22 iterations
x = 5, Result: 2.79128784747792, 21 iterations
x = 6, Result: 3, 20 iterations
x = 7, Result: 3.192582403567252, 20 iterations
x = 8, Result: 3.372281323269014, 19 iterations
x = 9, Result: 3.54138126514911, 19 iterations
x = 10, Result: 3.701562118716424, 18 iterations
x = 11, Result: 3.854101966249685, 18 iterations
x = 12, Result: 4, 18 iterations
x = 20, Result: 5, 16 iterations
x = 30, Result: 6, 15 iterations
x = 42, Result: 7, 14 iterations
x = 56, Result: 8, 14 iterations

Some Interesting Observations

When x = 1, the result is the Golden Ratio, Φ = (1 + √5)/2.

When x = n * (n-1) where n is an integer and n ≥ 2, the result is n. 

Examples: 
x = 2 =  2 * 1, result: 2
x = 12 = 4 * 3, result: 4
x = 56  = 8 * 7, result: 8
x = 240 = 16 * 15, result: 16
x = 992 = 32 * 31, result:  32

Fun stuff.  What other patterns can we find with this or other similar calculation?  Until next time,

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.

Saturday, November 17, 2018

TI-84+ and Casio (fx-CG 50) Micropython: Simplifying Nested Radicals

TI-84+ and Casio (fx-CG 50) Micropython: Simplifying Nested Radicals

Introduction 

The following program will simplifying the following expression:

√(x + y) = √a + √b

The input is x and y, with output a and b.  The following conditions are implied:  x > y and x > 0.

To see the derivation of simplification and examples, please check out this blog entry:

https://edspi31415.blogspot.com/2018/11/simplifying-nested-radicals.html

TI 84 Plus Program:  DENEST

"EWS 2018-11-12"
Disp "√(X+Y)=√(A)+√(B)","X>Y","X>0"
Prompt X,Y
X/2+1/2*√(X^2-Y^2)→A
X/2-1/2*√(X^2-Y^2) →B
√(A)→Z
round(Z,9)→Z
Disp " "

If Y<0 font="">

Then
If fPart(Z)=0
Then 
Disp √(A),"- √(",B,")"
Else 
Disp "√(",A,")- √(",B,")"
End

Else
If fPart(Z)=0
Then 
Disp √(A),"+ √(",B,")"
Else 
Disp "√(",A,")+ √(",B,")"
End

End

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

Input is the form of:

√(x1 * √x2 + y1 * √y2) = √a + √b

import math
print("sqrt(x+y)=")
print("sqrt(a)+sqrt(b)")
print("x>y","x>0")

print(" ")
print("x1*sqrt(x2)")
x1=float(input("x1:"))
x2=float(input("x2:"))
x=x1*math.sqrt(x2)

print(" ")
print("y1*sqrt(y2)")
y1=float(input("y1:"))
y2=float(input("y2:"))
y=y1*math.sqrt(y2)

a=x/2+math.sqrt(x**2-y**2)
b=x/2-math.sqrt(x**2-y**2)
z=math.sqrt(a)
z=round(z,10)
a=round(a,10)
b=round(b,10)

if y<0: font="">
  if z-int(z)==0:
    print(z,"-sqrt(")
    print(b,"?")
  else:
    print("sqrt(",a)
    print("-sqrt(")
    print(b,")")
else:
  if z-int(z)==0:
    print(z,"+sqrt(")
    print(b,")")
  else:
    print("sqrt(",a)
    print("+sqrt(")
    print(b,")")


Source:

Michael J. Wester, Editor.  Computer Algebra Systems: A Practical Guide John Wiley & Sons: Chichester 1999.  ISBN 978-0-471-983538

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, November 15, 2018

TI-84+ and Casio (fx-CG 50) Micropython: Simplifying A sin x + B cos x To r sin (x + θ)

TI-84+ and Casio (fx-CG 50) Micropython:  Simplifying  A sin x + B cos x To r sin (x + θ)

Introduction

Simplify the expression:

(I)   a * sin x + b * cos x

to an expression that involves only one trigonometric function. 

The first task is to multiply the expression by √(a^2 + b^2)/√(a^2 + b^2). (refer to Source)  This results as:

(II) a* √(a^2 + b^2)/√(a^2 + b^2) * sin x + b * √(a^2 + b^2)/√(a^2 + b^2) * cos x

Refer to the triangle diagram below and it will should become clear why it is desirable to multiply by √(a^2 + b^2)/√(a^2 + b^2). 



With:

(III)
 √(a^2 + b^2) * a/√(a^2 + b^2) * sin x +  √(a^2 + b^2) * b/√(a^2 + b^2) * cos x
=  √(a^2 + b^2) * cos θ * sin x +  √(a^2 + b^2) * sin θ * cos x
=  √(a^2 + b^2) * (cos θ * sin x +  sin θ * cos x)

With the use of the trigonometric identity:
sin(x + y) = sin x * cos y + cos x * sin y

This becomes:
(IV)
 =  √(a^2 + b^2) * sin(x + θ)

Let's make some observations.

1.  The angle θ dependents on what quadrant the point (a,b) is. 

2.  Taking into consideration of where the point (a,b) is and the expression √(a^2 + b^2), the task of finding the coefficients of reduced formula for a * sin x + b* cos x can be achieved by calculating a rectangular to polar conversion of the point (a,b). 

Hence: (IV) becomes:

(V)
 a * sin x + b * cos x =  r * sin(x + θ)

where
r = √(a^2 + b^2)  = abs(a + bi)
θ = atan(b/a) = atan2(b,a) = arg(a + bi)


TI 84 Plus Program:  SCTOSIN

"EWS 2018-11-12"
a+bi
Disp "A*sin(X)+B*cos(X)", "=R*sin(X+θ)"
Prompt A,B
abs(A+B*i)→R
angle(A+B*i)→θ
Disp R,"sin(X+", θ, ")"

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

import math
print("a*sin(x)+b*cos(x)")
print("=r*sin(x+t)")
a=float(input("a:"))
b=float(input("b:"))
r=math.sqrt(a**2+b**2)
t=math.atan2(b,a)
print(" ")
print(r)
print("*sin(x+")
print(t,")")

Examples
(Radians mode assumed)

Example 1:
4 * sin x + 2 * cos x = 4.472135955 * sin(x + 0.463647609)

Input:
a = 4
b = 2

Output:
r = 4.472135955
θ = 0.463647609



Example 2:
3 * sin x - 5 * cos x = 5.803951895 * sin(x - 1.030376827)

Input:
a = 3
b = -5

Output:
r = 5.803951895
θ = -1.030376827


Source:
Dugopolski, Mark  Trigonometry Addison Wesley: Boston 2003 pp 211-212  ISBN 0-201-70338-6

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.

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.


Classic RPN Stack Operations

  Classic RPN Stack Operations Starting Stack: T:  d Z:  c Y:  b X:  a OVER :   Copies b to the Z and X stacks, while leaving ...