Trusted answers to developer questions

Harris Amjad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The Ruby ** gcd() function** returns the GCD of two integers.

The

GCDorGreatest Common Divisorof two numbers, i.e.,`num_1`

and`num_2`

, is the greatest positive number that divides both the`num_1`

and the`num_2`

.

Figure 1, below, shows the visual representation of the `gcd()`

function.

```
num_1.gcd(num_2)
# where num_1 and num_2 are the integers whose gcd is to be calculated
```

If

`num_1`

or`num_2`

or both arenon-integers, then this function throws an`error`

.

This function requires another integer, `(num_2)`

, as a parameter.

This function returns the GCD of two integers.

The following example shows how to use the `gcd()`

function in Ruby.

#both positive numbersprint "(36).gcd(60) : ",(36).gcd(60) , "\n"#both negative numbersprint "(-24).gcd(-16) : ",(-24).gcd(-16) , "\n"#one of the numbers is zeroprint "(0).gcd(12) : ",(0).gcd(12), "\n"#one is negative and another is positiveprint "(-9).gcd(18) : ",(-9).gcd(18) , "\n"

RELATED TAGS

ruby

math

gcd

communitycreator

CONTRIBUTOR

Harris Amjad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring

Related Courses