Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc00c689

Browse files
vamshi9trekhleb
authored andcommitted
Update euclideanAlgorithm.js (trekhleb#207)
Method 2 is easy to evaluate or understand without using recursion stack!
1 parentc4458e9 commitc00c689

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

‎src/algorithms/math/euclidean-algorithm/euclideanAlgorithm.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,23 @@
33
*@param {number} originalB
44
*@return {number}
55
*/
6+
7+
/*Method 1: A bit Complex to understand*/
68
exportdefaultfunctioneuclideanAlgorithm(originalA,originalB){
79
consta=Math.abs(originalA);
810
constb=Math.abs(originalB);
911

1012
return(b===0) ?a :euclideanAlgorithm(b,a%b);
1113
}
14+
15+
/*Method 2: Easy to evaluate*/
16+
exportdefaultfunctioneuclideanAlgorithm2(originalA,originalB){
17+
consta=Math.abs(originalA);
18+
constb=Math.abs(originalB);
19+
20+
while(a!=b){
21+
[a,b]=a>b :[a-b,b] :[a,b-a]
22+
}
23+
24+
returna||b;
25+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp