Skip to main content
added 9 characters in body
Source Link
Dominic van Essen
  • 37.2k
  • 2
  • 24
  • 61

R, 9191 90 bytes

\(x,n,a=polyroot(x),c=Re(a[!Im(a)])%/%1+1%1+0:n/n)c[which.minc[order(sapply(c,\(y)x%*%y^seq(!x)/y)^2)]][1] 

Attempt This Online!Attempt This Online!

Uses the R polyroot function to find the (limited-precision) root a, then for each element n of the sequence, divides floor(a)-ceiling(a) into n steps and returns the one with the smallest absolute result after applying the polynomial.
The final result is still limited by R's precision*, but the approach should work in principle, and is not affected by the limited-precision of the polyroot function (as long as it is correct to the nearest integer).

The limit as n tends to infinity is the true real root of the polynomial; however, successive elements of the sequence may have increasing absolute differences from the true root, depending on whether partitioning the interval into n-1 or n steps allows a better solution. For a convergent-at-every-step sequence, divide into 2^n instead of n steps, for 4 more bytes: try it herehere.


(*) Note that this approach is actually very limited by R's precision, as the a[!Im(a)] part of the code (=get non-complex roots) fails quite easily due to floating-point inaccuracies: the test link overcomes this by redefining R's `!` (logical-NOT) operator to return TRUE as long as its argument is sufficiently small.

R, 91 bytes

\(x,n,a=polyroot(x),c=Re(a[!Im(a)])%/%1+1:n/n)c[which.min(sapply(c,\(y)x%*%y^seq(!x)/y)^2)] 

Attempt This Online!

Uses the R polyroot function to find the (limited-precision) root a, then for each element n of the sequence, divides floor(a)-ceiling(a) into n steps and returns the one with the smallest absolute result after applying the polynomial.
The final result is still limited by R's precision*, but the approach should work in principle, and is not affected by the limited-precision of the polyroot function (as long as it is correct to the nearest integer).

The limit as n tends to infinity is the true real root of the polynomial; however, successive elements of the sequence may have increasing absolute differences from the true root, depending on whether partitioning the interval into n-1 or n steps allows a better solution. For a convergent-at-every-step sequence, divide into 2^n instead of n steps, for 4 more bytes: try it here.


(*) Note that this approach is actually very limited by R's precision, as the a[!Im(a)] part of the code (=get non-complex roots) fails quite easily due to floating-point inaccuracies: the test link overcomes this by redefining R's `!` (logical-NOT) operator to return TRUE as long as its argument is sufficiently small.

R, 91 90 bytes

\(x,n,a=polyroot(x),c=Re(a[!Im(a)])%/%1+0:n/n)c[order(sapply(c,\(y)x%*%y^seq(!x)/y)^2)][1] 

Attempt This Online!

Uses the R polyroot function to find the (limited-precision) root a, then for each element n of the sequence, divides floor(a)-ceiling(a) into n steps and returns the one with the smallest absolute result after applying the polynomial.
The final result is still limited by R's precision*, but the approach should work in principle, and is not affected by the limited-precision of the polyroot function (as long as it is correct to the nearest integer).

The limit as n tends to infinity is the true real root of the polynomial; however, successive elements of the sequence may have increasing absolute differences from the true root, depending on whether partitioning the interval into n-1 or n steps allows a better solution. For a convergent-at-every-step sequence, divide into 2^n instead of n steps, for 4 more bytes: try it here.


(*) Note that this approach is actually very limited by R's precision, as the a[!Im(a)] part of the code (=get non-complex roots) fails quite easily due to floating-point inaccuracies: the test link overcomes this by redefining R's `!` (logical-NOT) operator to return TRUE as long as its argument is sufficiently small.

added 868 characters in body
Source Link
Dominic van Essen
  • 37.2k
  • 2
  • 24
  • 61

R, 91 bytes

\(x,n,a=polyroot(x),c=Re(a[!Im(a)])%/%1+1:n/n)c[which.min(sapply(c,\(y)x%*%y^seq(!x)/y)^2)] 

Attempt This Online!

Uses the R polyroot function to find the (limited-precision) root a, then for each element n of the sequence, divides floor(a)-ceiling(a) into n steps and returns the one with the smallest absolute result after applying the polynomial.
The final result is still limited by R's precisionprecision*, but the approach should work in principle, and is not affected by the limited-precision of the polyroot funcitonfunction (as long as it is correct to the nearest integer).

The limit as n tends to infinity is the true real root of the polynomial; however, whichsuccessive elements of the sequence may have increasing absolute differences from the true root, depending on whether partitioning the interval into n-1 or n steps allows a better solution. For a convergent-at-every-step sequence, divide into 2^n instead of n steps, for 4 more bytes: try it should be)here.
Note


(*) Note that this approach is actually very limited by R's precision, as the a[!Im(a)] part of the code (=get non-complex roots) fails quite easily due to floating-point inaccuracies: the test link overcomes this by redefining R's `!` (logical-NOT) operator to return TRUE as long as its argument is sufficiently small.

R, 91 bytes

\(x,n,a=polyroot(x),c=Re(a[!Im(a)])%/%1+1:n/n)c[which.min(sapply(c,\(y)x%*%y^seq(!x)/y)^2)] 

Attempt This Online!

Uses the R polyroot function to find the (limited-precision) root a, then for each element n of the sequence, divides floor(a)-ceiling(a) into n steps and returns the one with the smallest absolute result after applying the polynomial.
The final result is still limited by R's precision, but the approach should work in principle, and is not affected by the limited-precision of the polyroot funciton (as long as it is correct to the nearest integer, which it should be).
Note that this approach is actually very limited by R's precision, as the a[!Im(a)] part of the code (=get non-complex roots) fails quite easily due to floating-point inaccuracies: the test link overcomes this by redefining R's `!` (logical-NOT) operator to return TRUE as long as its argument is sufficiently small.

R, 91 bytes

\(x,n,a=polyroot(x),c=Re(a[!Im(a)])%/%1+1:n/n)c[which.min(sapply(c,\(y)x%*%y^seq(!x)/y)^2)] 

Attempt This Online!

Uses the R polyroot function to find the (limited-precision) root a, then for each element n of the sequence, divides floor(a)-ceiling(a) into n steps and returns the one with the smallest absolute result after applying the polynomial.
The final result is still limited by R's precision*, but the approach should work in principle, and is not affected by the limited-precision of the polyroot function (as long as it is correct to the nearest integer).

The limit as n tends to infinity is the true real root of the polynomial; however, successive elements of the sequence may have increasing absolute differences from the true root, depending on whether partitioning the interval into n-1 or n steps allows a better solution. For a convergent-at-every-step sequence, divide into 2^n instead of n steps, for 4 more bytes: try it here.


(*) Note that this approach is actually very limited by R's precision, as the a[!Im(a)] part of the code (=get non-complex roots) fails quite easily due to floating-point inaccuracies: the test link overcomes this by redefining R's `!` (logical-NOT) operator to return TRUE as long as its argument is sufficiently small.

Source Link
Dominic van Essen
  • 37.2k
  • 2
  • 24
  • 61

R, 91 bytes

\(x,n,a=polyroot(x),c=Re(a[!Im(a)])%/%1+1:n/n)c[which.min(sapply(c,\(y)x%*%y^seq(!x)/y)^2)] 

Attempt This Online!

Uses the R polyroot function to find the (limited-precision) root a, then for each element n of the sequence, divides floor(a)-ceiling(a) into n steps and returns the one with the smallest absolute result after applying the polynomial.
The final result is still limited by R's precision, but the approach should work in principle, and is not affected by the limited-precision of the polyroot funciton (as long as it is correct to the nearest integer, which it should be).
Note that this approach is actually very limited by R's precision, as the a[!Im(a)] part of the code (=get non-complex roots) fails quite easily due to floating-point inaccuracies: the test link overcomes this by redefining R's `!` (logical-NOT) operator to return TRUE as long as its argument is sufficiently small.