[英]Lost in Proof for Recursive function
Discrete math is fun but I still have a lot of difficulty with the algebra involved. 离散数学很有趣,但是我涉及的代数仍然有很多困难。 I am attempting to prove, through induction, a recursive function. 我试图通过归纳证明递归函数。 I am just starting my course in algorithm design and the assignment was to rewrite an iterative function into a recursive function and then prove it. 我刚刚开始算法设计课程,其任务是将迭代函数重写为递归函数,然后对其进行证明。 I was able to implement the recursive function and I was able to test it using a brute force technique, but I am at a loss as to how to set up my proof. 我能够实现递归函数,并且能够使用蛮力技术对其进行测试,但是我对如何设置我的证明不知所措。 I don't think I am starting it correctly. 我认为我没有正确启动它。 I included my start with the proof. 我从证明开始。 Thanks for any pointers you can give me. 感谢您的指导,您可以给我。
edit #3 final proof completed thanks to help 编辑#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
edit #2 This is my proof so far and I am sure I am way off. 编辑#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.
Below is my code: 下面是我的代码:
/*
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
*/
A recursive function typically has a form something like: 递归函数通常具有类似以下形式的形式:
recursive(param) {
if (base_case)
return base_value;
new_param = move_param_toward_base(param);
return combine(present_value, recursive(new_param);
}
An inductive proof basically has two steps: 归纳证明基本上包括两个步骤:
With a recursive function: 具有递归功能:
But, there are also some differences, including the one you seem to be running into here. 但是,也存在一些差异,包括您似乎正在遇到的差异。 In particular, in mathematics, a non-modular number can grow without limit--but on a computer, all numbers are modular; 特别是在数学中,非模数可以无限制地增长-但是在计算机上,所有数字都是模数; none of them can grow without limit. 他们都无法无限发展。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.