The Euclidean Algorithm implemented in Rust's type system

I was playing around with GATs today and decided to challenge myself to implement the Euclidean Algorithm using only the type system. Also, to make it even harder, I used GATs wherever possible, including for implementing comparisons, addition, squaring, and multiplication on the natural numbers.

GitHub gist

Playground link

Surprisingly the most difficult part was implementing the absolute difference operation (|x-y|). I had to create a "-1"-ish type which I'm not thrilled about :/

I tried to provide at least a little documentation of my thought process, but some of the implementation (mostly floor division) is a bit messy.

Let me know what you think, this is my first time posting here and my first finished Rust project that I'm really proud of!

πŸ‘︎ 137
πŸ’¬︎
πŸ‘€︎ u/Steelbirdy
πŸ“…︎ Nov 07 2021
🚨︎ report
Question about the Extended Euclidean Algorithm

So about the Extended Euclidean algorithm, the algorithm that finds, a, b, and d in the equation am+bn=d, where m and n are two positive integers, d is their GCD, and a and b are two integers (not necessarily positive)

So the book I am following has the following instructions for implementing this algorithm:

E1. [Initialize.] Set aβ€² ← b ← 1, a ← bβ€² ← 0, c ← m, d ← n.

E2. [Divide.] Let q and r be the quotient and remainder, respectively, of c divided by d. (We have c = qd + r and 0 ≀ r < d.)

E3. [Remainder zero?] If r = 0, the algorithm terminates; we have in this case am + bn = d as desired.

E4. [Recycle.] Set c ← d, d ← r, t ← aβ€², aβ€² ← a, a ← t βˆ’ qa, t ← bβ€², bβ€² ← b, b ← t βˆ’ qb, and go back to E2.

My problem is that I don't particularly understand all the math around this algorithm, I understand the normal Euclidean algorithm well enough, so I understand half of the extended one as well. What I don't understand is the need for a' and b' as well t (I get that t is a temporary variable but I don't get the a = t - qa and b = t - qb part). That as well as the initialization of the variables a, a', b, and b' as well as the swapping of the values between them on each iteration. Can anybody help me bridge these gaps in my understanding of this algorithm?

πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/prithvidiamond1
πŸ“…︎ Nov 22 2021
🚨︎ report
I made a video on the Euclidean Algorithm for SOME1. Here is an animation (link to video in the comments) v.redd.it/yxefzn75zii71
πŸ‘︎ 104
πŸ’¬︎
πŸ‘€︎ u/NumberSense4
πŸ“…︎ Aug 20 2021
🚨︎ report
I made a video on the Euclidean Algorithm for SOME1. Here is an animation (link to video in the comments) v.redd.it/yxefzn75zii71
πŸ‘︎ 21
πŸ’¬︎
πŸ‘€︎ u/NumberSense4
πŸ“…︎ Aug 20 2021
🚨︎ report
I made a visualization of the Euclidean algorithm in my first week with manim v.redd.it/r56t9lju5wu61
πŸ‘︎ 88
πŸ’¬︎
πŸ‘€︎ u/NumberSense4
πŸ“…︎ Apr 23 2021
🚨︎ report
I made a visualization of the Euclidean algorithm in my first week with manim v.redd.it/r56t9lju5wu61
πŸ‘︎ 200
πŸ’¬︎
πŸ‘€︎ u/NumberSense4
πŸ“…︎ Apr 23 2021
🚨︎ report
Programming the Euclidean Algorithm on the Casio fx-991EX

As the title says, a program to run the Euclidean algorithm on the fx-991EX: https://imgur.com/a/L3MC2dD

It's a little technical, a little long ( ... ), but for a first ever ( because I can say with some confidence that no one sufficiently versed with calculator logic programs on a non-programmable calculator ) attempt I made in a physics lesson I don't think it's so bad ( there are a few bits here and there I'm able to condense, but for reasons of convenience I don't think I will ).

Now, as for actually running the 'program':

We start with the initialisation of the variables:

  • Set A to be the larger of the two values, B to be the smaller, and everything else as 0.

  • Make sure the calculator is operating with radians as the angle measure, degrees will not work with what I've done here. ( If you're unsure of how this is changed, just type in [ SHIFT, SETUP, 2, 2 ] ) ).

  • Next, input: [ A . STO x ]

  • If entered correctly, this should trigger a syntax error ( all intended, don't worry! ).

( note: it's important that the decimal point is not deleted until after the program is entered in full, so keep it there even as we're typing in the rest )

  • Press on the D-PAD Left button three times to move to the end of the expression and continue to write the program till we're finished and it's all on screen.

  • Remove the decimal point and start the program by pressing enter. From here on you'll need to manually push equals for every step forward.

Done! There's nothing more that requires any direct input, so just keep it going and if all goes well it’ll finish with the very last value of B as the HCF of our initial inputs A & B.

If anyone has any thoughts / queries, feel free to share, I’m quite interested to see if there’re actually any meaningful applications to this. Happy calculating!

πŸ‘︎ 14
πŸ’¬︎
πŸ‘€︎ u/MLGcrumpets
πŸ“…︎ Apr 01 2021
🚨︎ report
Hi, here is a simple solved example of RSA algorithm.The value of d is found without using Extended Euclidean algorithm. youtu.be/aJ9HAdiAnIU
πŸ‘︎ 15
πŸ’¬︎
πŸ‘€︎ u/Trinity_software
πŸ“…︎ Oct 26 2020
🚨︎ report
Fibonacci Numbers and Euclidean Algorithm

n >= 3.
The greatest common divisor is:

(F_n, F_n+1).

so be it:

aF_n + bF_{n+1}.

What general assumption could you make about a and b? Prove it.

This is our task and we are completly lost about how to deal with that.

πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/FallenButNotLost
πŸ“…︎ Dec 15 2020
🚨︎ report
[University Discrete Mathematical Structures] How to calculate the last step in an Extended Euclidean Algorithm example.

I know this is probably one of the simpler parts of the problem, but how (in this example) are they getting 5 * 675 - 16 *210 from 5 * (675 - 3 * 210) - 210? I understand all the mod calculations and plugging in steps, just that last jump is getting me.

https://preview.redd.it/89la6ngpx6u51.png?width=637&format=png&auto=webp&s=69ce6e371970ded844ec77488b9958d191fdec48

πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/jst_my_lck
πŸ“…︎ Oct 20 2020
🚨︎ report
I have an assignment to submit on Euclidean Algorithm . This is the reference i have got from the text book . I sort of figured out the first page . But the second one doesn't makes any sense. Any replies regarding an explanation will be extremely appreciated reddit.com/gallery/koqhvz
πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/SOKLOMAX
πŸ“…︎ Jan 02 2021
🚨︎ report
Gabriel LamΓ©'s Theorem: A running time analysis of the Euclidean Algorithm - First practical application of Fibonacci numbers... This is the third video, continuing from 2 introductory videos related to Fibonacci numbers. Feedback is much appreciated :-) youtu.be/VEQ30NKUZI0
πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/rondoCappuccino20
πŸ“…︎ Aug 27 2020
🚨︎ report
Just released Modulus Operandi, a Swift package for declarative modular arithmetic that supports Euclidean, flooring, and truncating division algorithms -- also with a CLI tool built by Swift Argument Parser twitter.com/cypher_poet/s…
πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/CypherPoet
πŸ“…︎ Oct 25 2020
🚨︎ report
Why and how are partial fractions, euclidean algorithm and Pell's equation all related?

While solving some of the problems below #100, I stumbled across those topics and for a long time I had no idea they were related. I superficially understand those concepts separately, but I don't get how they relate to each other. Where can I find some good material about them? I have a little knowledge on number theory, but as you can see I'm not really good at it. I'm hoping PE helps me improve.

πŸ‘︎ 11
πŸ’¬︎
πŸ‘€︎ u/beardoog
πŸ“…︎ Apr 19 2020
🚨︎ report
Just released Modulus Operandi, a Swift package for declarative modular arithmetic that supports Euclidean, flooring, and truncating division algorithms -- also with a CLI tool built by Swift Argument Parser twitter.com/cypher_poet/s…
πŸ‘︎ 6
πŸ’¬︎
πŸ‘€︎ u/CypherPoet
πŸ“…︎ Oct 25 2020
🚨︎ report
Euclidean algorithm help!!

Anyone able to help with these questions? Thank you so much in advance!!

  1. Find gcd (2 ^80 - 1), (2 ^ 50 - 1)
  2. show that consecutive Fibonacci numbers are relatively prime
πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/katebrackett
πŸ“…︎ Oct 01 2020
🚨︎ report
[High School Math, Euclidean Algorithm]: For which n is the value of the grestest common divisor maximal?

So, again: For which n (n e N) is the value of the grestest common divisor maximal?

The numbers given are for example 13n-7 and 2n+1.

I gave it a go by using the Euclidean algorithm and this is how it went:

https://ibb.co/T0P8KLR

The red text is the right answer and as you can see my answers are almost right but not quite (the first one is correct but I did the same thing with the other examples??). So please, if you have an idea of what I’m doing wrong, it would be very very appreciated!

Also, I am not sure how this is the answer to the question β€œmake the common divisor maximal”, other than the fact that we are finding a greatest common divisor that is bigger than 1. I mean, isn’t the value of the gcd supposed to be maximal anyway?

Thank you!

πŸ‘︎ 6
πŸ’¬︎
πŸ‘€︎ u/Honeeeeeeey
πŸ“…︎ Jan 31 2020
🚨︎ report
[Number Theory] Can someone help me with the extended euclidean algorithm and bezout identity s and t values?

Found GCD(561, 442) to be 17

561 = 1*442 +119

412 = 3*119 +85

119 = 1*85 + 34

85 = 2*34 + 17

34 = 2*17 + 0

Rewritten

561 - 1*442 =119

412 - 3*119 =85

119 - 1*85 = 34

85 -2*34 = 17

34 -2*17 = 0

Then what do I do? I think I need to substitute somehow, but I cannot figure that out

πŸ‘︎ 2
πŸ’¬︎
πŸ“…︎ Oct 07 2020
🚨︎ report
The Extended Euclidean Algorithm is Disgusting (Look Closer)
πŸ‘︎ 34
πŸ’¬︎
πŸ‘€︎ u/NoahRCarver
πŸ“…︎ Feb 26 2020
🚨︎ report
Gabriel LamΓ©'s Theorem: A running time analysis of the Euclidean Algorithm - First practical application of Fibonacci numbers... This is the third video, continuing from the 2 I had posted links to previously. Feedback is much appreciated :-) youtu.be/VEQ30NKUZI0
πŸ‘︎ 6
πŸ’¬︎
πŸ‘€︎ u/rondoCappuccino20
πŸ“…︎ Aug 27 2020
🚨︎ report
In the Euclidean algorithm, why do we recurse the way that we do?

I understand how it works, but I was wondering why it changes both numbers in the recursive step.

In the algorithm, we start with two positive integers n and m, assuming n > m. If the remainder n mod m is nonzero, then we know that the GCD (let's call it d) must evenly divide n, m, and the remainder (n mod m). This fact allows us to reduce the problem such that d = gcd(m, n mod m).

But isn't it also true that d = gcd(n, n mod m)? This would also reach the same result. Why does euclid's algorithm use the first method and not the second?

πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/BatWithTheGat
πŸ“…︎ Sep 04 2020
🚨︎ report
Congruency with the Euclidean Algorithm [University]

Here is a picture of the example I'm looking at. I tried to work through it myself.

https://imgur.com/a/aMmAHZW

I do not understand how in this example 2 is congruent to 18 is congruent to 3(mod15).

I do understand why 18 is congruent to 3(mod15), but I don't understand why also
2 is congruent to 18 is congruent to 3(mod15) as the example in my book states.

πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/cubedspace3
πŸ“…︎ Jul 25 2020
🚨︎ report
Extended Euclidean Algorithm

from "https://crypto.stanford.edu/pbc/notes/numbertheory/euclid.html"

3=33m+27n

6=33-1*27

3=27-4*6

3=27-4*(33-1*27) = (-4)*33+5*27

m = -4, n=5

How did they get from 3=27-4(33-1*27)

to

3= -4*33+5*27... Where did the +5 come from? How does that term 3=27-4(33-1*27) become -4*33+5*27

Thanks!

πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/EveningPossible5
πŸ“…︎ Mar 13 2020
🚨︎ report
Euclidean algorithm function not working

Hi, im fairly new to using python i have been doing bits of it for my course work however its come to it to write a program to get the gcd value for a euclidean algorithm i believe i've got the correct code but i keep getting an error that says Traceback (most recent call last):

File "<pyshell#0>", line 1, in <module>

euclidian(93,219)

NameError: name 'euclidian' is not defined

however my code is as follows:

from math import *

def euclidlian(x,y, verbose=True):

if x < y:

return euclidian(y, x, verbose)

print ()

while y != 0:

if verbose: print('%s = %s * %s + %s' % (x, floor(x/y),y, x % y))

(x, y) = (y,x % y)

if verbose: print('gcd is %s' % x)

return x

euclidian(93,219)

any help would be greatly appreciated thanks in advance.

πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/demon_gamer1996
πŸ“…︎ Dec 18 2019
🚨︎ report
Help with the Euclidean's extended algorithm

Hi, I feel like this must be a really silly question compared to the other questions I have seen on this subreddit.

But pretty much I'm a little stuck on how to do the Euclidean's algorithm.

I think the part I don't understand is when you simplify the groups.

How did they get from this to the end result

31 = ( 527 - 341 x 1 ) x 2 - 341

31 = 527 x 2 - 341 x 2 - 341

31 = 527 x 2 - 341 x 3

I understand how they got to the first equation by expanding but I'm a little confused about the simplification process.

also please keep in mind this is not a homework question but I just want to learn the Euclidean's algorithm the question I'm talking about can be found here.

πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/abmoob
πŸ“…︎ Mar 24 2020
🚨︎ report
Extended Euclidean Algorithm

Animating the Euclidean Algorithm and its application to computer science encryption (extended Euclidean Algorithm - BΓ©zout's identity) would be really cool. Computers stay secured by an algorithm derived 3000 years ago.

https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm

πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/osarjeant
πŸ“…︎ Jan 13 2020
🚨︎ report
Implementing a variation of the Continuous Dijkstra algorithm for shortest euclidean path finding

I’m looking to implement the Hershberger and Suri's Wave Algorithm for shortest path finding in c++, research paper is https://pdfs.semanticscholar.org/4c1f/2cc5262541db0f1047dab0f0789968f8975d.pdf. Does anyone have experience with this algorithm?

πŸ‘︎ 5
πŸ’¬︎
πŸ‘€︎ u/python_wizard
πŸ“…︎ Mar 01 2020
🚨︎ report
Why does euclidean algorithm allow us to find a^-1 for a in any ring?

Hello,

I understand how to use euclidean algorithm to find a^(-1) for situation like:

a^(-1) Γ— a = 1 mod n

because that's basically just following instruction, but i don't really get why this algorithm is applied here and why does it work.

Can anybody help me understand?

πŸ‘︎ 5
πŸ’¬︎
πŸ‘€︎ u/chaueq
πŸ“…︎ Jan 16 2020
🚨︎ report
BIO-Complexity β€œResearch Paper” #6: β€œClimbing the Steiner Treeβ€”Sources of Active Information in a Genetic Algorithm for Solving the Euclidean Steiner Tree Problem ”, in which Dembski complains about a genetic algorithm.

So here we go, β€œpaper” #6 from BIO-Complexity (pdf).

This paper is all about a genetic algorithm used to solve for a Steiner Tree. A bit of background before we get into it: A Steiner tree is a network of lines and nodes where they meet (interchanges in this paper) connecting pre-specified coordinate points via the shortest possible path. Back in 2006, the authors over at The Panda’s Thumb issued a challenge (read that, you’ll see some names familiar to r/debateevolution) that involved Steiner trees: Solve the problem with an algorithm, and also have people do math to solve it, then have creationists identify which answers where which.

Of course, none of them could tell the designed (i.e. solved by people) solutions from the evolved (i.e. generated by the algorithm), so they (Dembski specifically) responding by arguing that the information to solve the problem was β€œfront-loaded” into the algorithm.

&nbsp;

This charge takes two forms. The first is that the answer is specifically and explicitly present in the code. This was absurd on its face, so they fell back to the second form of the argument, that the structure of the code itself dictates that a specific results must occur. So even if the solution isn’t explicitly stated in the code, it is still β€œdesigned” when it is generated due to the programmer designing the code in such a way that a specific outcome is predetermined.

This paper is an attempt to elaborate on that second form of the objection, specifically with regard to Dave Thomas’ algorithm (Thomas is the author of the Panda’s Thumb piece linked above).

So let’s get into it.

&nbsp;

The specific challenge issued to Dembski et al. is to identify the specific part of code that provides the solution. In other words, specifically identify the β€œfront-loading”. The authors identify two specific pieces.

The first ensures that there are at least two interchanges in the network connecting the points. The second specifies that no mutations cause tne number of interchanges to go below two.

The claim is that these two pieces of the code contain all of the information necessary to solve the problem.

But that’s wrong, and the data in this paper show that that’s wrong, because they generate lots of different potential solutions, some better and many worse than others.

All that th

... keep reading on reddit ➑

πŸ‘︎ 7
πŸ’¬︎
πŸ‘€︎ u/DarwinZDF42
πŸ“…︎ Aug 02 2018
🚨︎ report
Is the Euclidean Algorithm a bad way to teach a student to find the GCD of two numbers?

It's what I was taught in school maybe 10 or more years ago. I'm helping my cousin, who is in 8th grade, with his schoolwork since he is out sick, some of his work is about finding the GCD, but apparently what he was taught and what his textbook says is that the way to find the GCD is to find the LCM by prime factorization and then solve with the formula GCD = a * b/LCM [a,b]. This seems backwards to me, prime factorization is a silly way to go about this at all, and I was taught that to find the LCM you'd solve a * b/GCD, so if you're going to use prime factorization you'd use it find the GCD and then use that to find the LCM. Is there any good reason why they'd choose to teach backwards, and to solve using prime factorization rather than Euclid's Algorithm?

πŸ‘︎ 2
πŸ’¬︎
πŸ“…︎ Nov 22 2019
🚨︎ report
Intuition for the correctness of the Euclidean algorithm

How can I intuitively understand the Euclidean algorithm? I’ve looked up the proof, but I feel like I am mindlessly following the steps without really understanding why it is true. Any help? And any tips to keep in mind when browsing future theorems and lemmas?

πŸ‘︎ 3
πŸ’¬︎
πŸ‘€︎ u/bovine_scatology
πŸ“…︎ Jul 06 2019
🚨︎ report
Analysis of traditional Extended Euclidean Algorithm [PART-1]
πŸ‘︎ 3
πŸ’¬︎
πŸ‘€︎ u/hiswaps
πŸ“…︎ Jan 11 2020
🚨︎ report

Please note that this site uses cookies to personalise content and adverts, to provide social media features, and to analyse web traffic. Click here for more information.