22
4
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 code-golf 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!
16I'll give brainfuck a shot. Let's see what horrible monster comes out. – YoYoYonnY – 2016-01-30T12:56:14.483
I assume you mean the Euclidean distance? – flawr – 2016-01-30T13:54:06.580
3@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 – 2016-01-30T14:02:25.760
@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 – 2016-01-30T14:58:06.307
@YoYoYonnY Go for it then. Brainfuck won't win this one anyway, but I really wanna see your solution :) Just make it clear in your answer that you output the distance squared. – Denker – 2016-01-30T15:31:27.027
2@DenkerAffe I think it's safe to say that brainfuck will never win a code golf. But it's just for fun anyways :) – YoYoYonnY – 2016-01-30T16:17:04.753
I propose adding a non-floating point clause (See my edit) – YoYoYonnY – 2016-01-31T00:46:37.257
Can we assume the input is all in floating-point (no ints) if our language supports it/is strongly-typed? – snail_ – 2018-04-11T21:25:26.663