• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# mp_20200515.php puggan   May 15th, 2020 1,053 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. <?php
2.
3. /*
4.  * a & b is positive integers
5.  * f(0) = a;
6.  * f(1) = a + b;
7.  * f(n) = f(n-1) + f(n-2)
8.  * f(x) = 1e6, find a & b so that x get as high as posible
9.  *
10.  * g(0,a,b) = a
11.  * g(1,a,b) = a+b
12.  * g(n,a,b) = g(n-1,a,b) + g(n-2,a,b)
13.  *
14.  * g(n,0,1) => fibinatchii => g(31,0,1) > 1e6 => n < 31
15.  * best found; g(18,144,154) = 1e6
16.  * g(18,a,b) = 4181a + 2584b
17.  * g(19,a,b) = 6765a + 4181b, x > 18 => 4181b < 1e6 => b < 240
18.  */
19.
20. function fibMaker(\$a, \$b) {
21.     yield \$a;
22.     while(true) {
23.         yield \$b += \$a;
24.         yield \$a += \$b;
25.     }
26. }
27.
28. function serie(\$n = 0, \$max = 240) {
29.     while(\$n < \$max) {
30.         yield \$n++;
31.     }
32. }
33.
34. function getX(\$a, \$b, \$goal = 1e6, \$limit = 31) {
35.     \$fib = fibMaker(\$a,\$b);
36.     \$index = 0;
37.     foreach(\$fib as \$n) {
38.         if(\$n == \$goal) {
39.             return \$index;
40.         }
41.         if(\$n > \$goal) {
42.             return 0;
43.         }
44.         if(\$index > \$limit) {
45.             return 0;
46.         }
47.         \$index++;
48.     }
49.     return 0;
50. }
51.
52. \$best = 1;
53. foreach(serie(1) as \$n1) {
54.     echo \$n1, "\r";
55.     foreach(serie(0) as \$n2) {
56.         \$x = getX(\$n1, \$n2);
57.         if(\$x >= \$best) {
58.             echo 'g(', \$x, ',', \$n1, ',', \$n2, ') = 1e6', PHP_EOL;
59.             \$best = \$x;
60.         }
61.     }
62. }
63. echo '    ', PHP_EOL;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Top