1. 程式人生 > >黃金分割二分效能分析

黃金分割二分效能分析

F0 = 0

F1 = 0

F2 = F1 + t(2) + F0 + t(1)


Fn = Fn-1 + t(n) + Fn-2 + t(n-1)


t(n) 為斐波那契數列

En = Fn / sum
= Fn / t(n+1)
= (Fn-1 + Fn-2 + t(n+1) ) /  t(n+1)
= 1 + Fn-1 /  t(n+1) + Fn-2 /  t(n+1)
= 1 + En-1 * t(n) / t(n+1) + En-2 *t(n-1)

從這裡可以看出,期望正好是左右2塊的期望值加上深度1,在數學計算與邏輯上也是一致的



#include <iostream>


using namespace std;


int fib[1024] = {0};
//fib[0] = 0;
//fib[1] = 1;
int getFib(int i)
{
//

cout << "[i] = " << i << endl;
if (fib[i] > 0)
{
return fib[i];
}


if (i == 0)
{
return 0;
}
if (i == 1)
{
return 1;
}

int value = getFib(i - 1) + getFib(i - 2);
fib[i] = value;

return value;
}


int sum[1024] = {
0
};
//sum[0] = 0;
//sum[1] = 0;


int getSum(int i)
{
if (i == 0 || i == 1)
{
return 0;
}


if (sum[i] > 0)
{
return sum[i];
}

int value = getSum(i - 1) + getSum(i - 2) + getFib(i + 1);
sum[i] = value;

return value;
}


int main()
{
fib[0] = 0;
fib[1] = 1;
sum[0] = 0;
sum[1] = 0;
int num = 0;
cout << "put in number: ";
cin >> num;
for(int i = 2; i < num; i++)
{
cout << "i = " << i << " sum = [" << getSum(i) << "] T(" << i + 1 << ") = " << getFib(i+1)
 << " E = " << getSum(i) / getFib(i+1)  << endl;
}
return 0;
}

#include <iostream>

using namespace std;

int fib[1024] = {0};
//fib[0] = 0;
//fib[1] = 1;
int getFib(int i)
{
//	cout << "[i] = " << i << endl;
	if (fib[i] > 0)
	{
		return fib[i];
	}

	if (i == 0)
	{
		return 0;
	}
	if (i == 1)
	{
		return 1;
	}
	
	int value = getFib(i - 1) + getFib(i - 2);
	fib[i] = value;
	
	return value;
}

int sum[1024] = {
	0
};
//sum[0] = 0;
//sum[1] = 0;

int getSum(int i)
{
	if (i == 0 || i == 1)
	{
		return 0;
	}

	if (sum[i] > 0)
	{
		return sum[i];
	}
	
	int value = getSum(i - 1) + getSum(i - 2) + getFib(i + 1);
	sum[i] = value;
	
	return value;
}

int main()
{
	fib[0] = 0;
	fib[1] = 1;
	sum[0] = 0;
	sum[1] = 0;
	int num = 0;
	cout << "put in number: ";
	cin >> num;
	for(int i = 2; i < num; i++)
	{
		cout << "i = " << i << " sum = [" << getSum(i) << "] T(" << i + 1 << ") = " << getFib(i+1)
 << " E = " << getSum(i) / getFib(i+1)  << endl;
	}
	return 0;
}


put in number: 40
i = 2 sum = [2] T(3) = 2 E = 1
i = 3 sum = [5] T(4) = 3 E = 1
i = 4 sum = [12] T(5) = 5 E = 2
i = 5 sum = [25] T(6) = 8 E = 3
i = 6 sum = [50] T(7) = 13 E = 3
i = 7 sum = [96] T(8) = 21 E = 4
i = 8 sum = [180] T(9) = 34 E = 5
i = 9 sum = [331] T(10) = 55 E = 6
i = 10 sum = [600] T(11) = 89 E = 6
i = 11 sum = [1075] T(12) = 144 E = 7
i = 12 sum = [1908] T(13) = 233 E = 8
i = 13 sum = [3360] T(14) = 377 E = 8
i = 14 sum = [5878] T(15) = 610 E = 9
i = 15 sum = [10225] T(16) = 987 E = 10
i = 16 sum = [17700] T(17) = 1597 E = 11
i = 17 sum = [30509] T(18) = 2584 E = 11
i = 18 sum = [52390] T(19) = 4181 E = 12
i = 19 sum = [89664] T(20) = 6765 E = 13
i = 20 sum = [153000] T(21) = 10946 E = 13
i = 21 sum = [260375] T(22) = 17711 E = 14
i = 22 sum = [442032] T(23) = 28657 E = 15
i = 23 sum = [748775] T(24) = 46368 E = 16
i = 24 sum = [1265832] T(25) = 75025 E = 16
i = 25 sum = [2136000] T(26) = 121393 E = 17
i = 26 sum = [3598250] T(27) = 196418 E = 18
i = 27 sum = [6052061] T(28) = 317811 E = 19
i = 28 sum = [10164540] T(29) = 514229 E = 19
i = 29 sum = [17048641] T(30) = 832040 E = 20
i = 30 sum = [28559450] T(31) = 1346269 E = 21
i = 31 sum = [47786400] T(32) = 2178309 E = 21
i = 32 sum = [79870428] T(33) = 3524578 E = 22
i = 33 sum = [133359715] T(34) = 5702887 E = 23
i = 34 sum = [222457608] T(35) = 9227465 E = 24
i = 35 sum = [370747675] T(36) = 14930352 E = 24
i = 36 sum = [617363100] T(37) = 24157817 E = 25
i = 37 sum = [1027198944] T(38) = 39088169 E = 26
i = 38 sum = [1707808030] T(39) = 63245986 E = 27
i = 39 sum = [-1457626167] T(40) = 102334155 E = -14


修改程式

#include <iostream>

using namespace std;

long int fib[1024] = {0};
//fib[0] = 0;
//fib[1] = 1;
long getFib(int i)
{
//	cout << "[i] = " << i << endl;
	if (fib[i] > 0)
	{
		return fib[i];
	}

	if (i == 0)
	{
		return 0;
	}
	if (i == 1)
	{
		return 1;
	}
	
	long value = getFib(i - 1) + getFib(i - 2);
	fib[i] = value;
	
	return value;
}

long long sum[1024] = {
	0
};
//sum[0] = 0;
//sum[1] = 0;

long getSum(int i)
{
	if (i == 0 || i == 1)
	{
		return 0;
	}

	if (sum[i] > 0)
	{
		return sum[i];
	}
	
	long value = getSum(i - 1) + getSum(i - 2) + getFib(i + 1);
	sum[i] = value;
	
	return value;
}

int main()
{
	fib[0] = 0;
	fib[1] = 1;
	sum[0] = 0;
	sum[1] = 0;
	int num = 0;
	cout << "put in number: ";
	cin >> num;
	for(int i = 2; i < num; i++)
	{
		cout << "i = " << i << "	sum = [" << getSum(i) << "]	T(" << i + 1 << ") = " << getFib(i+1)
 << "	E = " <<(float) getSum(i) /(float) getFib(i+1)  << endl;
	}
	return 0;
}

$ ./test
put in number: 50
i = 2 sum = [2] T(3) = 2 E = 1
i = 3 sum = [5] T(4) = 3 E = 1.66667
i = 4 sum = [12] T(5) = 5 E = 2.4
i = 5 sum = [25] T(6) = 8 E = 3.125
i = 6 sum = [50] T(7) = 13 E = 3.84615
i = 7 sum = [96] T(8) = 21 E = 4.57143
i = 8 sum = [180] T(9) = 34 E = 5.29412
i = 9 sum = [331] T(10) = 55 E = 6.01818
i = 10 sum = [600] T(11) = 89 E = 6.74157
i = 11 sum = [1075] T(12) = 144 E = 7.46528
i = 12 sum = [1908] T(13) = 233 E = 8.18884
i = 13 sum = [3360] T(14) = 377 E = 8.91247
i = 14 sum = [5878] T(15) = 610 E = 9.63607
i = 15 sum = [10225] T(16) = 987 E = 10.3597
i = 16 sum = [17700] T(17) = 1597 E = 11.0833
i = 17 sum = [30509] T(18) = 2584 E = 11.8069
i = 18 sum = [52390] T(19) = 4181 E = 12.5305
i = 19 sum = [89664] T(20) = 6765 E = 13.2541
i = 20 sum = [153000] T(21) = 10946 E = 13.9777
i = 21 sum = [260375] T(22) = 17711 E = 14.7013
i = 22 sum = [442032] T(23) = 28657 E = 15.4249
i = 23 sum = [748775] T(24) = 46368 E = 16.1485
i = 24 sum = [1265832] T(25) = 75025 E = 16.8721
i = 25 sum = [2136000] T(26) = 121393 E = 17.5957
i = 26 sum = [3598250] T(27) = 196418 E = 18.3193
i = 27 sum = [6052061] T(28) = 317811 E = 19.043
i = 28 sum = [10164540] T(29) = 514229 E = 19.7666
i = 29 sum = [17048641] T(30) = 832040 E = 20.4902
i = 30 sum = [28559450] T(31) = 1346269 E = 21.2138
i = 31 sum = [47786400] T(32) = 2178309 E = 21.9374
i = 32 sum = [79870428] T(33) = 3524578 E = 22.661
i = 33 sum = [133359715] T(34) = 5702887 E = 23.3846
i = 34 sum = [222457608] T(35) = 9227465 E = 24.1082
i = 35 sum = [370747675] T(36) = 14930352 E = 24.8318
i = 36 sum = [617363100] T(37) = 24157817 E = 25.5554
i = 37 sum = [1027198944] T(38) = 39088169 E = 26.279
i = 38 sum = [1707808030] T(39) = 63245986 E = 27.0026
i = 39 sum = [2837341129] T(40) = 102334155 E = 27.7262
i = 40 sum = [4710729300] T(41) = 165580141 E = 28.4498
i = 41 sum = [7815984725] T(42) = 267914296 E = 29.1735
i = 42 sum = [12960208462] T(43) = 433494437 E = 29.8971
i = 43 sum = [21477601920] T(44) = 701408733 E = 30.6207
i = 44 sum = [35572713552] T(45) = 1134903170 E = 31.3443
i = 45 sum = [58886627375] T(46) = 1836311903 E = 32.0679
i = 46 sum = [97430556000] T(47) = 2971215073 E = 32.7915
i = 47 sum = [161124710351] T(48) = 4807526976 E = 33.5151
i = 48 sum = [266334008400] T(49) = 7778742049 E = 34.2387
i = 49 sum = [440044987776] T(50) = 12586269025 E = 34.9623


採用對稱二分方法,期望

 

E = lg(x) / lg(2)

通過驗證發現,對稱二分方法和黃金分割二分方法的期望差不多,對稱二分方法的期望稍微小一點