Skip to content

Instantly share code, notes, and snippets.

@ericvoorhis
Last active August 13, 2022 21:52
Show Gist options
  • Save ericvoorhis/bf8e067eafae9ad759e22cf3dd6f9b1a to your computer and use it in GitHub Desktop.
Save ericvoorhis/bf8e067eafae9ad759e22cf3dd6f9b1a to your computer and use it in GitHub Desktop.
An emojicode (https://github.com/emojicode/emojicode) program for computing the greatest common divisor of two given integers
๐Ÿ ๐Ÿ‡
๐Ÿ‘ด I used the following wiki page as a reference:
๐Ÿ‘ด https://simple.wikipedia.org/wiki/Euclidean_algorithm
๐Ÿ‘ด The following callable (What emojicode calls functions) named gcd
๐Ÿ‘ด takes two integers a and b (each of type ๐Ÿš‚ or better known as "int")
๐Ÿ‘ด and returns (โžก๏ธ) the greatest common divisor between them.
๐Ÿฆ gcd ๐Ÿ‡ a๐Ÿš‚ b๐Ÿš‚ โžก๏ธ ๐Ÿš‚
๐Ÿฎ m a
๐Ÿฎ n b
๐Ÿ‘ด Swap a and b if b is larger
๐ŸŠ โ–ถ๏ธ b a ๐Ÿ‡
๐Ÿฎ m b
๐Ÿฎ n a
๐Ÿ‰
๐Ÿ‘ด While n > 0, calculate remainder and shift numbers
๐Ÿ‘ด over as done in the interations of Euclid's algorithm.
๐Ÿ” โ–ถ๏ธ n 0 ๐Ÿ‡
๐Ÿฆ r ๐Ÿšฎ m n
๐Ÿฎ m n
๐Ÿฎ n r
๐Ÿ‰
๐ŸŽ m
๐Ÿ‰
๐Ÿ‘ด Call the gcd callable that we defined above using ๐Ÿญ
๐Ÿ‘ด with a few examples.
๐Ÿฆ result1 ๐Ÿญ gcd 675 40
๐Ÿฆ result2 ๐Ÿญ gcd 40 675
๐Ÿฆ result3 ๐Ÿญ gcd 13 13 ๐Ÿ‘ด a equals b
๐Ÿฆ result4 ๐Ÿญ gcd 37 600 ๐Ÿ‘ด first argument is prime
๐Ÿฆ result5 ๐Ÿญ gcd 20 100 ๐Ÿ‘ด first argument divides the second arg
๐Ÿฆ result6 ๐Ÿญ gcd 624129 2061517
๐Ÿ˜€ ๐Ÿช ๐Ÿ”คgcd(675, 40) = ๐Ÿ”ค ๐Ÿ”ก result1 10 ๐Ÿช ๐Ÿ‘ด outputs 5
๐Ÿ˜€ ๐Ÿช ๐Ÿ”คgcd(40, 675) = ๐Ÿ”ค ๐Ÿ”ก result2 10 ๐Ÿช ๐Ÿ‘ด outputs 5
๐Ÿ˜€ ๐Ÿช ๐Ÿ”คgcd(13, 13) = ๐Ÿ”ค ๐Ÿ”ก result3 10 ๐Ÿช ๐Ÿ‘ด outputs 13
๐Ÿ˜€ ๐Ÿช ๐Ÿ”คgcd(37, 600) = ๐Ÿ”ค ๐Ÿ”ก result4 10 ๐Ÿช ๐Ÿ‘ด outputs 1
๐Ÿ˜€ ๐Ÿช ๐Ÿ”คgcd(20, 100) = ๐Ÿ”ค ๐Ÿ”ก result5 10 ๐Ÿช ๐Ÿ‘ด outputs 20
๐Ÿ˜€ ๐Ÿช ๐Ÿ”คgcd(624129, 2061517) = ๐Ÿ”ค ๐Ÿ”ก result6 10 ๐Ÿช ๐Ÿ‘ด outputs 18913
๐Ÿ‰
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment