题目
实验I:体验神奇的算法
本实验要求根据给定的正整数n计算第n个斐波那契数。课堂上已经讲过4种不同的算法,请选择自己最熟悉的编程语言实现这些算法来体验算法求解问题的神奇,下列基本要求必须完成:
1、 设计一个交互界面(例如菜单)供用户选择,如果可能,最好是一个图形化用户界面。
2、 利用迭代算法寻找不超过编程环境能够支持的最大整数的斐波那契数是第几个斐波那契数。(Java: 231-1 for int, 263-1 for long)
java int最大值是:2147483647
int 类型数占4个byte.
1byte=8bit
也就是有32个bit占位符
//找到超过int最大值之前的一个斐波拉契数字是第几个
public static int findMax(){
int index = 2;
int first = 1,second = 1;
int cur = 0;
while(first + second > 0){
cur = first + second;
first = second;
second = cur;
index++;
}
System.out.println("没超过int的最大斐波那契数字为:"+cur+"位于"+index);
return index;
}
3、 根据第二步计算的最大的斐波那契数序号n,采用递归方式计算第n个斐波那契数,看其是否可以在1分钟内完成。
case 2:
if(maxIndex == -1){
System.out.println("请至少先运行第一个功能再来试试吧");
break;
}
//long start = new Date().getTime();
long start = System.currentTimeMillis();
recur(maxIndex);
long end = System.currentTimeMillis();
System.out.println("用了" + (end - start) / 1000 + "秒");
break;
//递归求解需要多少秒呢?
public static int recur(int index){
if(index == 1 || index == 2){
return 1;
}
return recur(index - 1) + recur(index - 2);
}
4、 利用递归算法计算你的计算机能够在30秒内计算出的最大斐波那契数是第几个,计算出下一个斐波那契数的时间是多少,利用迭代算法求解同样的问题。
case 3:
int timeIndex = timeSearchForRecur();
long s = System.currentTimeMillis();
recur(timeIndex + 1);
long e = System.currentTimeMillis();
System.out.println("第" + (timeIndex + 1)+ "位斐波那契数递归用了" + (e - s) / 1000 + "秒 (超过了30s3)");
break;
//找到30s内可以递归出来的最大斐波那契数列(递归)
public static int timeSearchForRecur(){
int index = 1;
while(true){
long cur = System.currentTimeMillis();
recur(index);
long end = System.currentTimeMillis();
if(end - cur >= 30 * 1000){
System.out.println("(递归)30s内最大的斐波那契数位于" + (index - 1));
return index - 1;
}
index++;
}
}
5、 利用公式F(n) = [fn/sqrt(5)]快速计算第n个斐波那契数,找出出现误差时的最小n值。
//公式求解快速计算出第n个斐波那契数字
public static void mathForFib(int index){
long result = 1;
float temp = (float) Math.sqrt(5.0);
result = (long)((Math.pow((1 + temp) / 2, index) - Math.pow((1 - temp) / 2, index)) / temp);
System.out.println("第" + index + "位的" + result);
}
//公式求解快速计算出第n个斐波那契数字,找出出现误差最小的n值
public static void mathFib(){
int index = 1;
long result = 1;
float temp = (float) Math.sqrt(5.0);
while(true){
result = (long)((Math.pow((1 + temp) / 2, index) - Math.pow((1 - temp) / 2, index)) / temp);
if(result != fib(index)){
break;
}
index++;
}
System.out.println("出现误差的最小n值为第" + index + "位的" + result);
}
6、 利用矩阵相乘方法计算第n个斐波那契数。
复杂度分析
时间复杂度:O(\log n)O(logn)。
空间复杂度:O(1)O(1)。
参考代码:http://blog.sina.com.cn/s/blog_6ea2c6a20100x359.html
static long matrix[][] = {{1,1},{1,0}};
// 矩阵方式求斐波拉契数列
public static long matrixForFib(int index){
if (index <= 0) {
throw new RuntimeException("输入参数小于1");
}
if (index == 1 || index == 2) {
return 1;
}
long[][] tem = matrix;
for (int i = 1; i < index - 2; i++) {
//已知一开始tmp和matrix然后迭代计算到最后
tem = doMatrix(tem,matrix);
}
return tem[0][0] + tem[1][0];
}
public static long[][] doMatrix(long[][] a,long[][] b){
long[][] res = new long[2][2];
res[0][0] = a[0][0] * b[0][0] + a[0][1] * b[1][0];
res[0][1] = a[0][0] * b[0][1] + a[0][1] * b[1][1];
res[1][0] = a[1][0] * b[0][0] + a[1][1] * b[1][0];
res[1][1] = a[1][0] * b[0][1] + a[1][1] * b[1][1];
return res;
}
7、 对于相同的输入n值,比较上述四种方法的基本操作次数,以掌握对数、线性和指数增长率的极大差别。
实现代码
package week1;
import java.util.Scanner;
/**
* @author newThread
* @className Solution
* @date 2021/10/15 16:10
*/
class Solution {
static int max = Integer.MAX_VALUE;
//找到超过int最大值之前的一个斐波拉契数字是第几个
public static int findMax(){
int index = 2;
int first = 1,second = 1;
int cur = 0;
while( first + second > 0){
cur = first + second;
first = second;
second = cur;
index++;
}
System.out.println("没超过int的最大斐波那契数字为:"+cur+"位于"+index);
return index;
}
public static long fib(int index){
if(index == 1 || index == 2){
return 1;
}
long first = 1,second = 1;
long cur = 0;
index = index - 2;
while(index > 0){
cur = first + second;
first = second;
second = cur;
index--;
}
return cur;
}
//递归求解需要多少秒呢?
public static int recur(int index){
if(index == 1 || index == 2){
return 1;
}
return recur(index - 1) + recur(index - 2);
}
//找到30s内可以递归出来的最大斐波那契数列(递归)
public static int timeSearchForRecur(){
int index = 1;
while(true){
long cur = System.currentTimeMillis();
recur(index);
long end = System.currentTimeMillis();
if(end - cur >= 30 * 1000){
System.out.println("(递归)30s内最大的斐波那契数位于" + (index - 1));
return index - 1;
}
index++;
}
}
//找到30s内可以递归出来的最大斐波那契数列(递归)
public static int timeSearchForDieDai(){
int index = 1;
while(true){
long cur = System.currentTimeMillis();
fib(index);
long end = System.currentTimeMillis();
if(end - cur >= 30 * 1000){
System.out.println(end -cur);
System.out.println("(迭代)30s内最大的斐波那契数位于" + (index - 1));
return index - 1;
}
index++;
}
}
//公式求解快速计算出第n个斐波那契数字,找出出现误差最小的n值
public static void mathFib(){
int index = 1;
long result = 1;
float temp = (float) Math.sqrt(5.0);
while(true){
result = (long)((Math.pow((1 + temp) / 2, index) - Math.pow((1 - temp) / 2, index)) / temp);
if(result != fib(index)){
break;
}
index++;
}
System.out.println("出现误差的最小n值为第" + index + "位的" + result);
}
//公式求解快速计算出第n个斐波那契数字
public static void mathForFib(int index){
long result = 1;
float temp = (float) Math.sqrt(5.0);
result = (long)((Math.pow((1 + temp) / 2, index) - Math.pow((1 - temp) / 2, index)) / temp);
System.out.println("第" + index + "位的" + result);
}
static long matrix[][] = {{1,1},{1,0}};
// 矩阵方式求斐波拉契数列
public static long matrixForFib(int index){
if (index <= 0) {
throw new RuntimeException("输入参数小于1");
}
if (index == 1 || index == 2) {
return 1;
}
long[][] tem = matrix;
for (int i = 1; i < index - 2; i++) {
//已知一开始tmp和matrix然后迭代计算到最后
tem = doMatrix(tem,matrix);
}
return tem[0][0] + tem[1][0];
}
public static long[][] doMatrix(long[][] a,long[][] b){
long[][] res = new long[2][2];
res[0][0] = a[0][0] * b[0][0] + a[0][1] * b[1][0];
res[0][1] = a[0][0] * b[0][1] + a[0][1] * b[1][1];
res[1][0] = a[1][0] * b[0][0] + a[1][1] * b[1][0];
res[1][1] = a[1][0] * b[0][1] + a[1][1] * b[1][1];
return res;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int maxIndex = -1;
while(true){
System.out.println("-------------------------实验I:体验神奇的算法----------------------------|\n");
System.out.println("| 1.利用迭代算法找出不超过编程环境能够支持的最大整数的斐波那契数是第几个数。 | ");
System.out.println("| 2.根据选项1的序号n,用递归计算第n个斐波那契数.看其是否可以在1分钟内完成。 | ");
System.out.println("| 3.利用递归算法计算你的计算机能够在30秒内计算出的最大斐波那契数是第几个, |\n" +
"| 并且计算出下一个斐波那契数的时间 |");
System.out.println("| 4.利用公式计算第n个斐波那契数 | ");
System.out.println("| 5.利用公式计算出现误差的最小斐波那契是第几个 | ");
System.out.println("| 6.利用矩阵计算第n个斐波那契数 | ");
System.out.println("| 7.退出系统 | ");
System.out.println("|----------------------------------------------------------------------");
System.out.println("请输入对应选项序号:");
int choice = scanner.nextInt();
switch (choice){
case 1:
maxIndex = findMax();
break;
case 2:
if(maxIndex == -1){
System.out.println("请至少先运行第一个功能再来试试吧");
break;
}
//long start = new Date().getTime();
long start = System.currentTimeMillis();
recur(maxIndex);
long end = System.currentTimeMillis();
System.out.println("用了" + (end - start) / 1000 + "秒");
break;
case 3:
int timeIndex = timeSearchForRecur();
long s = System.currentTimeMillis();
recur(timeIndex + 1);
long e = System.currentTimeMillis();
System.out.println("第" + (timeIndex + 1)+ "位斐波那契数递归用了" + (e - s) / 1000 + "秒 (超过了30s3)");
break;
case 4:
System.out.println("您想求出第几位斐波那契数?");
int index = scanner.nextInt();
mathForFib(index);
break;
case 5:
mathFib();
break;
case 6:
System.out.println("您想求出第几位斐波那契数?");
int matrixIndex = scanner.nextInt();
long l = matrixForFib(matrixIndex);
System.out.println("第"+ matrixIndex + "位的斐波那契数为" + l);
break;
case 7:
return;
default:
System.out.println("请输入正确的选项");
break;
}
}
}
}
1.矩阵求解斐波那契数列
1次平方阶
1
2.斐波那契数列原则
3.继续推演
2次平方阶
1.
2.
结论:
3次平方阶
4次平方阶
n次平方阶
1.
结论:
最终结论:
=
=
C语言代码:
//通过矩阵相乘来计算
long matx(int n)//利用矩阵相乘方法计算第n个斐波那契数 n-->索引值+1
{
//x=f0,y=f1,t=f2-->x=fn-1,y=fn,t=fn-1(推演下一次x的位置),p-->斐波那契数列的第几个,n是最终值,p是推演具体值(对应左上角fn+1)
int x = 0, y = 1, t = 1, p = 2;
//i是起始值:i=1,从第一个斐波那契数列开始
int i;
//temp1暂存fn-1的值
//temp2暂存fn+1的值
int temp1 = 0;
int temp2 = 0;
for (i = 1; i < n; i++) {
//1.求矩阵右下角
//fn-1的值赋值给temp1
temp1 = x;
//fn-2*0+fn-1*1=fn-1
x = x * 0 + y * 1;
//2.求矩阵左下角、右上角
//1*fn-1+1*fn-2=fn
y = temp1 * 1 + y * 1;
//求矩阵左上角
//fn+1的值暂存给temp2
temp2 = t;
//fn-1*0+fn=fn+1
t = t * 0 + p * 1;
//fn-1*1+fn*1=fn+1
p = temp2 * 1 + p * 1;
}
return y;
}
此题结构如下: