„Python“ programoje daug matematinių operacijų galima lengvai atlikti importuojant modulį pavadinimu „matematika“, kuris apibrėžia įvairias funkcijas, kurios palengvina mūsų užduotis. 1. ceil() :- Ši funkcija grąžina mažiausia integralo vertė, didesnė už skaičių . Jei skaičius jau yra sveikasis skaičius, grąžinamas tas pats skaičius. 2. aukštas () :- Ši funkcija grąžina didžiausia integralo reikšmė mažesnė už skaičių . Jei skaičius jau yra sveikasis skaičius, grąžinamas tas pats skaičius.
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))
Išvestis:
The ceil of 2.3 is : 3 The floor of 2.3 is : 2
Laiko sudėtingumas: O(1)
Pagalbinė erdvė: O(1)
3. fabs () :- Ši funkcija grąžina absoliuti vertė numerio. 4. faktorialas() :- Ši funkcija grąžina faktorinis numerio. Jei skaičius nėra vientisas, rodomas klaidos pranešimas.
Python
# 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))
Išvestis:
The absolute value of -10 is : 10.0 The factorial of 5 is : 120
Laiko sudėtingumas: O(b)
Pagalbinė erdvė: O(1)
5. kopijavimo ženklas (a b) :- Ši funkcija grąžina skaičių su reikšmė „a“, bet su „b“ ženklu . Grąžinama vertė yra float tipo. 6. gcd() :- Ši funkcija naudojama apskaičiuoti didžiausias bendras 2 skaičių daliklis nurodyta savo argumentuose. Ši funkcija veikia 3.5 ir naujesnėse python versijose.
# 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(515))
Išvestis:
The copysigned value of -10 and 5.5 is : -5.5 The gcd of 5 and 15 is : 5
Laiko sudėtingumas: O(min(cd))
Pagalbinė erdvė: O(1)
kas yra prologas