Tuesday, June 22, 2010

Эхний жижигхэн програм

>>> i, fac, sum  = 1, 1, 0
>>> while i < 25 :
...     print '{0} xurtelx niilber :{1}, factorial :{2}'.format(i-1,sum,fac)
...     fac = fac * i
...     sum = sum + i
...     i += 1
...

0 xurtelx niilber :0, factorial :1
1 xurtelx niilber :1, factorial :1
2 xurtelx niilber :3, factorial :2
3 xurtelx niilber :6, factorial :6
4 xurtelx niilber :10, factorial :24
5 xurtelx niilber :15, factorial :120
6 xurtelx niilber :21, factorial :720
7 xurtelx niilber :28, factorial :5040
8 xurtelx niilber :36, factorial :40320
9 xurtelx niilber :45, factorial :362880
10 xurtelx niilber :55, factorial :3628800
11 xurtelx niilber :66, factorial :39916800
12 xurtelx niilber :78, factorial :479001600
13 xurtelx niilber :91, factorial :6227020800
14 xurtelx niilber :105, factorial :87178291200
15 xurtelx niilber :120, factorial :1307674368000
16 xurtelx niilber :136, factorial :20922789888000
17 xurtelx niilber :153, factorial :355687428096000
18 xurtelx niilber :171, factorial :6402373705728000
19 xurtelx niilber :190, factorial :121645100408832000
20 xurtelx niilber :210, factorial :2432902008176640000
21 xurtelx niilber :231, factorial :51090942171709440000
22 xurtelx niilber :253, factorial :1124000727777607680000
23 xurtelx niilber :276, factorial :25852016738884976640000

Дээрх кодоо функц болгон бичвэл :
>>> def fac_sum(n):
...     i,fac,sum=1,1,0
...     while i < n+1:
...             print '{0} xurtelx niilber :{1}, factorial :{2}'.format(i-1,sum,fac)
...             fac = fac*i
...             sum = sum+i
...             i += 1
...

Одоо тодорхойлсон функцээ өөр хувьсагчид олгож ашиглаж болно :
>>> f= fac_sum
>>> f(1)
0 xurtelx niilber :0, factorial :1
>>> f(5)
0 xurtelx niilber :0, factorial :1
1 xurtelx niilber :1, factorial :1
2 xurtelx niilber :3, factorial :2
3 xurtelx niilber :6, factorial :6
4 xurtelx niilber :10, factorial :24

No comments:

Post a Comment

Сэтгэгдэл үлдээж байгаад баярлалаа :]