-
-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #170 from JanluOfficial/main
[Snippet] Added code snippet for Greatest Common Divisor in JS
- Loading branch information
Showing
2 changed files
with
44 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
snippets/javascript/mathematical-functions/greatest-common-divisor.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
title: Greatest Common Divisor | ||
description: Calculates the largest positive integer that divides each of the integers without leaving a remainder. Useful for calculating aspect ratios. | ||
author: JanluOfficial | ||
tags: math,division | ||
--- | ||
|
||
```js | ||
function gcd(a, b) { | ||
while (b !== 0) { | ||
let temp = b; | ||
b = a % b; | ||
a = temp; | ||
} | ||
return a; | ||
} | ||
|
||
// Usage: | ||
gcd(1920, 1080); // Returns: 120 | ||
gcd(1920, 1200); // Returns: 240 | ||
gcd(5,12); // Returns: 1 | ||
``` |