Euler's identity: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: simplified cos, sin, sqrt functions, added whitespace.)
Line 518: Line 518:
<br>program.
<br>program.
<lang rexx>/*REXX program proves Euler's identity by showing that: e^(i pi) + 1 ≡ 0 */
<lang rexx>/*REXX program proves Euler's identity by showing that: e^(i pi) + 1 ≡ 0 */
numeric digits length( pi() ) - 1 /*set number of decimal digs precision.*/
numeric digits length( pi() ) - length(.) /*define pi; set # dec. digs precision*/
cosPi= fmt( cos( pi() ) ) /*calculate the value of cos(pi). */
cosPI= fmt( cos(pi) ) /*calculate the value of cos(pi). */
sinPi= fmt( sin( pi() ) ) /* " " " " sin(pi). */
sinPI= fmt( sin(pi) ) /* " " " " sin(pi). */
say ' cos(pi) = ' cosPi /*display " " " cos(Pi). */
say ' cos(pi) = ' cosPI /*display " " " cos(Pi). */
say ' sin(pi) = ' sinPi /* " " " " sin(Pi). */
say ' sin(pi) = ' sinPI /* " " " " sin(Pi). */
say /*separate the wheat from the chaff. */
say /*separate the wheat from the chaff. */
$= cosPi + mult( sqrt(-1), sinPi) + 1 /*calc. product of sin(x) and sqrt(-1).*/
$= cosPI + mult( sqrt(-1), sinPI ) + 1 /*calc. product of sin(x) and sqrt(-1).*/
say 'e^(i pi) + 1 = ' fmt($) proof($ = 0) /*display both sides of the equation. */
say ' e^(i pi) + 1 = ' fmt($) ' ' word("unproven proven", ($=0) + 1)
exit /*stick a fork in it, we're all done. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
fmt: procedure; parse arg x; x=format(x,,digits()%2,0); return left('', x>=0)x / 1
fmt: procedure; parse arg x; x= format(x, , digits() %2, 0); return left('', x>=0)x /1
mult: procedure; parse arg a,b; if a=0 | b=0 then return 0; return a*b
mult: procedure; parse arg a,b; if a=0 | b=0 then return 0; return a*b
pi: pi= 3.1415926535897932384626433832795028841971693993751058209749445923; return pi
pi: pi= 3.1415926535897932384626433832795028841971693993751058209749445923; return pi
proof: procedure; parse arg ?; return ' ' word("unproven proven", ? + 1)
cos: procedure; parse arg x; z= 1; _= 1; q= x*x; i= -1; return .sinCos()
cos: procedure; parse arg x; return .sinCos(1, -1)
sin: procedure; parse arg x 1 z 1 _; q= x*x; i= 1; return .sinCos()
sin: procedure; parse arg x; return .sinCos(x, 1)
.sinCos: do k=2 by 2 until p=z; p=z; _= -_ * q/(k*(k+i)); z= z+_; end; return z
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
.sinCos: parse arg z 1 _,i; q=x*x; do k=2 by 2 until p=z; p=z; _=-_*q/(k*(k+i)); z=z+_
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); i=; h= d+6
end /*k*'; return z
numeric digits; numeric form; if x<0 then do; x= -x; i= 'i'; end; m.= 9
/*──────────────────────────────────────────────────────────────────────────────────────*/
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); i=; m.=9; h=d+6
numeric digits; numeric form; if x<0 then do; x= -x; i= 'i'; end
parse value format(x, 2, 1, , 0) 'E0' with g 'E' _ .; g= g * .5'e'_ % 2
parse value format(x, 2, 1, , 0) 'E0' with g 'E' _ .; g= g * .5'e'_ % 2
do j=0 while h>9; m.j=h; h= h%2 + 1; end /*j*/
do j=0 while h>9; m.j= h; h= h % 2 + 1; end
do k=j+5 to 0 by -1; numeric digits m.k; g= (g+x/g) * .5; end /*k*/
do k=j+5 to 0 by -1; numeric digits m.k; g= (g+x/g) *.5; end; return g || i</lang>
{{out|output|text=&nbsp; when using the internal default input:}}
numeric digits d; return (g/1)i /*make complex if X < 0.*/</lang>
{{out|output}}
<pre>
<pre>
cos(pi) = -1
cos(pi) = -1
sin(pi) = 0
sin(pi) = 0


e^(i pi) + 1 = 0 proven
e^(i pi) + 1 = 0 proven
</pre>
</pre>