迷失在递归函数的证明中
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);
}
一个归纳证明基本上有两个步骤:
- 证明一些(通常是微不足道的)基本情况。
- 证明一些扩展它的方法,这样如果基本情况是正确的,你的扩展版本对于一些更大的输入集仍然是正确的。
- 证明扩展可以或多或少地任意应用,因此结果对所有输入都成立。
带递归函数:
- 您表明您已正确检测和处理基本情况。
- 你表明你对其他值的扩展是正确的。
- 您表明您修改参数的方式将在有限的步骤中得出基本情况。
但是,也有一些差异,包括您似乎 运行 进入这里的那个。特别是在数学中,非模数可以无限增长——但在计算机上,所有数字都是模数; none 其中可以无限增长。
离散数学很有趣,但我在涉及的代数方面仍然有很多困难。我试图通过归纳来证明递归函数。我刚刚开始我的算法设计课程,作业是将迭代函数重写为递归函数,然后证明它。我能够实现递归函数,并且能够使用强力技术对其进行测试,但我不知道如何设置我的证明。我认为我没有正确启动它。我从证明开始。感谢您给我的任何指示。
编辑 #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);
}
一个归纳证明基本上有两个步骤:
- 证明一些(通常是微不足道的)基本情况。
- 证明一些扩展它的方法,这样如果基本情况是正确的,你的扩展版本对于一些更大的输入集仍然是正确的。
- 证明扩展可以或多或少地任意应用,因此结果对所有输入都成立。
带递归函数:
- 您表明您已正确检测和处理基本情况。
- 你表明你对其他值的扩展是正确的。
- 您表明您修改参数的方式将在有限的步骤中得出基本情况。
但是,也有一些差异,包括您似乎 运行 进入这里的那个。特别是在数学中,非模数可以无限增长——但在计算机上,所有数字都是模数; none 其中可以无限增长。