5

I'm trying to make a simple code for power 12 to 4(12 ** 4) . I have the output num (20736) but when I want to figure returns (20736) to its original value (12). I don't know how to do that in Python .. in Real mathematics I do that by the math phrase {12؇}

The question is how to make {12؇} in Python ?? I'm using sqrt() but sqrt only for power 2

  #!/usr/bin/env python3.3
import math 
def pwo():
   f=12 ** 4 #f =20736 #
   c=        # should  c = 12 #
   return f,c
print pwo()
JS.
  • 14,781
  • 13
  • 63
  • 75
Mr Sam
  • 981
  • 1
  • 8
  • 12
  • 2
    `20736 ** 0.25` might be a bit off, or exact, I don't know for sure. The `n`-th root of `x` is `x^(1/n)`, so `20736 ** (1.0/4)`. – Daniel Fischer Jul 11 '13 at 22:09
  • 1
    Hint: `n^(1/4) = (n^(1/2))^(1/2)`. Or use `n ** 0.25`. – Matt Ball Jul 11 '13 at 22:10
  • In math, `12؇4` is the 12th root of 4, not the 4th root of 12. In other words, it's the inverse of `4 ** 12`, not the inverse of `12 ** 4`. Which one do you actually want? – abarnert Jul 11 '13 at 22:23
  • @DanielFischer: And if you want it exact to a given number of decimal places, just use `20736 ** decimal.Decimal(1)/4` instead. – abarnert Jul 11 '13 at 22:24
  • @abarnert :12؇ is the 4th root of 12 ; 12*12*12*12=20736 :(20736؇4=12) – Mr Sam Jul 12 '13 at 14:27
  • Apparently my font doesn't display U+0607 (؇) usefully, so I guessed wrong at what you meant. It's supposed to be the traditional Arabic fourth-root symbol, equivalent to the western U+221C (∜), not the xth-root symbol. – abarnert Jul 12 '13 at 17:46
  • the symbol ∜ from Western Arabic numerals :(1234567890) and ؇ for Eastern Arabic numerals :(٠‎ ١‎ ٢‎ ٣‎ ٤‎ ٥‎ ٦‎ ٧‎ ٨‎ ٩‎) Thank U a lot ;Im from saudi arabia – Mr Sam Jul 14 '13 at 04:57

2 Answers2

22
def f(num):
    return num**0.25

or

import math
def f(num):
    return math.sqrt(math.sqrt(num))
Guy Blanc
  • 833
  • 8
  • 14
4

For scientific purposes (where you need a high level of precision), you can use numpy:

>>> def root(n, r=4):
...     from numpy import roots
...     return roots([1]+[0]*(r-1)+[-n])
...
>>> print(root(12))
[ -1.86120972e+00+0.j          -3.05311332e-16+1.86120972j
  -3.05311332e-16-1.86120972j   1.86120972e+00+0.j        ]
>>>

The output may look strange, but it can be used just as you would use a list. Furthermore, the above function will allow you to find any root of any number (I put the default for r equal to 4 since you asked for the fourth root specifically). Finally, numpy is a good choice because it will return the complex numbers that will also satisfy your equations.

  • So, it seems this may not be quite as precise as a simpler Python method for finding the first non-imaginary positive root in some cases. E.g.: (Sorry, oneliners…) `import numpy as np`, `numpy_root = lambda n, r: float(np.real(filter(lambda n: np.imag(n) == 0 and n > 0, np.roots(np.double([1]+[0]*(r-1)+[-n])))[0]))`, `py_root = lambda n, r: float(n)**(1.0/float(r))`. On my box, `numpy_root(2, 12) == 1.0594630943592955`, but `py_root(2, 12) == 1.0594630943592953`, which is better according to http://www.komsta.net/digits/12roots/ . Am I doing something bad? Would another numpy API be better…? – Mark G. Sep 18 '15 at 05:29