# Newtons Method by Recursive Quines

## Common Lisp, 2239568 66

(#1=(lambda(x p)(format t"~S~%~S"p(,x',x,(+(/ p 2)(/ p)))))'#1#1)

Now that I read the problem statement more carefully (thanks, primo!) I noticed that the first line must be the result of calculation, not that it needs to contain the result. Thus, I think my earlier attempts did not quite follow the rules. This one should.

Example use (SBCL 1.1.15):

$sbcl --script nq.lisp | tee nq2.lisp 1 ((LAMBDA (X P) (FORMAT T "~S~%~S" P (,X ',X ,(+ (/ P 2) (/ P))))) '(LAMBDA (X P) (FORMAT T "~S~%~S" P (,X ',X ,(+ (/ P 2) (/ P))))) 3/2)$ sbcl --script nq2.lisp | tee nq3.lisp
3/2
((LAMBDA (X P) (FORMAT T "~S~%~S" P (,X ',X ,(+ (/ P 2) (/ P)))))
'(LAMBDA (X P) (FORMAT T "~S~%~S" P (,X ',X ,(+ (/ P 2) (/ P))))) 17/12)
$sbcl --script nq3.lisp | tee nq4.lisp 17/12 ((LAMBDA (X P) (FORMAT T "~S~%~S" P (,X ',X ,(+ (/ P 2) (/ P))))) '(LAMBDA (X P) (FORMAT T "~S~%~S" P (,X ',X ,(+ (/ P 2) (/ P))))) 577/408)$ sbcl --script nq4.lisp | tee nq5.lisp
577/408
((LAMBDA (X P) (FORMAT T "~S~%~S" P (,X ',X ,(+ (/ P 2) (/ P)))))
'(LAMBDA (X P) (FORMAT T "~S~%~S" P (,X ',X ,(+ (/ P 2) (/ P)))))
665857/470832)
$sbcl --script nq5.lisp | tee nq6.lisp 665857/470832 ((LAMBDA (X P) (FORMAT T "~S~%~S" P (,X ',X ,(+ (/ P 2) (/ P))))) '(LAMBDA (X P) (FORMAT T "~S~%~S" P `(,X ',X ,(+ (/ P 2) (/ P))))) 886731088897/627013566048)$

### Python 53 bytes

Saved 7 bytes due to @Mukundan.

x=1.
o='print"x=%s\\no=%r;exec o"%(x/2+1/x,o)';exec o

I've simplified the formula slightly, using the following substitutions:

x-(x²-2)/(2x)
= (2x²)/(2x)-(x²-2)/(2x)
= (2x²-x²+2)/(2x)
= (x²+2)/(2x)
= (x+2/x)/2
= x/2+1/x

I hope that's not an issue.

The program proceeds in the following manner:

$python newton-quine.py x=1.5 o='print"x=%s\\no=%r;exec o"%(x/2+1/x,o)';exec o$ python newton-quine.py
x=1.41666666667
o='print"x=%s\\no=%r;exec o"%(x/2+1/x,o)';exec o

$python newton-quine.py x=1.41421568627 o='print"x=%s\\no=%r;exec o"%(x/2+1/x,o)';exec o$ python newton-quine.py
x=1.41421356237
o='print"x=%s\\no=%r;exec o"%(x/2+1/x,o)';exec o

etc.

## CJam, 20 bytes

1
{\d_2/[email protected]/+p"_~"}_~

Try it online.

### Output

$cjam <(echo -e '1\n{\d_2/[email protected]/+p"_~"}_~'); echo 1.5 {\d_2/[email protected]/+p"_~"}_~$ cjam <(cjam <(echo -e '1\n{\d_2/[email protected]/+p"_~"}_~')); echo
1.4166666666666665
{\d_2/[email protected]/+p"_~"}_~
$cjam <(cjam <(cjam <(echo -e '1\n{\d_2/[email protected]/+p"_~"}_~'))); echo 1.4142156862745097 {\d_2/[email protected]/+p"_~"}_~$ cjam <(cjam <(cjam <(cjam <(echo -e '1\n{\d_2/[email protected]/+p"_~"}_~')))); echo
1.4142135623746899
{\d_2/[email protected]/+p"_~"}_~

### How it works

1       " Push the initial guess.                                                 ";
{       "                                                                         ";
\d    " Swap the code block with the initial guess and cast to Double.          ";
_2/   " Duplicate the initial guess and divide the copy by 2.                   ";
[email protected]/   " Push 1, rotate the initial guess on top and divide.                     ";
+p    " Add the quotients and print.                                            ";
"_~"  " Push the string '_~'.                                                   ";
}       "                                                                         ";
_~      " Duplicate the code block (to leave a copy on the stack) and execute it. ";