Distance between two points in n-dimensional space

Here is another simple one:

The Challenge

Given two points in an n-dimensional space, output the distance between them, also called the Euclidean distance.

• The coordinates will be rational numbers; the only limits are the restrictions of your language.
• Lowest dimension is 1, highest is whatever your language can handle
• You may assume that the two points are of the same dimension and that there will be no empty input.
• The distance has to be correct to at least 3 decimal places. If your language does not support floating point numbers, output the nearest whole number.

Rules

• As usual, function or full program allowed.
• Input may be taken from STDIN, command line- or function arguments.
• Input format is up to you, specify which one you used in your answer.
• Output may be provided by printing to stdout or return value.
• This is so lowest byte-count wins! In case of a tie, the earlier answer wins.

Test cases

Each point is represented by a list of length n.

[1], [3] -> 2
[1,1], [1,1] -> 0
[1,2], [3,4] -> 2.82842712475
[1,2,3,4], [5,6,7,8] -> 8
[1.5,2,-5], [-3.45,-13,145] -> 150.829382085
[13.37,2,6,-7], [1.2,3.4,-5.6,7.89] -> 22.5020221314


Happy Coding!

• I'll give brainfuck a shot. Let's see what horrible monster comes out. – YoYoYonnY Jan 30 '16 at 12:56
• I assume you mean the Euclidean distance? – flawr Jan 30 '16 at 13:54
• @flawr Yep, exactly. Just wanted to keep the title simple, since not everyone might know what that is at first glance. Could definetly write that in the challange tho :) – Denker Jan 30 '16 at 14:02
• @DenkerAffe is it OK to return the distance squared if "your programming language does not support floating points"? This would make my brainfuck program a lot more accurate (Otherwise I'll have to implement some sort of estimation algorithm). – YoYoYonnY Jan 30 '16 at 14:58
• @DenkerAffe I think it's safe to say that brainfuck will never win a code golf. But it's just for fun anyways :) – YoYoYonnY Jan 30 '16 at 16:17

05AB1E, 4 bytes

-nOt


Try it online!

Negative not y'all!

-    # a-b
n   # (a-b)**2
O  # sum((a-b)**2) for all a,b
t # sqrt(sum((a-b)**2) for all a,b)


C 276 bytes

f(){*a,*b,d=0;l=1;a=malloc(sizeof(float)*50);b=malloc(sizeof(float)*50);scanf("%f",&a[0]);while(getchar()!='\n'){scanf("%f",&a[l]);l++;}l=1;scanf("%f",&b[0]);while(getchar()!='\n'){scanf("%f",&b[l]);l++;}for(int i=0;i<l;i++){d+=pow((a[i]-b[i]),2);}printf("%.5f",pow(d,0.5));}


Ungolfed version:

void f()
{

float *a,*b,d=0;
int l=1;

a=malloc(sizeof(float)*50);
b=malloc(sizeof(float)*50);

//Accept p1,p2,p3.....pn
scanf("%f",&a[0]);

while(getchar()!='\n')
{
scanf("%f",&a[l]);
l++;
}
l=1;

//Accept q1,q2,q3.....qn
scanf("%f",&b[0]);
while(getchar()!='\n')
{
scanf("%f",&b[l]);
l++;
}

for(int i=0;i<l;i++)
{
d+=pow((a[i]-b[i]),2);
}

printf("\n%.5f",pow(d,0.5));
}


Pretty straightforward. This solution lets you measure distance between 2 points in upto 50 dimensions (can be increased).

In Cartesian coordinates, input the position of first point of n dimensions. (p1 p2 p3 ...pn) and press Enter.

Next, input the position of second point of n dimensions. (q1 q2 q3 ...qn) and press Enter to get the Euclidean Distance.

Elixir, 74 bytes

:math.sqrt Enum.reduce Enum.zip(p,q),0,fn({a,b},c)->:math.pow(a-b,2)+c end


You can try it online

• Welcome to PPCG! Nice first post! – Rɪᴋᴇʀ Nov 24 '17 at 2:15

TI-Basic (TI-84 Plus CE), 15 bytes

Prompt A,B
√(sum((LA-LB)2

TI-Basic is a tokenized language.

Prompts for input as two lists, and returns the Euclidian distance betwrrn them in Ans

Explanation:

Prompt A,B    # 5 bytes, Prompts for two inputs; if the user inputs lists:
# they are stored in LA and LB
√(sum((LA-LB)2 # 10 bytes, Euclidian distance between points
#(square root of (sum of (squares of (differences of coordinates))))


Excel VBA, 36 Bytes

Anonymous VBE immediate window function that takes input as two vectors, which are projected unto the range 1:2, and outputs to the VBE immediate window

[3:3]="=(A1-A2)^2":?[Sqrt(Sum(3:3))]


Python 3, 44 bytes

lambda*a:sum((x-y)**2for x,y in zip(*a))**.5


Try it online!

Ungolfed version:

      ₙ
sqrt( ∑(xᵢ - yᵢ)² )
ⁱ⁼¹

def d(a, b):                    # two points (lists or tuples)
return sum(                 # sum of...
(x - y) ** 2            # (xᵢ - yᵢ)²
for x,y in zip(a, b)    #
) ** 0.5                    # square root of sum


Stax, 8 bytes

äÖ╙í▼=╬b


Run and debug it

Unpacked, ungolfed, and commented, it looks like this.

\       zip the coordinates into pairs
{       for each pair,
:s    compute the absolute span
J+    square and add to total
F
|Q      square root result


Run this one

C (gcc), 79 bytes

TIO requires compiler flag -lm. GCC of MinGW (that I use) does not.

f(p,q,d,s)float*p,*q,s;{for(s=0;d--;)s+=pow(p[d]-q[d],2);printf("%f",sqrt(s));}


Try it online!