[FUN] CodeGolf: solve equation x^x=C

classic Classic list List threaded Threaded
46 messages Options
123
Reply | Threaded
Open this post in threaded view
|

Re: [FUN] CodeGolf: solve equation x^x=C

Dirk Laurie-2
Op Vr. 28 Jun. 2019 om 18:11 het Albert Chan <[hidden email]> geskryf:

>
>
> > On Jun 28, 2019, at 9:24 AM, Albert Chan <[hidden email]> wrote:
> >
> > Had we use a higher guess, say 4, we would have c > 16
>
> Example, x^x = c = 15.8, guess x0 = 4, iterations diverges:
>
> x1 = log(c,x0) = 1.9909
> x2 = log(c,x1) = 4.0081
> x3 = log(c,x2) = 1.9880
> x4 = log(c,x3) = 4.0167 ...

x0 is not arbitrary. You can't have x0=4 except when C=exp(4).

The shortest code has x uninitialized, i.e. x=nil.  Therefore x0 =
log(C,nil) = log(C).

Reply | Threaded
Open this post in threaded view
|

Re: [FUN] CodeGolf: solve equation x^x=C

Egor Skriptunoff-2
In reply to this post by Xavier Wang
On Fri, Jun 28, 2019 at 1:42 PM Xavier Wang wrote:

Dirk Laurie 于2019年6月28日周五 下午6:22写道:
I can do 41 bytes.

x=3 while 1 do x=math.log(C,x)print(x)end

 
Thanks! combine with your method, I finally have a 34 bytes solution:
::a::x=math.log(C,x)print(x)goto a


Nice!  You are the first person posted a solution.
And I should note that it was Dirk who has solved the mathematical half of the task.

BTW, my own program was longer than 34 bytes because I wasn't aware of the idea of using non-initialized X.


But this is not the end of the story.
There is a bonus task: "dijkstrify" the code
Edsger Dijkstra considered "goto" statement harmful.
Rewrite the code preserving its length of 34 bytes but without using "goto".

Reply | Threaded
Open this post in threaded view
|

Re: [FUN] CodeGolf: solve equation x^x=C

Sergey Kovalev
In reply to this post by Egor Skriptunoff-2
Have no idea how to fit it in 36 bytes but simple solution may look like this:

[dirty-solution.lua]
function X(C,L,t,x)L=math.log x,t=3,L(C)for i=0,9 do
x=(x*x+t*t/L(x))/(x+t)end return x end

[test.lua]
C=123 -- 3.6875806713595 next line is 36 byes :)
require "dirty-solution" print(X(C))

ср, 26 июн. 2019 г. в 23:47, Egor Skriptunoff <[hidden email]>:
>
> Global variable "C" contains numeric value > 15.5
> The solution of math equation x^x=C must be printed
> Your code must fit into 36 bytes
> Dirty tricks are allowed

Reply | Threaded
Open this post in threaded view
|

Re: [FUN] CodeGolf: solve equation x^x=C

Soni "They/Them" L.
In reply to this post by Egor Skriptunoff-2


On 2019-06-28 6:03 p.m., Egor Skriptunoff wrote:

> On Fri, Jun 28, 2019 at 1:42 PM Xavier Wang wrote:
>
>
>     Dirk Laurie 于2019年6月28日周五 下午6:22写道:
>
>         I can do 41 bytes.
>
>         x=3 while 1 do x=math.log(C,x)print(x)end
>
>     Thanks! combine with your method, I finally have a 34 bytes solution:
>     ::a::x=math.log(C,x)print(x)goto a
>
>
> Nice!  You are the first person posted a solution.
> And I should note that it was Dirk who has solved the mathematical
> half of the task.
>
> BTW, my own program was longer than 34 bytes because I wasn't aware of
> the idea of using non-initialized X.
>
>
> But this is not the end of the story.
> There is a bonus task: *"dijkstrify" the code*
> Edsger Dijkstra considered "goto" statement harmful.
> Rewrite the code preserving its length of 34 bytes but without using
> "goto".
>

::a::x=math.log(C,x)print(x)goto a
repeat x=math.log(C,x)until print(x)

uh well, 2 bigger. I tried.

Reply | Threaded
Open this post in threaded view
|

Re: [FUN] CodeGolf: solve equation x^x=C

Sean Conner
In reply to this post by Egor Skriptunoff-2
It was thus said that the Great Egor Skriptunoff once stated:
>
> But this is not the end of the story.
> There is a bonus task: *"dijkstrify" the code*
> Edsger Dijkstra considered "goto" statement harmful.
> Rewrite the code preserving its length of 34 bytes but without using "goto".

for x in math.log,C do print(x)end

  -spc (Do I win a prize?)

Reply | Threaded
Open this post in threaded view
|

Re: [FUN] CodeGolf: solve equation x^x=C

Egor Skriptunoff-2
On Sat, Jun 29, 2019 at 12:35 AM Sean Conner wrote:
for x in math.log,C do print(x)end


CORRECT!


Do I win a prize?


Definitely :-)
123