Skip to content

Instantly share code, notes, and snippets.

@EnderRobber101
Created February 12, 2025 04:27
Show Gist options
  • Save EnderRobber101/58b95c9dd836c13b941c5f6d7507c4cc to your computer and use it in GitHub Desktop.
Save EnderRobber101/58b95c9dd836c13b941c5f6d7507c4cc to your computer and use it in GitHub Desktop.
ti-84 python simplify radical
def simplify_radical():
index = int(input("Enter index: "))
radicand = int(input("Enter radicand: "))
if index <= 1 or radicand <= 0 : # Handle invalid input
return "Invalid input. Index must be greater than 1 and radicand must be positive."
coefficient = 1
simplified_radicand = radicand
# Find the prime factorization of the radicand
i = 2
factors = {}
while i * i <= simplified_radicand:
while simplified_radicand % i == 0:
factors[i] = factors.get(i, 0) + 1
simplified_radicand //= i
i += 1
if simplified_radicand > 1:
factors[simplified_radicand] = factors.get(simplified_radicand, 0) + 1
# Simplify the radical based on the index
for factor, power in factors.items():
outside_power = power // index
inside_power = power % index
coefficient *= factor ** outside_power
if inside_power > 0:
if simplified_radicand == 1: # Reset if starting with a clean slate after factoring out perfect powers.
simplified_radicand = factor**inside_power
else: #Update existing inner portion by multiplying
simplified_radicand *= factor ** inside_power
if isinstance(coefficient, str): # Check for error message from invalid input
print(coefficient)
elif simplified_radicand == 1:
print(coefficient) #Just print the coefficient if the root simplifies to 1
else:
if index == 2:
print(str(coefficient) + "v" + str(simplified_radicand) + "\nCoefficient: " + str(coefficient) + "\nIndex: " + str(index) + "\nRadicand: " + str(simplified_radicand) + "\n" )
else:
print(str(coefficient) + " " + str(index) + "v" + str(simplified_radicand) + "\nCoefficient: " + str(coefficient) + "\nIndex: " + str(index) + "\nRadicand: " + str(simplified_radicand) + "\n" )
# Format output nicely using f-strings
simplify_radical()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment