失去了递归功能的证明
Lost in Proof for Recursive function
离散数学很有趣,但是我仍然很难涉及的代数。我试图通过归纳来证明递归功能。我刚刚开始使用算法设计课程,任务是将迭代功能重写为递归功能,然后证明它。我能够实施递归功能,并且能够使用蛮力技术来对其进行测试,但是我对如何设置证明感到不知所措。我认为我没有正确地开始。我开始了证明。感谢您的任何指示,您可以给我。
编辑#3最终证明完成了,多亏了帮助
f (k + 1) – f(k) =
(k + 1) ^2 – ½ (k + 1) (k + 1 – 1) – k^2 – ½ (k (k -1)) =
k^2 + 2k + 1 – ½ (k^2 – k) – k^2 + ½ (k^2 - k) =
2k + 1 - k =
k + 1
编辑#2这是我到目前为止的证明,我确定我会离开。
Base Case, n = 1
When n is 1, 1 is returned Line 1
1^2-(1*(1-1))/2 = 1
Inductive Case, n > 1
Assume for k = n-1, show for n = k
triangular_recursive(k) =
triangular_recursive (k -1) + k = Line 1
(k – 1) ^2 – ½(k-1) (k-1-1) + k = Inductive Assumption
k^2 -2k +1 – ½ (k^2 -3k +2) + k =
k^2 – k + 1 – ½ (k^2 -3k + 2)
This doesn’t see, correct at all.
以下是我的代码:
/*
JLawley
proof_of_correctness1.cpp
This provides a brute force proof of my algorithm
Originally, everything was integer type.
I changed to double when I added pow.
*/
#include "stdafx.h"
#include <iostream>
// this is the original function
// we were to rewrite this as a recursive function
// so the proof would be simpler
double triangular(double n) {
auto result = 0;
for (auto i = 1; i <= n; i++) result += i;
return result;
}
/*
* This is my recursive implementation
* It includes base case and post case
*/
// n > 0
double triangular_recursive(double n) {
return (n == 1) ? n : triangular_recursive(n - 1) + n;
}
// returns n^2 - (n(n-1)) / 2
// utility method to test my theory by brute force
double test_my_theory(double n)
{
return pow(n, 2) - (n * (n - 1))/2;
}
int main(void)
{
// at values much beyond 4000, this loop fails
// edit added - the failure is due to values too large
// the program crashes when this occurs
// this is a drawback of using recursive functions
for (auto i = 1; i <= 4000; i++)
if (test_my_theory(i) != triangular_recursive(i) || test_my_theory(i) != triangular(i))
std::cout << "n!= at i = " << i;
// I am not getting any "i ="'s so I assume a good brute force test
return 0;
}
/*
* My proof so far:
Base Case, n = 1
When n is 1, 1 is returned Line 1
1^2-(1*(1-1))/2 = 1
Inductive Case, n > 1
Assume for k = n-1, show for n = k
triangular_recursive(k) =
triangular_recursive (k -1) + k = Line 1
(k – 1) ^2 – ½(k-1)(k-1-1) + k = Inductive Assumption
*/
递归函数通常具有类似的形式:
recursive(param) {
if (base_case)
return base_value;
new_param = move_param_toward_base(param);
return combine(present_value, recursive(new_param);
}
归纳证明基本上有两个步骤:
- 证明了一些(通常是微不足道的)基本案例。
- 证明了某种扩展它的方法,因此,如果基本情况为真,则您的扩展版本对于某些较大的输入集仍然是正确的。
- 证明可以或多或少地使用扩展名,因此所有输入的结果仍然是正确的。
具有递归功能:
- 您证明您已经正确检测并处理了基本案例。
- 您表明您对某些其他值的扩展是正确的。
- 您表明您正在以有限数量的步骤来修改参数。
但是,还有一些差异,包括您似乎正在遇到的差异。特别是,在数学中,非模块数可以在没有限制的情况下增长 - 但在计算机上,所有数字都是模块化的。它们都无法生长而没有限制。
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 多态性和功能结合
- 带内存和隔离功能的SQLite
- 在CMakeLists.txt的安装功能中使用.cmake文件有什么用
- 类模板的成员功能的定义在单独的TU中完全专业化
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何在C++中获得"静态纯虚拟"功能?
- 两个文件使用彼此的功能-如何解决
- 我应该实现右值推送功能吗?我应该使用std::move吗
- QML按钮点击功能执行顺序
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 有没有可能有一个只有ADL才能找到的非好友功能
- 功能样式转换从 'int' 到 'ItemType' 的匹配转换
- 文件系统:复制功能的速度秘诀是什么
- 在用于格式4的arm模拟器中实现功能时的一个问题
- 如何在Directwrite中获得给定字体的可用OpenType功能
- 对可变参数使用声明.如何选择正确的功能
- 询问在设计我的手臂模拟器功能表示格式1
- 功能原型的目的
- 这里在 Linux 中具有"CreatePipe"和"CreateProcessW"功能吗?