Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
31
Task/Haversine-formula/ERRE/haversine-formula.erre
Normal file
31
Task/Haversine-formula/ERRE/haversine-formula.erre
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
% Implemented by Claudio Larini
|
||||
|
||||
PROGRAM HAVERSINE_DEMO
|
||||
|
||||
!$DOUBLE
|
||||
|
||||
CONST DIAMETER=12745.6
|
||||
|
||||
FUNCTION DEG2RAD(X)
|
||||
DEG2RAD=X*π/180
|
||||
END FUNCTION
|
||||
|
||||
FUNCTION RAD2DEG(X)
|
||||
RAD2DEG=X*180/π
|
||||
END FUNCTION
|
||||
|
||||
PROCEDURE HAVERSINE_DIST(TH1,PH1,TH2,PH2->RES)
|
||||
LOCAL DX,DY,DZ
|
||||
PH1=DEG2RAD(PH1-PH2)
|
||||
TH1=DEG2RAD(TH1)
|
||||
TH2=DEG2RAD(TH2)
|
||||
DZ=SIN(TH1)-SIN(TH2)
|
||||
DX=COS(PH1)*COS(TH1)-COS(TH2)
|
||||
DY=SIN(PH1)*COS(TH1)
|
||||
RES=ASN(SQR(DX^2+DY^2+DZ^2)/2)*DIAMETER
|
||||
END PROCEDURE
|
||||
|
||||
BEGIN
|
||||
HAVERSINE_DIST(36.12,-86.67,33.94,-118.4->RES)
|
||||
PRINT("HAVERSINE DISTANCE: ";RES;" KM.")
|
||||
END PROGRAM
|
||||
36
Task/Haversine-formula/FreeBASIC/haversine-formula.freebasic
Normal file
36
Task/Haversine-formula/FreeBASIC/haversine-formula.freebasic
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
' version 09-10-2016
|
||||
' compile with: fbc -s console
|
||||
|
||||
' Nashville International Airport (BNA) in Nashville, TN, USA,
|
||||
' N 36°07.2', W 86°40.2' (36.12, -86.67)
|
||||
' Los Angeles International Airport (LAX) in Los Angeles, CA, USA,
|
||||
' N 33°56.4', W 118°24.0' (33.94, -118.40).
|
||||
' 6372.8 km is an approximation of the radius of the average circumference
|
||||
|
||||
#Define Pi Atn(1) * 4 ' define Pi = 3.1415..
|
||||
#Define deg2rad Pi / 180 ' define deg to rad 0.01745..
|
||||
#Define earth_radius 6372.8 ' earth radius in km.
|
||||
|
||||
Function Haversine(lat1 As Double, long1 As Double, lat2 As Double, _
|
||||
long2 As Double , radius As Double) As Double
|
||||
|
||||
Dim As Double d_long = deg2rad * (long1 - long2)
|
||||
Dim As Double theta1 = deg2rad * lat1
|
||||
Dim As Double theta2 = deg2rad * lat2
|
||||
Dim As Double dx = Cos(d_long) * Cos(theta1) - Cos(theta2)
|
||||
Dim As Double dy = Sin(d_long) * Cos(theta1)
|
||||
Dim As Double dz = Sin(theta1) - Sin(theta2)
|
||||
Return Asin(Sqr(dx*dx + dy*dy + dz*dz) / 2) * radius * 2
|
||||
|
||||
End Function
|
||||
|
||||
Print
|
||||
Print " Haversine distance between BNA and LAX = "; _
|
||||
Haversine(36.12, -86.67, 33.94, -118.4, earth_radius); " km."
|
||||
|
||||
|
||||
' empty keyboard buffer
|
||||
While Inkey <> "" : Wend
|
||||
Print : Print "hit any key to end program"
|
||||
Sleep
|
||||
End
|
||||
12
Task/Haversine-formula/FunL/haversine-formula.funl
Normal file
12
Task/Haversine-formula/FunL/haversine-formula.funl
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
import math.*
|
||||
|
||||
def haversin( theta ) = (1 - cos( theta ))/2
|
||||
|
||||
def radians( deg ) = deg Pi/180
|
||||
|
||||
def haversine( (lat1, lon1), (lat2, lon2) ) =
|
||||
R = 6372.8
|
||||
h = haversin( radians(lat2 - lat1) ) + cos( radians(lat1) ) cos( radians(lat2) ) haversin( radians(lon2 - lon1) )
|
||||
2R asin( sqrt(h) )
|
||||
|
||||
println( haversine((36.12, -86.67), (33.94, -118.40)) )
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
include "ConsoleWindow"
|
||||
|
||||
local fn Haversine( lat1 as double, lon1 as double, lat2 as double, lon2 as double, miles as ^double, kilometers as ^double )
|
||||
dim as double deg2rad, dLat, dLon, a, c, earth_radius_miles, earth_radius_kilometers
|
||||
|
||||
earth_radius_miles = 3959.0 // Radius of the Earth in miles
|
||||
earth_radius_kilometers = 6372.8 // Radius of the Earth in kilometers
|
||||
deg2rad = Pi / 180 // Pi is predefined in FutureBasic
|
||||
|
||||
dLat = deg2rad * ( lat2 - lat1 )
|
||||
dLon = deg2rad * ( lon2 - lon1 )
|
||||
a = sin( dLat / 2 ) * sin( dLat / 2 ) + cos( deg2rad * lat1 ) * cos( deg2rad * lat2 ) * sin( dLon / 2 ) * sin( dLon / 2 )
|
||||
c = 2 * asin( sqr(a) )
|
||||
|
||||
miles.nil# = earth_radius_miles * c
|
||||
kilometers.nil# = earth_radius_kilometers * c
|
||||
end fn
|
||||
|
||||
dim as double miles, kilometers
|
||||
fn Haversine( 36.12, -86.67, 33.94, -118.4, @miles, @kilometers )
|
||||
|
||||
print "Distance in miles between BNA and LAX: "; using "####.####"; miles; " miles."
|
||||
print "Distance in kilometers between BNA LAX: "; using "####.####"; kilometers; " km."
|
||||
40
Task/Haversine-formula/Idris/haversine-formula.idris
Normal file
40
Task/Haversine-formula/Idris/haversine-formula.idris
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
module Main
|
||||
|
||||
-- The haversine of an angle.
|
||||
hsin : Double -> Double
|
||||
hsin t = let u = sin (t/2) in u*u
|
||||
|
||||
-- The distance between two points, given by latitude and longtitude, on a
|
||||
-- circle. The points are specified in radians.
|
||||
distRad : Double -> (Double, Double) -> (Double, Double) -> Double
|
||||
distRad radius (lat1, lng1) (lat2, lng2) =
|
||||
let hlat = hsin (lat2 - lat1)
|
||||
hlng = hsin (lng2 - lng1)
|
||||
root = sqrt (hlat + cos lat1 * cos lat2 * hlng)
|
||||
in 2 * radius * asin (min 1.0 root)
|
||||
|
||||
-- The distance between two points, given by latitude and longtitude, on a
|
||||
-- circle. The points are specified in degrees.
|
||||
distDeg : Double -> (Double, Double) -> (Double, Double) -> Double
|
||||
distDeg radius p1 p2 = distRad radius (deg2rad p1) (deg2rad p2)
|
||||
where
|
||||
d2r : Double -> Double
|
||||
d2r t = t * pi / 180
|
||||
deg2rad (t, u) = (d2r t, d2r u)
|
||||
|
||||
-- The approximate distance, in kilometers, between two points on Earth.
|
||||
-- The latitude and longtitude are assumed to be in degrees.
|
||||
earthDist : (Double, Double) -> (Double, Double) -> Double
|
||||
earthDist = distDeg 6372.8
|
||||
|
||||
main : IO ()
|
||||
main = putStrLn $ "The distance between BNA and LAX is about " ++ show (floor dst) ++ " km."
|
||||
where
|
||||
bna : (Double, Double)
|
||||
bna = (36.12, -86.67)
|
||||
|
||||
lax : (Double, Double)
|
||||
lax = (33.94, -118.40)
|
||||
|
||||
dst : Double
|
||||
dst = earthDist bna lax
|
||||
27
Task/Haversine-formula/LiveCode/haversine-formula-1.livecode
Normal file
27
Task/Haversine-formula/LiveCode/haversine-formula-1.livecode
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
function radians n
|
||||
return n * (3.1415926 / 180)
|
||||
end radians
|
||||
|
||||
function haversine lat1, lng1, lat2, lng2
|
||||
local radiusEarth
|
||||
local lat3, lng3
|
||||
local lat1Rad, lat2Rad, lat3Rad
|
||||
local lngRad1, lngRad2, lngRad3
|
||||
local haver
|
||||
put 6372.8 into radiusEarth
|
||||
put (lat2 - lat1) into lat3
|
||||
put (lng2 - lng1) into lng3
|
||||
put radians(lat1) into lat1Rad
|
||||
put radians(lat2) into lat2Rad
|
||||
put radians(lat3) into lat3Rad
|
||||
put radians(lng1) into lngRad1
|
||||
put radians(lng2) into lngRad2
|
||||
put radians(lng3) into lngRad3
|
||||
|
||||
put (sin(lat3Rad/2.0)^2) + (cos(lat1Rad)) \
|
||||
* (cos(lat2Rad)) \
|
||||
* (sin(lngRad3/2.0)^2) \
|
||||
into haver
|
||||
return (radiusEarth * (2.0 * asin(sqrt(haver))))
|
||||
|
||||
end haversine
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
haversine(36.12, -86.67, 33.94, -118.40)
|
||||
2887.259923
|
||||
18
Task/Haversine-formula/Nim/haversine-formula.nim
Normal file
18
Task/Haversine-formula/Nim/haversine-formula.nim
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
import math
|
||||
|
||||
proc radians(x): float = x * Pi / 180
|
||||
|
||||
proc haversine(lat1, lon1, lat2, lon2): float =
|
||||
const r = 6372.8 # Earth radius in kilometers
|
||||
let
|
||||
dLat = radians(lat2 - lat1)
|
||||
dLon = radians(lon2 - lon1)
|
||||
lat1 = radians(lat1)
|
||||
lat2 = radians(lat2)
|
||||
|
||||
a = sin(dLat/2)*sin(dLat/2) + cos(lat1)*cos(lat2)*sin(dLon/2)*sin(dLon/2)
|
||||
c = 2*arcsin(sqrt(a))
|
||||
|
||||
result = r * c
|
||||
|
||||
echo haversine(36.12, -86.67, 33.94, -118.40)
|
||||
12
Task/Haversine-formula/Oforth/haversine-formula.oforth
Normal file
12
Task/Haversine-formula/Oforth/haversine-formula.oforth
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
import: math
|
||||
|
||||
: haversine(lat1, lon1, lat2, lon2)
|
||||
| lat lon |
|
||||
|
||||
lat2 lat1 - asRadian ->lat
|
||||
lon2 lon1 - asRadian ->lon
|
||||
|
||||
lon 2 / sin sq lat1 asRadian cos * lat2 asRadian cos *
|
||||
lat 2 / sin sq + sqrt asin 2 * 6372.8 * ;
|
||||
|
||||
haversine(36.12, -86.67, 33.94, -118.40) println
|
||||
15
Task/Haversine-formula/Ring/haversine-formula.ring
Normal file
15
Task/Haversine-formula/Ring/haversine-formula.ring
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
decimals(8)
|
||||
see haversine(36.12, -86.67, 33.94, -118.4) + nl
|
||||
|
||||
func haversine x1, y1, x2, y2
|
||||
r=0.01745
|
||||
x1= x1*r
|
||||
x2= x2*r
|
||||
y1= y1*r
|
||||
y2= y2*r
|
||||
dy = y2-y1
|
||||
dx = x2-x1
|
||||
a = pow(sin(dx/2),2) + cos(x1) * cos(x2) * pow(sin(dy/2),2)
|
||||
c = 2 * asin(sqrt(a))
|
||||
d = 6372.8 * c
|
||||
return d
|
||||
29
Task/Haversine-formula/Sidef/haversine-formula.sidef
Normal file
29
Task/Haversine-formula/Sidef/haversine-formula.sidef
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
class EarthPoint(lat, lon) {
|
||||
|
||||
const earth_radius = 6371; # mean earth radius
|
||||
const radian_ratio = Math.pi/180;
|
||||
|
||||
# accessors for radians
|
||||
method latR { self.lat * radian_ratio };
|
||||
method lonR { self.lon * radian_ratio };
|
||||
|
||||
method haversine_dist(EarthPoint p) {
|
||||
var arc = EarthPoint(
|
||||
self.lat - p.lat,
|
||||
self.lon - p.lon,
|
||||
);
|
||||
|
||||
var a = Math.sum(
|
||||
(arc.latR / 2).sin**2,
|
||||
(arc.lonR / 2).sin**2 *
|
||||
self.latR.cos * p.latR.cos
|
||||
)
|
||||
|
||||
earth_radius * a.sqrt.asin * 2;
|
||||
}
|
||||
}
|
||||
|
||||
var BNA = EarthPoint.new(lat: 36.12, lon: -86.67);
|
||||
var LAX = EarthPoint.new(lat: 33.94, lon: -118.4);
|
||||
|
||||
say BNA.haversine_dist(LAX); # => 2886.44444283798329974715782394574672
|
||||
18
Task/Haversine-formula/Swift/haversine-formula.swift
Normal file
18
Task/Haversine-formula/Swift/haversine-formula.swift
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
import Foundation
|
||||
|
||||
func haversine(lat1:Double, lon1:Double, lat2:Double, lon2:Double) -> Double {
|
||||
let lat1rad = lat1 * M_PI/180
|
||||
let lon1rad = lon1 * M_PI/180
|
||||
let lat2rad = lat2 * M_PI/180
|
||||
let lon2rad = lon2 * M_PI/180
|
||||
|
||||
let dLat = lat2rad - lat1rad
|
||||
let dLon = lon2rad - lon1rad
|
||||
let a = sin(dLat/2) * sin(dLat/2) + sin(dLon/2) * sin(dLon/2) * cos(lat1rad) * cos(lat2rad)
|
||||
let c = 2 * asin(sqrt(a))
|
||||
let R = 6372.8
|
||||
|
||||
return R * c
|
||||
}
|
||||
|
||||
println(haversine(36.12, -86.67, 33.94, -118.40))
|
||||
9
Task/Haversine-formula/jq/haversine-formula.jq
Normal file
9
Task/Haversine-formula/jq/haversine-formula.jq
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
def haversine(lat1;lon1; lat2;lon2):
|
||||
def radians: . * (1|atan)/45;
|
||||
def sind: radians|sin;
|
||||
def cosd: radians|cos;
|
||||
def sq: . * .;
|
||||
|
||||
(((lat2 - lat1)/2) | sind | sq) as $dlat
|
||||
| (((lon2 - lon1)/2) | sind | sq) as $dlon
|
||||
| 2 * 6372.8 * (( $dlat + (lat1|cosd) * (lat2|cosd) * $dlon ) | sqrt | asin) ;
|
||||
Loading…
Add table
Add a link
Reference in a new issue