Skip to content

Instantly share code, notes, and snippets.

@ZCG-coder
Last active May 9, 2024 12:42
Show Gist options
  • Save ZCG-coder/b2e3c35d462aef465db35129225b90b4 to your computer and use it in GitHub Desktop.
Save ZCG-coder/b2e3c35d462aef465db35129225b90b4 to your computer and use it in GitHub Desktop.
A Pythonic way to take a n-th root without using math
#####################################################################################################
# Copyright (c) 2023-2024 NWSOFT #
# #
# Permission is hereby granted, free of charge, to any person obtaining a copy #
# of this software and associated documentation files (the "Software"), to deal #
# in the Software without restriction, including without limitation the rights #
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell #
# copies of the Software, and to permit persons to whom the Software is #
# furnished to do so, subject to the following conditions: #
# #
# The above copyright notice and this permission notice shall be included in all #
# copies or substantial portions of the Software. #
# #
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, #
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE #
# SOFTWARE. #
#####################################################################################################
def log(number: float, base: int, decimals: int = 10) -> float:
"""
Takes a logarithm.
:param number: The number to take logarithm of.
:param base: The base of the logarithm.
:param decimals: The precision of the calculation.
:returns: The result of n-th root.
"""
raised_times = 0
while number < 1:
number *= base
raised_times += 1
x = number
y = 0
idx = 0
while True:
radicand = y + (x - y) / 2
test = base**radicand
if abs(test - number) < (1 / 10 ** (decimals + 1)):
return radicand - raised_times
if test > number:
x = radicand
elif test < number:
y = radicand
idx += 1
print(f"{idx:10d} {x} {y} {abs(test - number)} {radicand:3.8f}")
if __name__ == "__main__":
print(log(0.5, 5))
#####################################################################################################
# Copyright (c) 2023-2024 NWSOFT #
# #
# Permission is hereby granted, free of charge, to any person obtaining a copy #
# of this software and associated documentation files (the "Software"), to deal #
# in the Software without restriction, including without limitation the rights #
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell #
# copies of the Software, and to permit persons to whom the Software is #
# furnished to do so, subject to the following conditions: #
# #
# The above copyright notice and this permission notice shall be included in all #
# copies or substantial portions of the Software. #
# #
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, #
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE #
# SOFTWARE. #
#####################################################################################################
def nthroot(number: float, base: int, decimals: int = 10) -> float:
"""
Takes a n-th root.
:param number: The number to take sqare root of.
:param base: The base of the root.
:param decimals: The precision of the calculation.
:returns: The result of n-th root.
"""
raised_times = 0
while number < 1:
number *= 10**base
raised_times += 1
x = number
y = 0
idx = 0
while True:
radicand = y + (x - y) / 2
test = radicand**base
if abs(test - number) < (1 / 10 ** (decimals + 1)):
return radicand / (10**raised_times)
if test > number:
x = radicand
elif test < number:
y = radicand
idx += 1
print(f"{idx:10d} {x} {y} {abs(test - number)} {radicand:3.8f}")
if __name__ == "__main__":
print(nthroot(0.5, 5))
@ZCG-coder
Copy link
Author

Should support all types of numbers, and any base.


To-do list:

  • Add tests
  • Check for illegal inputs (0-base...)
  • Faster return by logical guessing
  • Support for numbers less than one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment