Platform
Resources
Pricing
Sign in
Get started
David Hall
Workspace
Fork
Published
By
David Hall
Edited
Jul 24, 2019
Importers
Insert cell
Insert cell
Insert cell
// this is the main entry point
function
gcd
(
a
,
b
)
{
// ensure that neither parameter is negative
a
=
abs
(
a
)
;
b
=
abs
(
b
)
;
return
(
a
<
gcdCacheSize
&&
b
<
gcdCacheSize
)
?
cachedGcd
(
a
,
b
)
:
calculatedGcd
(
a
,
b
)
;
}
;
Insert cell
function
bigGcd
(
a
,
b
)
{
return
BigInt
(
gcd
(
a
,
b
)
)
;
}
Insert cell
function
cachedGcd
(
a
,
b
)
{
// requires that neither parameter is negative
// and that both are less than gcdCacheSize
return
gcdCache
[
a
]
[
b
]
;
}
;
Insert cell
function
calculatedGcd
(
a
,
b
)
{
// requires that neither parameter is negative
// uncomment the algorithm of your choice to do the calculation
return
gcdEuclidean
(
a
,
b
)
;
// return gcdRecursive(a, b);
// return gcdApacheCommons(a, b);
// return gcdGoogleCommons(a, b);
}
;
Insert cell
Insert cell
function
gcdEuclidean
(
a
,
b
)
{
// requires that neither parameter is negative
while
(
!
isZero
(
b
)
)
{
[
a
,
b
]
=
[
b
,
a
%
b
]
;
}
return
a
;
}
;
Insert cell
function
gcdRecursive
(
a
,
b
)
{
return
isZero
(
b
)
?
a
:
gcdRecursive
(
b
,
a
%
b
)
;
}
;
Insert cell
Insert cell
function
gcdApacheCommons
(
a
,
b
)
{
return
"TODO:"
;
}
;
Insert cell
Insert cell
function
gcdGoogleCommons
(
a
,
b
)
{
return
"TODO:"
;
}
;
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
test
(
true
,
isZero
(
0
)
)
;
Insert cell
test
(
true
,
cachedGcd
(
gcdCacheSize
-
1
,
gcdCacheSize
-
1
)
==
gcdCacheSize
-
1
)
;
Insert cell
test
(
1
,
gcd
(
gcdCacheSize
+
4
,
gcdCacheSize
+
6
)
)
;
Insert cell
test
(
7
,
gcd
(
-
7
,
42
)
)
;
Insert cell
test
(
BigInt
(
gcdCacheSize
)
,
gcd
(
BigInt
(
gcdCacheSize
*
3
)
,
BigInt
(
gcdCacheSize
*
5
)
)
)
;
Insert cell
test
(
(
gcdCacheSize
+
1
)
/
4
,
gcd
(
BigInt
(
(
gcdCacheSize
+
1
)
/
2
)
,
BigInt
(
(
gcdCacheSize
+
1
)
/
4
)
)
)
;
Insert cell
test
(
3n
,
bigGcd
(
6
,
3
)
)
;
Insert cell
Insert cell
Insert cell
Purpose-built for displays of data
Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Try it for free
Learn more
Fork
View
Export
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
gcd
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
bigGcd
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
cachedGcd
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
calculatedGcd
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
gcdEuclidean
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
gcdRecursive
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
gcdApacheCommons
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
gcdGoogleCommons
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
gcdCacheSize
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
gcdCache
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML