I am curious on how to generate my own list (the first, nth number) of: (Square Roots, Cube Roots, Fourth Roots, and Fifth Roots) in Python.
Something like this (Without GUI):
I am really curious on how this is created, because I would learn a lot about Roots in math in general, and about solving Python Root Problems.
Thank you.
As you are more interested in various different powers you could write a function which takes Nth number you want to print up to and the power you want to use. It will then return a tuple of the current value of n
, the value of n
to the power
and then a text formula.
The function also yields the results so if you were looking for large values of N they wouldnt all be loaded into memory immediately.
In this example i have then loaded the pandas module just for easy printing of the output as a data frame.
EDIT updated with an example for roots. the roots function can take an optional parameter for decimal precision since roots most often return decimal values. if no precision value is gien to the function the full precision will be shown
import pandas as pd
def get_nth_powers(nth, power):
for n in range(nth):
written_formula = (" x ".join([str(n)] * power))
yield (n, n ** power, written_formula)
def get_nth_roots(nth, root, decimal_precision=0):
decimal_precision = f'0.{decimal_precision}f' if decimal_precision else ''
for n in range(nth):
value = n ** (1/root)
written_formula = (" x ".join([f'{value:{decimal_precision}}'] * root))
yield (n, value, written_formula)
data = get_nth_powers(1000, 4)
df = pd.DataFrame(data, columns=('Nth', 'To Power', 'formula'))
print(df)
data = get_nth_roots(1000, 2, 3)
df = pd.DataFrame(data, columns=('Nth', 'value', 'formula'))
print(df)
OUTPUT
Nth To Power formula
0 0 0 0 x 0 x 0 x 0
1 1 1 1 x 1 x 1 x 1
2 2 16 2 x 2 x 2 x 2
3 3 81 3 x 3 x 3 x 3
4 4 256 4 x 4 x 4 x 4
.. ... ... ...
995 995 980149500625 995 x 995 x 995 x 995
996 996 984095744256 996 x 996 x 996 x 996
997 997 988053892081 997 x 997 x 997 x 997
998 998 992023968016 998 x 998 x 998 x 998
999 999 996005996001 999 x 999 x 999 x 999
[1000 rows x 3 columns]
Nth value formula
0 0 0.000000 0.000 x 0.000
1 1 1.000000 1.000 x 1.000
2 2 1.414214 1.414 x 1.414
3 3 1.732051 1.732 x 1.732
4 4 2.000000 2.000 x 2.000
.. ... ... ...
995 995 31.543621 31.544 x 31.544
996 996 31.559468 31.559 x 31.559
997 997 31.575307 31.575 x 31.575
998 998 31.591138 31.591 x 31.591
999 999 31.606961 31.607 x 31.607
[1000 rows x 3 columns]