पुनरावर्ती Quines द्वारा न्यूटन विधि


32

आपका काम न्यूटन की विधि का उपयोग करके 2 के वर्गमूल की गणना करना है - एक मामूली मोड़ के साथ। आपका कार्यक्रम न्यूटन की विधि का उपयोग करके एक पुनरावृत्ति की गणना करना है, और निम्न पुनरावृत्ति के लिए स्रोत कोड को आउटपुट करना है (जो कि ऐसा करने में सक्षम होना चाहिए)।

न्यूटन की विधि विकिपीडिया पर काफी विस्तृत रूप से वर्णित है

न्यूटन विधि का उपयोग करके वर्गमूल 2 की गणना करने के लिए, आप:

  • परिभाषित करें f(x) = x^2 - 2
  • परिभाषित करें f'(x) = 2x
  • परिभाषित करें x[0] (प्रारंभिक अनुमान)= 1
  • परिभाषित करें x[n+1] = x[n] - (f[n] / f'[n])

प्रत्येक पुनरावृत्ति x [n] को दो के वर्गमूल के करीब ले जाएगी। इसलिए -

  • x[0] = 1
  • x[1] = x[0] - f(x[0])/f'(x[0]) = 1 - (1 ^ 2 - 2) / (2 * 1) = 1.5
  • x[2] = x[1] - f(x[1])/f'(x[1]) = 1.5 - (1.5 ^ 2 - 2) / (2 * 1.5) = 1.416666667
  • x[3] = x[2] - f(x[2])/f'(x[1]) = 1.416666667 - (1.416666667 ^ 2 - 2) / (2 * 1.416666667) = 1.414215686
  • और इसी तरह

आपका कार्यक्रम होगा:

  • गणना करें x[n]कि कहां हैn समय की राशि कार्यक्रम चलाने की गई है
  • स्रोत कोड को उसी भाषा में मान्य प्रोग्राम के लिए आउटपुट करें जिसे गणना करना आवश्यक है x[n+1] इस प्रश्न के समान मानदंड की और संतुष्ट ।
  • स्रोत कोड की पहली पंक्ति गणना परिणाम, ठीक से टिप्पणी की जानी चाहिए। यदि स्रोत को पहली पंक्ति में कुछ विशेष (जैसे कि शेबंग) की आवश्यकता होती है, तो परिणाम दूसरी पंक्ति में रखा जा सकता है।

ध्यान दें कि

  • आपके प्रोग्राम को आरंभिक अनुमान का उपयोग करना चाहिए x[0] = 1
  • स्टैंडर्ड कमियां लागू
  • किसी भी इन-बिल्ड पावर, स्क्वायर रूट या एक्सरोट फ़ंक्शन को मना किया जाता है
  • आपके प्रोग्राम को किसी भी इनपुट को स्वीकार नहीं करना चाहिए। यह पूरी तरह से आत्म निहित होना चाहिए।

आपका स्कोर UTF-8 बाइट्स में आपके प्रारंभिक कार्यक्रम का आकार है। सबसे कम स्कोर जीतता है।


Do we have to define the functions, or can we simplify by writing x = x-(x*x-2)/(2*x)?
Kyle Kanos

That simplification looks valid to me. As long as it performs the calculation using Newton's Method
lochok

Does the program output the approximation, or just the source code? Can it take as its input the previous solution?
Emily

It has to output the approximation (commented) on the first line, with the source code for the next iteration. The approximation may be preceded by a shebang if the language requires it. The program (nor the program it produces) must not accept any input.
lochok

जवाबों:


19

Common Lisp, 223 95 68 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)
$

I've been mostly testing with CCL, but it works similarly with both SBCL and CLISP.
jlahd

1
This is more like I was expecting. +1
primo

17

Python 60 bytes

x=1.
o='x=%s\no=%r;print o%%(x/2+1/x,o)';print o%(x/2+1/x,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='x=%s\no=%r;print o%%(x/2+1/x,o)';print o%(x/2+1/x,o)

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

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

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

etc.


I don't know if this is legal or not, but you can shorten your initial code to g="x=%s;o=%r;print o%%(x/2+1/x,o)";print g%(1.5,g) @ 50 chars.
cjfaure

@Trimsty I think it's slightly problematic that 1) it doesn't actually calculate the first iteration, and that 2) the first line doesn't contain the current result. As I understand the problem description, both the original program and later generations should satisfy these criteria.
primo

13

CJam, 20 bytes

1
{\d_2/1@/+p"_~"}_~

Try it online.

Output

$ cjam <(echo -e '1\n{\d_2/1@/+p"_~"}_~'); echo
1.5
{\d_2/1@/+p"_~"}_~
$ cjam <(cjam <(echo -e '1\n{\d_2/1@/+p"_~"}_~')); echo
1.4166666666666665
{\d_2/1@/+p"_~"}_~
$ cjam <(cjam <(cjam <(echo -e '1\n{\d_2/1@/+p"_~"}_~'))); echo
1.4142156862745097
{\d_2/1@/+p"_~"}_~
$ cjam <(cjam <(cjam <(cjam <(echo -e '1\n{\d_2/1@/+p"_~"}_~')))); echo
1.4142135623746899
{\d_2/1@/+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.                   ";
  1@/   " 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. ";

2
Well that's impressive. +1
Kyle Kanos

8

ECMAScript 6, 38 36

(f=x=>"(f="+f+")("+(x/2+1/x)+")")(1)
(f=x=>"(f="+f+")("+(x/2+1/x)+")")(1.5)
(f=x=>"(f="+f+")("+(x/2+1/x)+")")(1.4166666666666665)
(f=x=>"(f="+f+")("+(x/2+1/x)+")")(1.4142156862745097)
(f=x=>"(f="+f+")("+(x/2+1/x)+")")(1.4142135623746899)

JavaScript, 51

(function f(x){return "("+f+")("+(x/2+1/x)+")"})(1)

This is the same as the above, for older browsers.


1
Sometimes I'm just amazed how simple javascript can make things. +1
seequ

This seems to be lacking any sort of output (print, putstr, console.log, etc.).
primo

@primo - When JavaScript is run in a console, the returned value is automatically printed.
Derek 朕會功夫

@Derek朕會功夫 Very many languages can be run as REPL - this is an expression, and not a complete program. See: Standard “loopholes” which are no longer funny.
primo

1
@Derek朕會功夫 The problem description specifically asks for a program - in several places. The program supplied does nothing. Witness: i.stack.imgur.com/Te7Vf.png The above is an expression that evaluates to an expression. It has merit of its own, but it is not a program.
primo

6

Lua 129

Probably way too long, but the Lua quine sucks because the nested [[ ]] is a deprecated feature. But it works regardless:

x=1.0;x=x/2.+1./x;l=[[io.write('x=',x,';x=x/2.+1./x;l=[','[',l,']','];',l)]];io.write('x=',x,';x=x/2.+1./x;l=[','[',l,']','];',l)

It's a bit nicer to see if you add newlines instead of colons:

x=1.0
x=x/2.+1./x
l=[[io.write('x=',x,'\nx=x/2.+1./x\nl=[','[',l,']','];',l)]];io.write('x=',x,'\nx=x/2.+1./x\nl=[','[',l,']','];',l)

4

J - 102 88 bytes

This is as horrible as I'm at making quines (I will probably revise this when I get better ideas). J's floats are limited to 5 decimal places, but by replacing first line with x=:1x it would be a fraction with infinite precision.

Edit 1: I got better idea. Also added the explanation.

x=:1
((3&{.,[:":(x%2)+1%x"_),:3&}.,],{:,{:)'x=:((3&{.,[:":(x%2)+1%x"_),:3&}.,],{:,{:)'''

First few iterations:

x=:1.5
((3&{.,[:":(x%2)+1%x"_),:3&}.,],{:,{:)'x=:((3&{.,[:":(x%2)+1%x"_),:3&}.,],{:,{:)'''

x=:1.41667
((3&{.,[:":(x%2)+1%x"_),:3&}.,],{:,{:)'x=:((3&{.,[:":(x%2)+1%x"_),:3&}.,],{:,{:)'''

x=:1.41422
((3&{.,[:":(x%2)+1%x"_),:3&}.,],{:,{:)'x=:((3&{.,[:":(x%2)+1%x"_),:3&}.,],{:,{:)'''

Explanation

((3&{.,[:":(x%2)+1%x"_),:3&}.,],{:,{:)'x=:((3&{.,[:":(x%2)+1%x"_),:3&}.,],{:,{:)'''
((3&{.,[:":(x%2)+1%x"_),:3&}.,],{:,{:) The quine-function
                         3&}.,],{:,{:  Build the second row
                         3&}.          Get everything but the first 3 characters from the string
                             ,]        Get the whole string and concat
                               ,{:     Get the last item (') and concat
                                  ,{:  -||-
 (3&{.,[:":(x%2)+1%x"_)                Build the first row
       [:":(x%2)+1%x"_                 Calculate x/2 + 1/x (stolen from Pythoneer) and stringify
  3&{.                                 Take the first 3 characters from the string (x=:)
      ,                                Concatenate 'x=:' and the result
                       ,:              Concatenate the two rows

1
I actually love how simple this program is (for serious).
seequ

If I get more time, I'm going to see if I can modify the above for Kona.
Kyle Kanos

@KyleKanos At least the digit-rotation-thingy was similar enough, but I don't know Kona. Good luck! :)
seequ

1%x is the same as %x. Instead of (x%2)+1%x, you can do (%&2+%)x.
Conor O'Brien

3

Ruby, 65

x=1.0
puts"x=#{x/2+1/x}",<<'1'*2,1
puts"x=#{x/2+1/x}",<<'1'*2,1
1

As too often happens, this is almost a straight port of the Python solution.

हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.