حساب المعادلات ذات الحدين باستخدام لغة البرمجة Python
-
2021-08-23
- | artistic12
- | م2260
2.11: المعاملات ذات الحدين هي عدد صحيح يعادل:
حيث k≥1، أو = 1 عندما تكون k=0.
a) باستخدام هذه المعادلة، اكتب برنامجاً بلغة بايثون باستخدام مكتبة تقوم بإنشائها تدعى binomial(n,k) تقوم بحساب المعاملات ذات الحدين للأعداد n و k. تأكد أن مكتبتك ترجع لك الإجابة بصيغة عدد صحيح (وليس عدد عشري) وتعطيك الإجابة الصحيحة وهي 1 عندما تكون قيمة k = 0.
b) باستخدام مكتبتك، اكتب برنامجاً يقوم بطباعة أول 20 سطر من مثلث باسكال. كل n سطر من الأسطر التي تمثل المثلث تحتوي على n+1 من الأعداد، وهيه الحدود ، وحتى . ستظهر لك السطور الأولى كالتالي:
1 1
1 2 1
1 3 3 1
1 4 6 4 1
2.11: The binomial coefficient is an integer equal to:
when k≥1, or =1 when k=0.
a) Using this form for the binomial coefficient, write a Python user-defined function binomial(n,k) that calculates the binomial coefficient for given n and k. Make sure your function returns the answer in the form of an integer (not a float) and gives the correct value of 1 for the case where k = 0.
b) Using your function write a program to print out the first 20 lines of “Pascal’s triangle.” The nth line of Pascal’s triangle contains n + 1 numbers, which are the coefficients and so on up to . Thus the first few lines are
1 1
1 2 1
1 3 3 1
1 4 6 4 1
book name: Computational Physics
chapter: 2, section: 6
author : Mark Newman
الأجوبة
ابحث عن مسائل برمجة بايثون | Python programming بالانجليزي
a)
import math
n= int(input("enter n"))
k= int(input("enter k"))
def binomial(n,k):
if (k>n):
print(0)
elif(k==0 or k==n ):
print(1)
else:
a= math.factorial(n)
b= math.factorial(n-k)*math.factorial(k)
c=a/b
print(c)
binomial(n,k)
b)
def Pascal(n) :
for line in range(0, n) :
for i in range(0, line + 1) :
print(binomial(line, i)," ", end = "")
print()
def binomial(n, k) :
res = 1
if (k > n - k) :
k = n - k
for i in range(0 , k) :
res = res * (n - i)
res = res // (i + 1)
return res
n = 20
Pascal(n)
-
2021-08-23
- | artistic12
- | إبلاغ عن إساءة