24
5
The challenge
The plastic number is a number related to the golden ratio, with many interesting mathematical properties. As such, there are many approaches that can be used to calculate the number.
In order to precisely specify the number for the purposes of this challenge, we'll use the following definition (although there are plenty of equivalent definitions, and you can use any definition you wish as long as it comes to the same number):
The plastic number is a real number ρ such that ρ³=ρ+1.
Your challenge is to write a program or function which takes an integer x as input (with x > 1), and produces an approximation to ρ as output, such that the larger the value of x gets, the closer the output gets to ρ (with at most finitely many exceptions; staying at the same value counts as "closer" for this purpose), and for any positive number δ, there's some input x to your program that produces an output that's within δ of ρ.
Clarifications
- If you're outputting via a method that inherently outputs strings (e.g. the standard output stream), you can format output either in decimal (e.g.
1.3247179572
), or as a ratio of two integers with a/
character between them. - If you're outputting as a value within your programming language (e.g. returning from a function), it must be of a fixed-point, floating-point, or rational type. (In particular, you can't use data types that store numbers symbolically, unless they're used only to hold the ratio of two integers. So if you're using Mathematica or a similar language, you'll need to include the extra code to actually generate the digits of the output.)
- Your answer must work in a hypothetical variant of your language in which integers can be arbitrarily large, and memory (including stack) is unlimited. You may not assume that floating-point arithmetic in your language is arbitrarily accurate, but must instead use its actual accuracy (meaning that outputting a floating-point number is only going to be possible in languages where the accuracy of floating-point numbers can be controlled at runtime).
- x can have any meaning you want (so long as increasing it gives more accurate outputs). I imagine that most submissions will have it control the number of digits of output to produce, or the number of iterations of the algorithm used by your program to converge on the plastic number, but other meanings are acceptable.
Testcase
Here are the first few digits of the plastic number:
1.32471795724474602596090885
More digits are available on OEIS.
Victory condition
As usual for code-golf, shorter is better, measured in bytes. However, feel free to post answers even if they don't win, so long as they add something (e.g. a different language, or a different algorithm) to the existing answers.
1
hmm, (cbrt(108+12sqrt(69))+cbrt(108-12sqrt(69)))/6 this seems like a good time to use `the Drake approximation': sqrt(69)=8.something http://bit.ly/2rCqedX ^_^
– DrQuarius – 2017-06-16T03:34:48.7102Can we also assume the recursion/stack depth is unlimited? – xnor – 2017-06-16T03:40:38.990
To clarify the second point, can we use arbitrary precision libraries (e.g. mpmath in Python)? They use an auxiliary data type, but do you count that as storing things "symbolically"? – Batman – 2017-06-16T04:08:37.393
@Batman: I believe
mpmath
works by storing a decimal expansion. That's not a symbolic data type, so it's fine. – None – 2017-06-16T05:30:26.303@xnor: Yes, as usual on PPCG. I'll edit that into the question. – None – 2017-06-16T05:30:35.637
This is a great model for irrational number challenges. I really like the x parameter to control accuracy. – Martin Ender – 2017-06-16T05:45:45.553
Is {{x -> 1.32471795724474602596090885}} a valid output format? – J42161217 – 2017-06-16T06:14:18.620
@Jenny_mathy: I'm not sure that we have a standard rule for that on PPCG; it might be worth asking on Meta (this is a situation where we'd expect the standard rules to be usable). I'm currently inclined towards no, but wouldn't much care if a meta discussion found otherwise. – None – 2017-06-16T06:17:00.790
Does such that the larger the value of x gets, the closer the output gets to ρ have to hold for all pairs of x? – Dennis – 2017-06-16T07:03:16.997
@Dennis: Well, if it didn't, simply outputting all rationals in some order would be a valid solution; the rule's to prevent that. I don't think answers like this are what people would really expect from this sort of challenge.
– None – 2017-06-16T07:51:41.2631Well, at the very least I'd expect answers to converge to ρ. Also, an "honest" solution could easily fail the test x > y --> |ρx - ρ| > |ρy - ρ| for a finite number of (x, y) pairs. If that isn't acceptable, I think this should be made more explicit in the spec. – Dennis – 2017-06-16T07:58:06.747
@Dennis: OK, I've clarified the ambiguity by allowing finitely many exceptions (given that it's not very interesting to be forced to add a constant to x to jump past exceptions with low values). – None – 2017-06-16T08:00:24.033
6
Many answerers have fallen into the trap(?) of computing an x digit approximation to ρ, the problem being that there are probably infinitely many x such that an (x + 1)-digit approximation is no better than an x digit approximation. You should probably clarify whether you intended this to be allowed. If you don’t, replace “closer” with “strictly closer”; if you do, “at least as close”, or something. You could also consider the looser requirement that the sequence converges to ρ, which would additionally allow xnor’s answer.
– Anders Kaseorg – 2017-06-16T09:25:47.337@AndersKaseorg: OK, that was a clear bug in the problem (with zero digits getting no closer). I've fixed it. As for converging, I'm rather torn on that; note that most of the current solutions that converge (but go backwards infinitely many times) can easily be fixed by using 2**x rather than just x. – None – 2017-06-16T17:42:18.313
@ais523 I'd favor just going with convergence because it's a standard definition rather than a custom-made one. It's what I'd expect for a challenge to approximate a value arbitrarily well. – xnor – 2017-06-16T21:57:16.217