פונקציות מתמטיות בפייתון | סט 1 (פונקציות מספריות)
ב-python ניתן לבצע מספר פעולות מתמטיות בקלות על ידי ייבוא מודול בשם 'מתמטיקה' המגדיר פונקציות שונות מה שמקל על המשימות שלנו. 1. ceil() :- פונקציה זו מחזירה את הערך האינטגרלי הקטן ביותר הגדול מהמספר . אם המספר כבר מספר שלם מוחזר אותו מספר. 2. floor() :- פונקציה זו מחזירה את הערך האינטגרלי הגדול ביותר קטן מהמספר . אם המספר כבר מספר שלם מוחזר אותו מספר.
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 ))
תְפוּקָה:
The ceil of 2.3 is : 3 The floor of 2.3 is : 2
מורכבות זמן: O(1)
מרחב עזר: O(1)
3. fabs() :- פונקציה זו מחזירה את ערך מוחלט של המספר. 4. פקטוריאלי() :- פונקציה זו מחזירה את פקטורי של המספר. הודעת שגיאה מוצגת אם המספר אינו אינטגרלי.
# 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 ))
תְפוּקָה:
The absolute value of -10 is : 10.0 The factorial of 5 is : 120
מורכבות זמן: O(b)
מרחב עזר: O(1)
5. סימן העתק (א ב) :- פונקציה זו מחזירה את המספר עם ה- הערך של 'a' אבל עם הסימן 'b' . הערך המוחזר הוא מסוג float. 6. gcd() :- פונקציה זו משמשת כדי לחשב את המחלק המשותף הגדול ביותר של 2 מספרים מוזכר בטיעוניה. פונקציה זו פועלת ב-python 3.5 ומעלה.
# 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 ))
תְפוּקָה:
The copysigned value of -10 and 5.5 is : -5.5 The gcd of 5 and 15 is : 5
מורכבות זמן: O(min(cd))
מרחב עזר: O(1)