Greatest Common Divisor and Least Common Multiplexer | Hello World



Greatest Common Divisor and Least Common Multiplexer | Hello World

This is a very interesting problem, lets say we have m and n two (positive) integers, how to get the greatest Common Divisor and Least Common Multiplexer?

Lets start from Greatest Common Divisor(gcd) first. To find the gcd, we need to find all the prime factors for m and n, then multiply all the common part, then we get the gcd. Besides this elementary methods, we could use the Euclid algorithm

The Euclidean algorithm is a way to find the greatest common divisor of two positive integers, a and b. First let me show the computations for a=210 and b=45.

Divide 210 by 45, and get the result 4 with remainder 30, so 210=4·45+30.
Divide 45 by 30, and get the result 1 with remainder 15, so 45=1·30+15.
Divide 30 by 15, and get the result 2 with remainder 0, so 30=2·15+0.
The greatest common divisor of 210 and 45 is 15.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Test {
    public static int getGCD(int a, int b) {
        if (a == 0 || b == 0)
            return 0;
        if (a == b)
            return a;
        int l = Math.max(a, b); // a is the larger one
        int s = Math.min(a, b); // b is the smaller one
        if (l % s == 0)
            return s;
        return getGCD(l%s, s);
    }
     
    public static void main(String [] args) {
        int res = getGCD(42, 56);
    }
}

In plain English: the GCD of the two numbers large and small can be recursively calculated as large%small and the small. The base case ends when large%small == 0, in which case the small will be the GCD.

Formal description of the Euclidean algorithm
Input Two positive integers, a and b.
Output The greatest common divisor, g, of a and b.
Internal computation
If a<b, exchange a and b.
Divide a by b and get the remainder, r. If r=0, report b as the GCD of a and b.
Replace a by b and replace b by r. Return to the previous step.

Why does the algorithm stop?
At each step, the remainder, r, decreases by at least 1. Therefore r must eventually be 0. A formal proof would use mathematical induction


Read full article from Greatest Common Divisor and Least Common Multiplexer | Hello World


No comments:

Post a Comment

Labels

Algorithm (219) Lucene (130) LeetCode (97) Database (36) Data Structure (33) text mining (28) Solr (27) java (27) Mathematical Algorithm (26) Difficult Algorithm (25) Logic Thinking (23) Puzzles (23) Bit Algorithms (22) Math (21) List (20) Dynamic Programming (19) Linux (19) Tree (18) Machine Learning (15) EPI (11) Queue (11) Smart Algorithm (11) Operating System (9) Java Basic (8) Recursive Algorithm (8) Stack (8) Eclipse (7) Scala (7) Tika (7) J2EE (6) Monitoring (6) Trie (6) Concurrency (5) Geometry Algorithm (5) Greedy Algorithm (5) Mahout (5) MySQL (5) xpost (5) C (4) Interview (4) Vi (4) regular expression (4) to-do (4) C++ (3) Chrome (3) Divide and Conquer (3) Graph Algorithm (3) Permutation (3) Powershell (3) Random (3) Segment Tree (3) UIMA (3) Union-Find (3) Video (3) Virtualization (3) Windows (3) XML (3) Advanced Data Structure (2) Android (2) Bash (2) Classic Algorithm (2) Debugging (2) Design Pattern (2) Google (2) Hadoop (2) Java Collections (2) Markov Chains (2) Probabilities (2) Shell (2) Site (2) Web Development (2) Workplace (2) angularjs (2) .Net (1) Amazon Interview (1) Android Studio (1) Array (1) Boilerpipe (1) Book Notes (1) ChromeOS (1) Chromebook (1) Codility (1) Desgin (1) Design (1) Divide and Conqure (1) GAE (1) Google Interview (1) Great Stuff (1) Hash (1) High Tech Companies (1) Improving (1) LifeTips (1) Maven (1) Network (1) Performance (1) Programming (1) Resources (1) Sampling (1) Sed (1) Smart Thinking (1) Sort (1) Spark (1) Stanford NLP (1) System Design (1) Trove (1) VIP (1) tools (1)

Popular Posts