Sunday, May 21, 2023

Python: Square Root Simplification

 Python: Square Root Simplification



Introduction


The script SQFACTOR.py attempts to factor and simplify square root expressions in the form of either:


1.  √n

2.  √m + √n


The script was created using the TI-83 Plus Premium CE Python Edition, which is my first Python script using the French calculator.  




Python script:  sqfactor.py


from math import *

# basic code


def sqfactor(n):

  c=1

  k=2

  while k**2<n:

    while (n/k**2)-int(n/k**2)==0:

      n/=k**2

      c*=k

    k+=1

  return [c,n]


# 2 forms

print("Select: ")

print("1. sqrt(n)")

print("2. sqrt(m)+sqrt(n)")

ch=int(input())


if ch==1:

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

  l=sqfactor(n)

  print("sqrt("+str(n)+")=")

  print(str(l[0])+"*sqrt("+str(l[1])+")")


if ch==2:

  m=float(input("m? "))

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

  l0=sqfactor(m)

  l1=sqfactor(n)

  c=l0[0]

  d=l0[1]

  r=l1[0]

  s=l1[1]

  print("sqrt("+str(m)+") + sqrt("+str(n)+")")

  if d==s:

    print(str(c+r)+"*sqrt("+str(d)+")")

  else:

    print(str(c)+"*sqrt("+str(d)+")+")

    print(str(r)+"*sqrt("+str(s)+")")



Examples



Example 1:


√1640

Result:  2 * sqrt(410.0)


Example 2:


√56 + √78

Result:  2 * sqrt(14.0) + 1 * sqrt(78.0)


Example 3:


√75 + √27

Result:  8 * sqrt(3.0)



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. 

TI 84 Plus CE: Consolidated Debts

TI 84 Plus CE: Consolidated Debts   Disclaimer: This blog is for informational and academic purposes only. Financial decisions are your ...