Giter VIP home page Giter VIP logo

formalized-euclid-algorithm-proof's Introduction

formalized Euclid Algorithm proof

Using the Coq library Z to prove the Euclid GCD Algorithm.

The following codes are the algorithm's implementation in C++.

int gcd(int a, int b) {
    if(b == 0) return a;
    else return gcd(b, a % b);
}

We use Coq to formalize the algorithm. (including negative numbers)

  • In definition "Euclid", we define the algorithm in Coq.

  • In "Euclid_divide", we proof that the return value must be a divisor of both a and b.

  • In "Euclid_greatest", we proof that the return value must be the greatest divisor of a and b.

By combining the proofs above, we can say that the correctness of Euclid GCD Algorithm has been proved.

By the way, we also prove that the algorithm's time complexity is O(log n) .

  • We define "step" as the iteration steps of the algorithm, the rest of the definition is the same as definition "Euclid". We call the new definition "Euclid_step".

  • In "Euclid_count_log", we proof the following formula. $$ 2^{\frac{step - 1}{2}} \leq |x| + |y| $$ where x and y are arguments and step is defined above.

    We believed the theorem can show the time complexity of the algorithm is O(log (x + y)), which is O (log n).

    • In the proof of the theorem, we prove some useful lemmas such as "Euclid_half_descending", and we use the trick of 2-step induction in the proof of "Euclid_count_log_strong".
    • We can easily get the lemma "Euclid_count_log_ge" after proving "Euclid_count_log_strong", but we need to prove that the conclusion still holds if |x| < |y|, so we prove lemma "Euclid_count_log_lt_step" to show that we only need one step to become the situation where |x| >= |y|, then we can use "Euclid_count_log_ge" to prove easily.

Last Update in Oct. 26th, 2019.

By Tony Fang (Galaxies), All rights reserved.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.