Skip to main content
Commonmark migration
Source Link

Given latitude/longitude of two points on the Moon (lat1, lon1) and (lat2, lon2), compute the distance between the two points in kilometers, by using any formula that gives the same result as the haversine formula.

##Input

Input

  • Four integer values lat1, lon1, lat2, lon2 in degree (angle) or
  • four decimal values ϕ1, λ1, ϕ2, λ2 in radians.

##Output

Output

Distance in kilometers between the two points (decimal with any precision or rounded integer).

##Haversine formula

Haversine formula

d = 2 r \arcsin\left(\sqrt{\sin^2\left(\frac{\phi_2 - \phi_1}{2}\right) + \cos(\phi_1) \cos(\phi_2)\sin^2\left(\frac{\lambda_2 - \lambda_1}{2}\right)}\right)

where

  • r is the radius of the sphere (assume that the Moon's radius is 1737 km),
  • ϕ1 latitude of point 1 in radians
  • ϕ2 latitude of point 2 in radians
  • λ1 longitude of point 1 in radians
  • λ2 longitude of point 2 in radians
  • d is the circular distance between the two points

(source: https://en.wikipedia.org/wiki/Haversine_formula)

##Other possible formulas

Other possible formulas

  • d = r * acos(sin ϕ1 sin ϕ2 + cos ϕ1 cos ϕ2 cos(λ2 - λ1)) @miles' formula.
  • d = r * acos(cos(ϕ1 - ϕ2) + cos ϕ1 cos ϕ2 (cos(λ2 - λ1) - 1)) @Neil's formula.

##Example where inputs are degrees and output as rounded integer

Example where inputs are degrees and output as rounded integer

42, 9, 50, 2  --> 284
50, 2, 42, 9  --> 284
4, -2, -2, 1  --> 203
77, 8, 77, 8  --> 0
10, 2, 88, 9  --> 2365

Rules

  • The input and output can be given in any convenient format.
  • Specify in the answer whether the inputs are in degrees or radians.
  • No need to handle invalid latitude/longitude values
  • Either a full program or a function are acceptable. If a function, you can return the output rather than printing it.
  • If possible, please include a link to an online testing environment so other people can try out your code!
  • Standard loopholes are forbidden.
  • This is so all usual golfing rules apply, and the shortest code (in bytes) wins.

Given latitude/longitude of two points on the Moon (lat1, lon1) and (lat2, lon2), compute the distance between the two points in kilometers, by using any formula that gives the same result as the haversine formula.

##Input

  • Four integer values lat1, lon1, lat2, lon2 in degree (angle) or
  • four decimal values ϕ1, λ1, ϕ2, λ2 in radians.

##Output

Distance in kilometers between the two points (decimal with any precision or rounded integer).

##Haversine formula

d = 2 r \arcsin\left(\sqrt{\sin^2\left(\frac{\phi_2 - \phi_1}{2}\right) + \cos(\phi_1) \cos(\phi_2)\sin^2\left(\frac{\lambda_2 - \lambda_1}{2}\right)}\right)

where

  • r is the radius of the sphere (assume that the Moon's radius is 1737 km),
  • ϕ1 latitude of point 1 in radians
  • ϕ2 latitude of point 2 in radians
  • λ1 longitude of point 1 in radians
  • λ2 longitude of point 2 in radians
  • d is the circular distance between the two points

(source: https://en.wikipedia.org/wiki/Haversine_formula)

##Other possible formulas

  • d = r * acos(sin ϕ1 sin ϕ2 + cos ϕ1 cos ϕ2 cos(λ2 - λ1)) @miles' formula.
  • d = r * acos(cos(ϕ1 - ϕ2) + cos ϕ1 cos ϕ2 (cos(λ2 - λ1) - 1)) @Neil's formula.

##Example where inputs are degrees and output as rounded integer

42, 9, 50, 2  --> 284
50, 2, 42, 9  --> 284
4, -2, -2, 1  --> 203
77, 8, 77, 8  --> 0
10, 2, 88, 9  --> 2365

Rules

  • The input and output can be given in any convenient format.
  • Specify in the answer whether the inputs are in degrees or radians.
  • No need to handle invalid latitude/longitude values
  • Either a full program or a function are acceptable. If a function, you can return the output rather than printing it.
  • If possible, please include a link to an online testing environment so other people can try out your code!
  • Standard loopholes are forbidden.
  • This is so all usual golfing rules apply, and the shortest code (in bytes) wins.

Given latitude/longitude of two points on the Moon (lat1, lon1) and (lat2, lon2), compute the distance between the two points in kilometers, by using any formula that gives the same result as the haversine formula.

Input

  • Four integer values lat1, lon1, lat2, lon2 in degree (angle) or
  • four decimal values ϕ1, λ1, ϕ2, λ2 in radians.

Output

Distance in kilometers between the two points (decimal with any precision or rounded integer).

Haversine formula

d = 2 r \arcsin\left(\sqrt{\sin^2\left(\frac{\phi_2 - \phi_1}{2}\right) + \cos(\phi_1) \cos(\phi_2)\sin^2\left(\frac{\lambda_2 - \lambda_1}{2}\right)}\right)

where

  • r is the radius of the sphere (assume that the Moon's radius is 1737 km),
  • ϕ1 latitude of point 1 in radians
  • ϕ2 latitude of point 2 in radians
  • λ1 longitude of point 1 in radians
  • λ2 longitude of point 2 in radians
  • d is the circular distance between the two points

(source: https://en.wikipedia.org/wiki/Haversine_formula)

Other possible formulas

  • d = r * acos(sin ϕ1 sin ϕ2 + cos ϕ1 cos ϕ2 cos(λ2 - λ1)) @miles' formula.
  • d = r * acos(cos(ϕ1 - ϕ2) + cos ϕ1 cos ϕ2 (cos(λ2 - λ1) - 1)) @Neil's formula.

Example where inputs are degrees and output as rounded integer

42, 9, 50, 2  --> 284
50, 2, 42, 9  --> 284
4, -2, -2, 1  --> 203
77, 8, 77, 8  --> 0
10, 2, 88, 9  --> 2365

Rules

  • The input and output can be given in any convenient format.
  • Specify in the answer whether the inputs are in degrees or radians.
  • No need to handle invalid latitude/longitude values
  • Either a full program or a function are acceptable. If a function, you can return the output rather than printing it.
  • If possible, please include a link to an online testing environment so other people can try out your code!
  • Standard loopholes are forbidden.
  • This is so all usual golfing rules apply, and the shortest code (in bytes) wins.
added 442 characters in body
Source Link
mdahmoune
  • 3.1k
  • 2
  • 16
  • 28

Given latitude/longitude of two points on the Moon (lat1, lon1) and (lat2, lon2), compute the distance between the two points in kilometers, by using any formula that gives the same result as the haversine formula.

##Input

  • Four integer values lat1, lon1, lat2, lon2 in degree (angle) or
  • four decimal values ϕ1, λ1, ϕ2, λ2 in radians.

##Output

Distance in kilometers between the two points (decimal with any precision or rounded integer).

##Haversine formula

d = 2 r \arcsin\left(\sqrt{\sin^2\left(\frac{\phi_2 - \phi_1}{2}\right) + \cos(\phi_1) \cos(\phi_2)\sin^2\left(\frac{\lambda_2 - \lambda_1}{2}\right)}\right)

where

  • r is the radius of the sphere (assume that the Moon's radius is 1737 km),
  • ϕ1 latitude of point 1 in radians
  • ϕ2 latitude of point 2 in radians
  • λ1 longitude of point 1 in radians
  • λ2 longitude of point 2 in radians
  • d is the circular distance between the two points

(source: https://en.wikipedia.org/wiki/Haversine_formula)

##Other possible formulas

  • d = r * acos(sin ϕ1 sin ϕ2 + cos ϕ1 cos ϕ2 cos(λ2 - λ1)) @miles' formula.
  • d = r * acos(cos(ϕ1 - ϕ2) + cos ϕ1 cos ϕ2 (cos(λ2 - λ1) - 1)) @Neil's formula.

##Example where inputs are degrees and output as rounded integer

42, 9, 50, 2  --> 284
50, 2, 42, 9  --> 284
4, -2, -2, 1  --> 203
77, 8, 77, 8  --> 0
10, 2, 88, 9  --> 2365

Rules

  • The input and output can be given in any convenient format.
  • Specify in the answer whether the inputs are in degrees or radians.
  • No need to handle invalid latitude/longitude values
  • Either a full program or a function are acceptable. If a function, you can return the output rather than printing it.
  • If possible, please include a link to an online testing environment so other people can try out your code!
  • Standard loopholes are forbidden.
  • This is so all usual golfing rules apply, and the shortest code (in bytes) wins.

Given latitude/longitude of two points on the Moon (lat1, lon1) and (lat2, lon2), compute the distance between the two points in kilometers, by using any formula that gives the same result as the haversine formula.

##Input

  • Four integer values lat1, lon1, lat2, lon2 in degree (angle) or
  • four decimal values ϕ1, λ1, ϕ2, λ2 in radians.

##Output

Distance in kilometers between the two points (decimal with any precision or rounded integer).

##Haversine formula

d = 2 r \arcsin\left(\sqrt{\sin^2\left(\frac{\phi_2 - \phi_1}{2}\right) + \cos(\phi_1) \cos(\phi_2)\sin^2\left(\frac{\lambda_2 - \lambda_1}{2}\right)}\right)

where

  • r is the radius of the sphere (assume that the Moon's radius is 1737 km),
  • ϕ1 latitude of point 1 in radians
  • ϕ2 latitude of point 2 in radians
  • λ1 longitude of point 1 in radians
  • λ2 longitude of point 2 in radians
  • d is the circular distance between the two points

(source: https://en.wikipedia.org/wiki/Haversine_formula)

##Example where inputs are degrees and output as rounded integer

42, 9, 50, 2  --> 284
50, 2, 42, 9  --> 284
4, -2, -2, 1  --> 203
77, 8, 77, 8  --> 0
10, 2, 88, 9  --> 2365

Rules

  • The input and output can be given in any convenient format.
  • Specify in the answer whether the inputs are in degrees or radians.
  • No need to handle invalid latitude/longitude values
  • Either a full program or a function are acceptable. If a function, you can return the output rather than printing it.
  • If possible, please include a link to an online testing environment so other people can try out your code!
  • Standard loopholes are forbidden.
  • This is so all usual golfing rules apply, and the shortest code (in bytes) wins.

Given latitude/longitude of two points on the Moon (lat1, lon1) and (lat2, lon2), compute the distance between the two points in kilometers, by using any formula that gives the same result as the haversine formula.

##Input

  • Four integer values lat1, lon1, lat2, lon2 in degree (angle) or
  • four decimal values ϕ1, λ1, ϕ2, λ2 in radians.

##Output

Distance in kilometers between the two points (decimal with any precision or rounded integer).

##Haversine formula

d = 2 r \arcsin\left(\sqrt{\sin^2\left(\frac{\phi_2 - \phi_1}{2}\right) + \cos(\phi_1) \cos(\phi_2)\sin^2\left(\frac{\lambda_2 - \lambda_1}{2}\right)}\right)

where

  • r is the radius of the sphere (assume that the Moon's radius is 1737 km),
  • ϕ1 latitude of point 1 in radians
  • ϕ2 latitude of point 2 in radians
  • λ1 longitude of point 1 in radians
  • λ2 longitude of point 2 in radians
  • d is the circular distance between the two points

(source: https://en.wikipedia.org/wiki/Haversine_formula)

##Other possible formulas

  • d = r * acos(sin ϕ1 sin ϕ2 + cos ϕ1 cos ϕ2 cos(λ2 - λ1)) @miles' formula.
  • d = r * acos(cos(ϕ1 - ϕ2) + cos ϕ1 cos ϕ2 (cos(λ2 - λ1) - 1)) @Neil's formula.

##Example where inputs are degrees and output as rounded integer

42, 9, 50, 2  --> 284
50, 2, 42, 9  --> 284
4, -2, -2, 1  --> 203
77, 8, 77, 8  --> 0
10, 2, 88, 9  --> 2365

Rules

  • The input and output can be given in any convenient format.
  • Specify in the answer whether the inputs are in degrees or radians.
  • No need to handle invalid latitude/longitude values
  • Either a full program or a function are acceptable. If a function, you can return the output rather than printing it.
  • If possible, please include a link to an online testing environment so other people can try out your code!
  • Standard loopholes are forbidden.
  • This is so all usual golfing rules apply, and the shortest code (in bytes) wins.
spelling fixes.
Source Link
JAD
  • 3k
  • 1
  • 12
  • 31

Given latitude/longitude of two points on the Moon (lat1, lon1) and (lat2, lon2), compute the distance between the two points in kilometrekilometers, by using any formula that gives the same result as the haversine formula.

##Input

  • Four integer values lat1, lon1, lat2, lon2 in degree (angle) or
  • four decimal values ϕ1, λ1, ϕ2, λ2 in radians.

##Output

Distance in kilometrekilometers between the two points (decimal with any precision or rounded integer).

##Haversine formula

d = 2 r \arcsin\left(\sqrt{\sin^2\left(\frac{\phi_2 - \phi_1}{2}\right) + \cos(\phi_1) \cos(\phi_2)\sin^2\left(\frac{\lambda_2 - \lambda_1}{2}\right)}\right)

where

  • r is the radius of the sphere (assume that the Moon's radius is 1737 km),
  • ϕ1 latitude of point 1 in radians
  • ϕ2 latitude of point 2 in radians
  • λ1 longitude of point 1 in radians
  • λ2 longitude of point 2 in radians
  • d is the circular distance between the two points

(source: https://en.wikipedia.org/wiki/Haversine_formula)

##Example where inputs are degrees and output as rounded integer

42, 9, 50, 2  --> 284
50, 2, 42, 9  --> 284
4, -2, -2, 1  --> 203
77, 8, 77, 8  --> 0
10, 2, 88, 9  --> 2365

Rules

  • The input and output can be given in any convenient format.
  • Specify in the answer whether the inputs are in degrees or radians.
  • No need to handle invalid latitude/longitude values
  • Either a full program or a function are acceptable. If a function, you can return the output rather than printing it.
  • If possible, please include a link to an online testing environment so other people can try out your code!
  • Standard loopholes are forbidden.
  • This is so all usual golfing rules apply, and the shortest code (in bytes) wins.

Given latitude/longitude of two points on the Moon (lat1, lon1) and (lat2, lon2), compute the distance between the two points in kilometre, by using any formula that gives the same result as the haversine formula.

##Input

  • Four integer values lat1, lon1, lat2, lon2 in degree (angle) or
  • four decimal values ϕ1, λ1, ϕ2, λ2 in radians.

##Output

Distance in kilometre between the two points (decimal with any precision or rounded integer).

##Haversine formula

d = 2 r \arcsin\left(\sqrt{\sin^2\left(\frac{\phi_2 - \phi_1}{2}\right) + \cos(\phi_1) \cos(\phi_2)\sin^2\left(\frac{\lambda_2 - \lambda_1}{2}\right)}\right)

where

  • r is the radius of the sphere (assume that the Moon's radius is 1737 km),
  • ϕ1 latitude of point 1 in radians
  • ϕ2 latitude of point 2 in radians
  • λ1 longitude of point 1 in radians
  • λ2 longitude of point 2 in radians
  • d is the circular distance between the two points

(source: https://en.wikipedia.org/wiki/Haversine_formula)

##Example where inputs are degrees and output as rounded integer

42, 9, 50, 2  --> 284
50, 2, 42, 9  --> 284
4, -2, -2, 1  --> 203
77, 8, 77, 8  --> 0
10, 2, 88, 9  --> 2365

Rules

  • The input and output can be given in any convenient format.
  • Specify in the answer whether the inputs are in degrees or radians.
  • No need to handle invalid latitude/longitude values
  • Either a full program or a function are acceptable. If a function, you can return the output rather than printing it.
  • If possible, please include a link to an online testing environment so other people can try out your code!
  • Standard loopholes are forbidden.
  • This is so all usual golfing rules apply, and the shortest code (in bytes) wins.

Given latitude/longitude of two points on the Moon (lat1, lon1) and (lat2, lon2), compute the distance between the two points in kilometers, by using any formula that gives the same result as the haversine formula.

##Input

  • Four integer values lat1, lon1, lat2, lon2 in degree (angle) or
  • four decimal values ϕ1, λ1, ϕ2, λ2 in radians.

##Output

Distance in kilometers between the two points (decimal with any precision or rounded integer).

##Haversine formula

d = 2 r \arcsin\left(\sqrt{\sin^2\left(\frac{\phi_2 - \phi_1}{2}\right) + \cos(\phi_1) \cos(\phi_2)\sin^2\left(\frac{\lambda_2 - \lambda_1}{2}\right)}\right)

where

  • r is the radius of the sphere (assume that the Moon's radius is 1737 km),
  • ϕ1 latitude of point 1 in radians
  • ϕ2 latitude of point 2 in radians
  • λ1 longitude of point 1 in radians
  • λ2 longitude of point 2 in radians
  • d is the circular distance between the two points

(source: https://en.wikipedia.org/wiki/Haversine_formula)

##Example where inputs are degrees and output as rounded integer

42, 9, 50, 2  --> 284
50, 2, 42, 9  --> 284
4, -2, -2, 1  --> 203
77, 8, 77, 8  --> 0
10, 2, 88, 9  --> 2365

Rules

  • The input and output can be given in any convenient format.
  • Specify in the answer whether the inputs are in degrees or radians.
  • No need to handle invalid latitude/longitude values
  • Either a full program or a function are acceptable. If a function, you can return the output rather than printing it.
  • If possible, please include a link to an online testing environment so other people can try out your code!
  • Standard loopholes are forbidden.
  • This is so all usual golfing rules apply, and the shortest code (in bytes) wins.
Tweeted twitter.com/StackCodeGolf/status/983910048232607744
deleted 21 characters in body
Source Link
mdahmoune
  • 3.1k
  • 2
  • 16
  • 28
Loading
added 1 character in body
Source Link
mdahmoune
  • 3.1k
  • 2
  • 16
  • 28
Loading
deleted 1 character in body
Source Link
mdahmoune
  • 3.1k
  • 2
  • 16
  • 28
Loading
edited body
Source Link
mdahmoune
  • 3.1k
  • 2
  • 16
  • 28
Loading
edited body
Source Link
mdahmoune
  • 3.1k
  • 2
  • 16
  • 28
Loading
edited body
Source Link
mdahmoune
  • 3.1k
  • 2
  • 16
  • 28
Loading
Source Link
mdahmoune
  • 3.1k
  • 2
  • 16
  • 28
Loading