Найти - Пользователи
Полная версия: Программа по Python. Срочно нужна помощь
Начало » Центр помощи » Программа по Python. Срочно нужна помощь
1
pythoncos
Нужна помощь в написании программы для вычисления y=cos(cos(..cos(x)..) (n раз)
Заранее благодарен за потраченое время.
Rodegast
 >>> from math import cos
>>> def coss(i, n=1):
...     if n == 1:
...         return cos(i)
...     return cos(coss(i, n-1))
>>> cos(10)
-0.8390715290764524
>>> coss(10)
-0.8390715290764524
>>> coss(10,2)
0.6681539175313869
>>> cos(cos(10))
0.6681539175313869
py.user.next
Rodegast
 def coss(i, n=1):
  
>>> from math import cos
>>> 
>>> def coss(i, n=1):
...     if n == 1:
...         return cos(i)
...     return cos(coss(i, n-1))
... 
>>> coss(1, 1000)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in coss
  File "<stdin>", line 4, in coss
...

Лучше через цикл сделать. (Оно и не сложно совсем.)
Rodegast
 >>> import sys
>>> sys.setrecursionlimit(10001)
>>> coss(10,1000)
0.7390851332151607
pythoncos
py.user.next
Rodegast
Можно через цикл, если не трудно?
Rodegast
 >>> from math import cos
>>> n = 2
>>> i = 10
>>> while not n == 0:
...     i = cos(i)
...     n -= 1
>>> i
0.6681539175313869
py.user.next
  
>>> import math
>>> 
>>> def cosn(x, n):
...     out = x
...     for _ in range(n):
...         out = math.cos(out)
...     return out
... 
>>> cosn(123, 1) == math.cos(123)
True
>>> cosn(123, 2) == math.cos(math.cos(123))
True
>>> cosn(123, 1000000)
0.7390851332151607
>>> 
>>> cosn(123, 0)
123
>>>
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB