Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
nth root
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
#nth root def _root(x, degree): if x in {0, 1}: return x, x scope = [0, 1] if x<1 else [1, x] while True: mid = sum(scope)/2 mid2 = mid**degree if mid2 == x: return mid, mid edge = mid2 > x if mid == scope[edge]: return scope scope[edge] = mid from numbers import Integral def root(x, degree=2, which_result=0): if not isinstance(degree, Integral): raise TypeError('degree must be Integral') if degree < 2: raise ArithmeticError('degree must be not less than 2') if x >= 0: return _root(x, degree)[which_result] else: return _root(-x, degree)[which_result] * (-1 if degree % 2 else 1j) from ast import literal_eval while True: try: print(root(*literal_eval(input()))) except EOFError: break except Exception as e: print(type(e), e)
[1] [0, 3] [-8, 3] [100] [-2]
[
-
]
Show input
Absolute running time: 0.15 sec, cpu time: 0.04 sec, memory peak: 6 Mb, absolute service time: 0.16 sec
fork mode
|
history
|
discussion