# efficient mean of solving constrained OLS problems?

I was wondering whether there was a efficient procedure for solving constrained quadratic approximations of the form:

$$\underset{k\in \mathbb{R}}{\min}\;||x_i-kx_0||_2$$

for fixed values of $x_0,x_i\in \mathbb{R}^d$. This needs to be solved recursively for a large number of values of $x_i$ for fixed $x_0$ (so if there is a way to parametrize the solution in terms of $x_i$, that will be helpful).

Currently, I'm using an iterative algorithm (in R):

set.seed(123)
d<-100
n<-50
xi<-matrix(rnorm(n*d),n,d)
x0<-rnorm(d)
ff<-function(k,xi,x0,ll){
crossprod(xi[ll,]-k*x0)
}