Funciones matemáticas en Python | Conjunto 1 (Funciones numéricas)
En Python se pueden realizar una serie de operaciones matemáticas con facilidad importando un módulo llamado "matemáticas" que define varias funciones que facilitan nuestras tareas. 1. techo() :- Esta función devuelve el valor integral más pequeño mayor que el número . Si el número ya es un número entero, se devuelve el mismo número. 2. piso() :- Esta función devuelve el mayor valor integral menor que el número . Si el número ya es un número entero, se devuelve el mismo número.
Python # Python code to demonstrate the working of # ceil() and floor() # importing 'math' for mathematical operations import math a = 2.3 # returning the ceil of 2.3 print ( 'The ceil of 2.3 is : ' end = '' ) print ( math . ceil ( a )) # returning the floor of 2.3 print ( 'The floor of 2.3 is : ' end = '' ) print ( math . floor ( a ))
Producción:
The ceil of 2.3 is : 3 The floor of 2.3 is : 2
Complejidad del tiempo: O(1)
Espacio Auxiliar: O(1)
3. fabulosos() :- Esta función devuelve el valor absoluto del número. 4.factorial() :- Esta función devuelve el factorial del número. Se muestra un mensaje de error si el número no es entero.
# Python code to demonstrate the working of # fabs() and factorial() # importing 'math' for mathematical operations import math a = - 10 b = 5 # returning the absolute value. print ( 'The absolute value of -10 is : ' end = '' ) print ( math . fabs ( a )) # returning the factorial of 5 print ( 'The factorial of 5 is : ' end = '' ) print ( math . factorial ( b ))
Producción:
The absolute value of -10 is : 10.0 The factorial of 5 is : 120
Complejidad del tiempo: Transmisión exterior)
Espacio Auxiliar: O(1)
5. signo de copia (a b) :- Esta función devuelve el número con el valor de 'a' pero con el signo de 'b' . El valor devuelto es de tipo flotante. 6. mcd() :- Esta función se utiliza para calcular el máximo común divisor de 2 números mencionado en sus argumentos. Esta función funciona en Python 3.5 y superior.
# Python code to demonstrate the working of # copysign() and gcd() # importing 'math' for mathematical operations import math a = - 10 b = 5.5 c = 15 d = 5 # returning the copysigned value. print ( 'The copysigned value of -10 and 5.5 is : ' end = '' ) print ( math . copysign ( 5.5 - 10 )) # returning the gcd of 15 and 5 print ( 'The gcd of 5 and 15 is : ' end = '' ) print ( math . gcd ( 5 15 ))
Producción:
The copysigned value of -10 and 5.5 is : -5.5 The gcd of 5 and 15 is : 5
Complejidad del tiempo: O(mín(cd))
Espacio Auxiliar: O(1)