본문 바로가기

프로그래밍/Python

[Py] 신이 보여준 정리

반응형
신이 보여준 정리를 보고 재미있을 것 같아서 짜봤다. 답을 대충 짐작하고 나서는 예쁘게 n + 1 = sqrt(1 + (n) sqrt(1 + (n+1) sqrt(..))) 이 n x (n + 1) = n x sqrt(1 + (n+1) sqrt(...)) 으로 정리되는 걸 알았다.
#!/usr/bin/env python

#     _____________________________
#    /          ___________________
#   /  1 + 2   /        ___________  = ?
# |/         |/ 1 + 3 |/ .....
#
#


import math

def get_prev(x, n):
       return math.sqrt(1 + n*x)

def eval_first(x0, n):
       #print ""
       #print "======================"
       x = x0
       while n > 1:
               #print n, x
               x = get_prev(x, n)
               n -= 1
       #print "----------------------"
       #print ""
       return x

x = 30
for i in range(300):
       x = x*x
       print x, eval_first(x, 30)
       print x, eval_first(x, 100)

C:\Documents and Settings\daewon>sqrt_recurs.py
900 3.00000001286
900 3.0
810000 3.00000003914
810000 3.0
656100000000 3.00000009182
656100000000 3.0
430467210000000000000000 3.00000019735
430467210000000000000000 3.0
185302018885184100000000000000000000000000000000 3.0000004088
185302018885184100000000000000000000000000000000 3.0
34336838202925124846578490892810000000000000000000000000000000000000000000000000
000000000000000 3.00000083251
34336838202925124846578490892810000000000000000000000000000000000000000000000000
000000000000000 3.0
11790184577738583171520872861412518665678211592275841109096961000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000 3.00000168171
11790184577738583171520872861412518665678211592275841109096961000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000 3.0
13900845237714473276493978678966130311421885080852913799160482443003607262976643
59410017691541096095218116655405488994355210000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000
Traceback (most recent call last):
File "C:\Documents and Settings\daewon\sqrt_recurs.py", line 23, in
print x, eval_first(x, 30)
File "C:\Documents and Settings\daewon\sqrt_recurs.py", line 14, in eval_first


그리고, 아마 이렇겠더라.


---

from math import sqrt

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

import numpy as np

import pprint


def f_god(n, r):

    '''               .---------------------------------------

         f(n, r) =  |/ 1 + 2   .------------------------------

                             |/ 1 +  3   .--------------------

                                       |/ 1 +  ...  n x r

                                                       

    '''


    l = range(2, n+1)

    l.reverse()

    return reduce(lambda x, y: sqrt(1 + y*x), l, r)



if __name__=="__main__":

    r = np.arange(50, 500, 10)

    n = np.arange(100, 500, 25)

    R, N = np.meshgrid(r, n)

    fig = plt.figure() 

    ax = fig.add_subplot(111, projection='3d')  

    zs = np.array([f_god(x,y) for x,y in zip(np.ravel(R), np.ravel(N))]) 

    Z = zs.reshape(R.shape)

    print Z

    

    ax.plot_surface(R, N, Z) 

    ax.set_title('Ramanujan equation from god') 

    ax.set_xlabel('r') 

    ax.set_ylabel('n') 

    ax.set_zlabel('result') 

    plt.show() 





728x90