404 Blog Not Found:�ץ�����ޡ��Ǥʤ��Ƥ�̾�����餤�Ф��Ƥ����������르�ꥺ��x10������ۤɤ�ȿ���ˤʤ�Ȥϡ�200�֥��ޤ��餤��ͽ�ۤ��Ƥ��������⤤���Ȥϡ�
�Ȥꤢ�������ܤβ����֥��르�ꥺ��ɴ���פȤ��ơ������ޥ���ޡ��Τ������פ�Ʊ���褦����blog����Ƥ�񤤤Ƥ������Ȥˤ��롣�֥ᥤ��ڡ����פΡ����르�ꥺ�����罸!��C&R����� - �ȥåץڡ����פ�����Ŭ���������Ƥ��������֤��ξ��ư�����륳���ɥ���ץ�פϤ����Ǥʤ��Ƚ񤱤ʤ��Τǡ�
���������֥��ޥ���ޡ��Τ�����פ��Ͻ����Ͻ���Ʊ�ˤʤ�Ϥ�������Ǥ��ʸ�����Τ��Ȥϡ֥��鸫�פʤ�̡֥���ñ¤¡×¤ï¿½ï¿½Æ¤ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½È¤ï¿½ï¿½ï¿½ï¿½ï¤±ï¿½Ç¡ï¿½ï¿½ï¿½Ê¸ï¿½ï¿½ï¿½Ñ¤ï¿½ï¿½Æ¾Ò²ð¤¹¤ï¿½Î¤ï¿½ï¿½ï¿½ï¿½ï¿½Entry��
�ҥ�ȤȤʤä�
���르�ꥺ��ǤϤʤ��Ǥ����������ե��ܥʥå����� - Shu�ΤĤޤŤ��������르�ꥺ��ǤϤʤ��Ǥ������ե��ܥʥå�����Ϥ������Ǥ��ͤ���
�˴��ա�
�ե��ܥʥå������O()��ؤ�
�ե��ܥʥå����Ȥ�������ʹ�������Ȥ�����Ǥ��礦�������إե���ˤϼ¤ˤ������ߤο��ǡ��������ߡ��ޤ˥Υߥ͡��Ȥ��줿���������Υ��ޥͥ���ؿ��ز��פǤ⡢�����Ǥ��줬���夲���ޤ�����
���ο����Ի׵Ĥʤ��Ȥˡ��������μ¤�¿���ΤȤ������о줷�ޤ����ܤ����ϡ�404 Blog Not Found:The Fibonacci Code�פǾҲ𤷤��ƽ�򤢤��äƤ�餦���Ȥˤ��ơ��ܽ�ϥ��르�ꥺ����ܤʤΤǡ����η׻�ˡ�򸫤Ƥ������Ȥˤ��Ƥ����ޤ���
�ޤ��ϥե��ܥʥå�����������顣
F(1) = 1 F(2) = 1 F(3) = 2 = 1+1 F(4) = 3 = 1+2 F(5) = 5 = 2+3
F(3)�ʹߤϡ�ñ�������Ĥο���Â���Ƥ����Ф������Ȥ��狼��ޤ�������ò¤½¤Î¤Þ¤ï¿½JavaScript��ľ���ȡ������ʤ�Ǥ��礦��
function fib(n){ if (n <= 2) return 1; return fib(n-1) + fib(n-2); }
�Ǥϡ��������®ư�����Ƥߤޤ��礦��
- �ץ������:
- ����:
- ���顼:
�ʥ����֤ʼ����ˤϥ�ʤ�����
�������ˤ��ޤ��Ԥ��ޤ����������������Ǿ�Υץ�������n <= 10
��25�Ȥ��ˤ��Ƹ��ޤ��礦���ɤ��ʤ�ޤ�����?���ʤ�ư���ä���Ȥ��ޤ���Ǥ�����?����������30�Ȥ������Ф�����ʤ��Ǥ����������֥饦�������ߤޤ�ʤ��ʤ�ޤ���
�ʤ��������ʤä��ΤǤ��礦���ץ�������⤦���٤褯���Ƥߤޤ��礦���Ǹ�ΤȤ����ǡ�fib()
�ϼ�ʬ���Ȥ����ٸƤ�Ǥ��ޤ����Ȥ������Ȥϡ�fib(3)
���Թ�3��fib(4)
��5��fib(5)
��8��fib()
��ƤӤޤ������̲�����ȡ�n���ܤΥե��ܥʥå�����F(n)�Ȥ���ȡ������׻�����ˤ�fib()
��F(n+1)��ƤФʤ���Фʤ�ʤ����Ȥˤʤ�ΤǤ���ʸ���̤�������Ū�˼�֤�������ΤǤ���
���Ρ֤��������ò¤¯¤Î¤Ë¡ï¿½ï¿½Ç¡ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ç¤ï¿½ï¿½ï¿½È²ò¤¯¼ï¿½Ö¤ï¿½ï¿½É¤ï¿½Û¤ï¿½ï¿½ç¤ï¿½ï¿½ï¿½Ê¤ï¿½Î¤ï¿½ï¿½×¤È¤ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ò¡¢·×»ï¿½ï¿½ï¿½ï¿½Ø¼Ô¤ï¿½ï¿½ï¿½ï¿½Ï¡Ö·×»ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½×¤È¸Æ¤Ó¤Þ¤ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Æ¤ï¿½ï¿½ï¿½ò¤¢¤ï¿½ï¤¹ï¿½Î¤ï¿½O(n�δؿ�)�Ȥ��������ò¤·¤Þ¤ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ï¡ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ò¤¯¼ï¿½Ö¤Ï¡ï¿½n�δؿ��Υ����������ç¤ï¿½ï¿½ï¿½Ê¤ï¿½×¤È¤ï¿½ï¿½ï¿½ï¿½ï¿½Ì£ï¿½Ë¤Ê¤ï¿½Þ¤ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½É¤ï¿½ï¿½ï¿½Î²ï¿½ï¿½ï¿½ï¿½á¥¢ï¿½ë¥´ï¿½ê¥ºï¿½ï¿½Ï¡ï¿½O(2n)�Ȥ������Ȥˤʤ�ޤ���
����äȤ������פΤ�����
�����ǡ��ץ������ν����򾯤��Ѥ��Ƥߤޤ��礦����ʬ���Ȥ�Ƥֲ���򡢥ץ����������2��ǤϤʤ�1��ˤ���ΤǤ���
- �ץ������:
- ����:
- ���顼:
JavaScript�ϴؿ�����˴ؿ�������Ǥ���Τǡ�����ä����ò¤¤½ï¿½ï¿½ï¿½ï¿½Ë¤Ê¤Ã¤Æ¤ï¿½ï¿½Þ¤ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Î´Ø¿ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ï¡ï¿½F(n-1)�η�̤�F(n-2)�η�̤�����Ȥ����Ϥ��Ƥ��뤳�ȤǤ����åµï¿½ï¿½f
���ƤФ�����ϡ����٤�n��3�ʾ�ʤ�n-1��Ȥʤ�ޤ������������Ǹ�����O(n)�Ȥ������Ȥˤʤ�ޤ���
����Ǥ⡢n�������Ƥ����˽��äƷ׻��̤������뤳�Ȥˤϰ㤤����ޤ��󡣤���ˤ褤��ˡ�Ϥʤ��Ǥ��礦����
O(1)�ؤ�ƻ����1:������Ȥ�
�¤ϡ��ե��ܥʥå������ȯ�Ƿ׻�������������Ǥ�¸�ߤ��ޤ���
����ò¤½¤Î¤Þ¤Þ°Ü¿ï¿½ï¿½ï¿½ï¿½ï¿½Ð¡ï¿½ï¿½É¤ï¿½Ê¤ï¿½n���ç¤ï¿½ï¿½ï¿½Æ¤â¤³ï¿½Î¼ï¿½ï¿½Ï°ï¿½ó¤·¤ï¿½ï¿½×»ï¿½ï¿½ï¿½ï¿½Ê¤ï¿½ï¿½Î¤Ç¡ï¿½ï¿½ï¿½ï¿½Î¼ï¿½ï¿½ï¿½O(1)�Ǥ���¤�O(1)�����ʤ��n���ç¤ï¿½ï¿½ï¿½Ë´Ø¤ï¿½é¤ºï¿½×»ï¿½ï¿½Ì¤ï¿½ï¿½Ñ¤ï¿½ï¿½Þ¤ï¿½ï¿½ï¿½
����Ū�˸��Ƥ�¤������������ǡ��夬���äƤ���Τ����������������Ф��������֤äƤ���Ȥ��������Ի׵ĤǤ�����������ԥ塼������Ǥ����ˤϤ��줬����Ȥʤꤨ�ޤ���
�������ľ��JavaScript�˰ܿ�����ȡ������ʤ�ޤ���
- �ץ������:
- ����:
- ���顼:
���Ƥ��̤ꡢ�����Τ�Τ��ɤ����Ƥ⺮���äƤ��ޤ��ޤ�������ϥ���ԥ塼��������̩���׻��Ǥ��ʤ�����̩�ˤ�JavaScript�α黻�Ǥϸ�̩���Ф��ʤ�����Ǥ���1 / 3 * 3
��1�ˤʤ�ʤ��Τ�Ʊ�ͤ���ͳ�Ȥ����Ф�ʬ����Ǥ��礦����
����ʲ��Τ褦�ˤ��뤳�Ȥǡ���������ϲ���Ǥ��ޤ���
- �ץ������:
- ����:
- ���顼:
����Ǥ⡢�����������٤Ƥ����֤�����ݤǤ��������Τޤ޷׻����ơ�����O(1)����ˡ�Ϥʤ��ΤǤ��礦��?
O(1)�ؤ�ƻ����2:��ⲽ
�ե��ܥʥå���F(n)�ϡ�n��Ʊ���ʤ�ɬ��Ʊ�����ˤʤ�ޤ�������ʤ�С����ٷ׻�������̤�Ф��Ƥ����С����������׻�����ɬ�פϤʤ����Ȥˤʤ�ޤ�����������ȡ��ʲ��Τ褦�ˤʤ�ޤ���
- �ץ������:
- ����:
- ���顼:
�������ư��ٽФ�����̤�Ф��Ƥ����ơ�����Ф����Ȥ��Ȥ�����ˡ�ϥ�ⲽ(memoization)�Ȥ����Τ��Ƥ��ޤ������Ƥ��̤ꡢ����̤�ʥʥ����֤ʷ׻�ˡ��ȤäƤ���Τˡ�O(2n)��O(1)�ʸ�̩�ˤϡ��ǽ�ΰ�����O(n))�ˤʤäƤ��ޤ��ΤǤ���
���줳���������르�ꥺ������̣�Ǥ���
�ޤȤ�
- �ʥ����֤ʼ����򤽤Τޤ޻Ȥ����ϡ�O()���̣���Ƥ���
- O()������������Ȥ��äơ��������ڤ��Ȥϸ¤�ʤ�
- ����Ū�������������ץ�������΢�ڤ��뤳�Ȥ�����
- �ʥ����֤Υ��르�ꥺ��⡢�̤Υ��르�ꥺ����Ȥ߹�碌�ˤ�ä�O()����Ū�˲����뤳�Ȥ⤢��
Dan the Algorithmic Man
������ȾΤ�����1�फ����5��򼨤�
�����������ǽ��5�फ���F(3)�ʹߤϡ����狼��ޤ��פ�����������ꤹ��
����Ĥ����ˤ����ƾ����Ф��������Ķ���Ƥ��ޤ���
�ǽ��5������ʤ顢�⤷��������
F(n)=n-1 (n mod 4��1) or n (n mod 4��1)
�Ȥ������󤫤⤷��ʤ��Ȥ����Τˡ�