簡體   English   中英

遞歸功能證明丟失

[英]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);
}

歸納證明基本上包括兩個步驟:

  1. 證明一些(通常是微不足道的)基本情況。
  2. 證明擴展它的方法,以便在基本情況為真的情況下,擴展版本對於某些較大的輸入集也保持正確。
  3. 證明該擴展名可以或多或少地任意應用,因此結果對於所有輸入均保持正確。

具有遞歸功能:

  1. 您可以證明已正確檢測並處理了基本情況。
  2. 您顯示對其他一些值的擴展名是正確的。
  3. 您表明您正在以某種方式修改參數,該方式將導致在有限數量的步驟中產生基本情況。

但是,也存在一些差異,包括您似乎正在遇到的差異。 特別是在數學中,非模數可以無限制地增長-但是在計算機上,所有數字都是模數; 他們都無法無限發展。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM