## Euclidean algorithm pseudocode conversion to Swift?

I have been working on a function for reducing fractions in Swift, and came across the Euclidean algorithm for finding the greatest common factor (http://en.wikipedia.org/wiki/Euclidean_algorithm)

I converted the pseudo code into swift, but yet I am confused how this is going to give me the greatest common factor if it is returning `a` which I thought was supposed to be the numerator of the fraction. Any help on this would be greatly appreciated. Thanks!

Pseudocode:

```function gcd(a, b)
while b ≠ 0
t := b
b := a mod b
a := t
return a
```

Swift:

```var a = 2
var b = 4

func gcd(a: Int, b: Int) -> Int {
var t = 0
while b != 0 {
t = b
let b = a % b
let a = t
}
return a
}

println("\(a)/\(b)")
```

Console output: 2/4

When you do this

```let b = a % b
```

you are creating another readonly variable `b`, which has nothing to do with the variable `b` from the outside scope. You need to remove both `let`s inside the loop, and make parameters modifiable by declaring them with `var`, like this:

```func gcd(var a: Int, var b: Int) -> Int {
var t = 0
while b != 0 {
t = b
b = a % b
a = t
}
return a
}
```

You can call your function like this:

```let a = 111
let b = 259
println("a=\(a), b=\(b), gcd=\(gcd(a,b))")
```

This prints `a=111, b=259, gcd=37`

ios - Euclidean algorithm pseudocode conversion to Swift?, Pseudocode: function gcd(a, b) while b ≠ 0 t := b b := a mod b a := t return a. Swift: var a = 2 var b = 4 func gcd(a: Int, b: Int) -> Int { var t = 0 while b != 0 { t = b let b  algorithm,graph,pseudocode,tarjans-algorithm. 1) Your first question: It can easily been done in O(1) , just maintain a boolean array inStack, the moment node n is put in the stack, flag inStack[n] to true. When you pop it off the stack, flag it back to false. 2) Not much different between w.index and

Taking @dasblinkenlight's answer and getting rid of `t` by using tuples for parallel assignment yields:

Swift 2.1:

```func gcd(var a: Int, var _ b: Int) -> Int {
while b != 0 {
(a, b) = (b, a % b)
}
return a
}

gcd(1001, 39)  // 13
```

`var` parameters are deprecated in Swift 2.2 and will be removed in Swift 3. So now it becomes necessary to declare `a` and `b` as `var` within the function:

```func gcd(a: Int, _ b: Int) -> Int {
var (a, b) = (a, b)
while b != 0 {
(a, b) = (b, a % b)
}
return a
}
```

Euclidean algorithm pseudocode conversion to Swift?, Any help on this would be greatly appreciated. Thanks! Pseudocode: function gcd(a, b) while b ≠ 0 t := b b := a mod b a := t return a Swift: var a = 2 var b = 4 func  The Euclidean algorithm is an algorithm. It can be used to find the biggest number that divides two other numbers (the greatest common divisor of two numbers). It can be used to find the biggest number that divides two other numbers (the greatest common divisor of two numbers).

Swift 3 version of answer given by Christopher Larsen

```func gcd(a: Int, b: Int) -> Int {
if b == 0 { return a }
return gcd(a: b, b: a % b)
}
```

ios - Euclidean algorithm pseudocode conversion to Swift? -, i have been working on function reducing fractions in swift, , came across euclidean algorithm finding greatest common factor  As is indeed a number of the form − for some , our algorithm is justified. The point of the algorithm is to continue this procedure until one number is 0, because g c d ( x , 0 ) = a b s ( x ) {\displaystyle gcd(x,0)=abs(x)} , which we can then return as our answer.

Can use a recursive method that just keeps calling itself until the GCD is found.

```func gcd(a: Int, b: Int) -> Int {
if b == 0 {
return a
}

let remainder: Int = a % b

return gcd(b, b: remainder)
}
```

and use like so

```let gcdOfSomeNums = gcd(28851538, b: 1183019)
• Thank you, this helped! I was caught up at first because I needed to add the keyword `var` before `a:` and `b:` so it doesn't default to `let` variables.
• @dasblinkenlight, I used a tuple to get rid of `t` in your answer.