Jump to content

Solve any equation with this 175 Byte function


minxomat
 Share

Recommended Posts

Hi.
 
It is winter outside, irish coffee is warming my heart and crazy things happen at the german forum at 1am.
 
So i came up with this function which is basically the whole newton equation solving algorithm in 287 (see #4 for 175 version) Bytes (using 6 vars) total. I know, it is highly unoptimized (For-Loops cause math errors and a huge performance drop, so does Dim) and totally against any rules which apply to good coding standards. But it was fun.
 
Example: 11^x+12^x  = 242/x+(144*(Cos(0)*2))/x
 
Next you want to choose a start value. 1 should do it in this case, and this is also the default used in the function. The complete Example:
 

ConsoleWrite(n("11^x+12^x", "242/x+(144*(Cos(0)*2))/x") & @LF)


; Function.........: n
; Does.............: solve equations
; Size.............: 278 Bytes
; Syntax...........: n(lterm, rterm, startval)
; Author...........: minx

Func n($0,$1,$2=1)
Dim $3=$0&"-("&$1&")",$4,$5=1/10^9
For $c=0 To 10^4
$6=$2+$5
$z=Execute(StringReplace($3,"x","$2"))
$2-=$z/((Execute(StringReplace($3,"x","$6"))-$z)/$5)
If IsInt($c/1000)+IsInt($c/2000)=1 Then $4=$2
If Abs($2-$4)+($4=$2)=1 Then Return $2
Next
EndFunc

cheers!

Edited by minx

I will answer every single PM, and you are free to ask anything anytime.

Link to comment
Share on other sites

Tried to shorten it a bit :)

Func n($0,$1,$2=1)
$0&='-('&$1&')'
For $3=0 To 10^4
$4=Execute(StringReplace($0,'x',$2))
$2-=$4/(Execute(StringReplace($0,'x',$2+1/10^9))-$4)/10^9
If IsInt($3/1000) And IsInt($3/2000)=0 Then $1=$2
If Abs($2-$1+$1=$2)=1 Then Return $2
Next
EndFunc ; 254 Byte

And for the crazy ones:

Func n($0,$1,$2=1,$3=-1,$4=0,$5=Assign,$6=StringReplace)
    Return ($3=-1)?n($0&'-('&$1&')',$1,$2,0):0*$5('4' _
    ,Execute($6($0,'x',$2)))*$5('2',$2-$4/(Execute($6 _
    ($0,'x',$2+1/10^9))-$4)/10^9)*((IsInt($3/1000)And _
    IsInt($3/2000)=0)?$5('1',$2):0)+Abs($2-$1+$1=$2)= _
    1?$2:n($0,$1,$2,$3+1)
EndFunc ; 285 Bytes (without _)

M

Link to comment
Share on other sites

Shortened the shortened version again, 250 Bytes now:
 

Func n($0,$1,$2=1)
$0&='-('&$1&')'
For $3=0 To 10^4
$4=Execute(StringReplace($0,'x',$2))
$2-=$4/(Execute(StringReplace($0,'x',$2+1/10^9))-$4)/10^9
If IsInt($3/1000)+IsInt($3/2000)=1 Then $1=$2
If Abs($2-$1+$1=$2)=1 Then Return $2
Next
EndFunc

I will answer every single PM, and you are free to ask anything anytime.

Link to comment
Share on other sites

Func n($0,$1,$2=1)
$0&='-('&$1&')'
For $3=0 To 999
$4=Execute(StringReplace($0,'x',$2))
$2-=$4/(Execute(StringReplace($0,'x',$2+1/10^9))-$4)/10^9
Next
Return $2
EndFunc

175 :)

At 2:00 noone sees, that Abs($2-$1+$1=$2)=1 -> $1=$2

M

Edit:

Func n($0,$1,$2=1,$3=Assign,$4=StringReplace)
Return IsString($1)?n($0&'-('&$1&')',0,$2):0*$3(6, _
Execute($4($0,'x',$2)))*$3(2,$2-$6/(Execute($4($0, _
'x',$2+1/10^9))-$6)/10^9)+$1=999?$2:n($0,$1+1,$2)
EndFunc
Edited by Marsi
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...