10
\$\begingroup\$

Introduction

Your task is to generate the first 1000 terms in the continued fraction representation of digit-wise sum of square root of 2 and square root of 3.

In other words, produce exactly the following list (but the output format is flexible)

[2, 6, 1, 5, 7, 2, 4, 4, 1, 11, 68, 17, 1, 19, 5, 6, 1, 5, 3, 2, 1, 2, 3, 21, 1, 2, 1, 2, 2, 9, 8, 1, 1, 1, 1, 6, 2, 1, 4, 1, 1, 2, 3, 7, 1, 4, 1, 7, 1, 1, 4, 22, 1, 1, 3, 1, 2, 1, 1, 1, 7, 2, 7, 2, 1, 3, 14, 1, 4, 1, 1, 1, 15, 1, 91, 3, 1, 1, 1, 8, 6, 1, 1, 1, 1, 3, 1, 2, 58, 1, 8, 1, 5, 2, 5, 2, 1, 1, 7, 2, 3, 3, 22, 5, 3, 3, 1, 9, 1, 2, 2, 1, 7, 5, 2, 3, 10, 2, 3, 3, 4, 94, 211, 3, 2, 173, 2, 1, 2, 1, 14, 4, 1, 11, 6, 1, 4, 1, 1, 62330, 1, 17, 1, 5, 2, 5, 5, 1, 9, 3, 1, 2, 1, 5, 1, 1, 1, 11, 8, 5, 12, 3, 2, 1, 8, 6, 1, 3, 1, 3, 1, 2, 1, 78, 1, 3, 2, 442, 1, 7, 3, 1, 2, 3, 1, 3, 2, 9, 1, 6, 1, 2, 2, 2, 5, 2, 1, 1, 1, 6, 2, 3, 3, 2, 2, 5, 2, 2, 1, 2, 1, 1, 9, 4, 4, 1, 3, 1, 1, 1, 1, 5, 1, 1, 4, 12, 1, 1, 1, 4, 2, 15, 1, 2, 1, 3, 2, 2, 3, 2, 1, 1, 13, 11, 1, 23, 1, 1, 1, 13, 4, 1, 11, 1, 1, 2, 3, 14, 1, 774, 1, 3, 1, 1, 1, 1, 1, 2, 1, 3, 2, 1, 1, 1, 8, 1, 3, 10, 2, 7, 2, 2, 1, 1, 1, 2, 2, 1, 11, 1, 2, 5, 1, 4, 1, 4, 1, 16, 1, 1, 1, 1, 2, 1, 1, 1, 2, 1, 8, 1, 2, 1,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, 2, 1, 3, 2, 9]

Challenge

The following general introduction to continued fraction is taken from the challenge Simplify a Continued Fraction.

Continued fractions are expressions that describe fractions iteratively. They can be represented graphically:

continued fraction

Or they can be represented as a list of values: [a0, a1, a2, a3, ... an]

This challenge is to find out the continued fraction of the digit-wise sum of sqrt(2) and sqrt(3), the digit-wise sum is defined as follows,

Take the digits in the decimal representation of sqrt(2) and sqrt(3), and obtain the sum digit by digit:

    1.  4  1  4  2  1  3  5  6  2  3 ...
+   1.  7  3  2  0  5  0  8  0  7  5 ...
=   2. 11  4  6  2  6  3 13  6  9  8 ...

Then only keep the last digit of the sum and compile them back to the decimal representation of a real number

    1.  4  1  4  2  1  3  5  6  2  3 ...
+   1.  7  3  2  0  5  0  8  0  7  5 ...
=   2. 11  4  6  2  6  3 13  6  9  8 ...
->  2.  1  4  6  2  6  3  3  6  9  8 ...

The digit-wise sum of sqrt(2) and sqrt(3) is therefore 2.1462633698..., and when it is expressed with continued fraction, the first 1000 values (i.e. a0 to a999) obtained are the ones listed in the introduction section.

Specs

  • You may write a function or a full program. Neither should take inputs. In other words, the function or the program should work properly with no inputs. It doesn't matter what the function or program does if non-empty input is provided.

  • You should output to STDOUT. Only if your language does not support outputting to STDOUT should you use the closest equivalent in your language.

  • You don't need to keep STDERR clean, and stopping the program by error is allowed as long as the required output is made in STDOUT or its equivalents.

  • You can provide output through any standard form.

  • This is , the lowest number of bytes wins.

  • As usual, default loopholes apply here.

\$\endgroup\$
2
\$\begingroup\$

Kotlin 1.1 script, 304 293 bytes

import java.math.BigDecimal as b
import java.math.*
val m=MathContext(1022)
var B=b(2)
var A=b((""+B.sqrt(m)).zip(""+b(3).sqrt(m)).joinToString(""){(a,b)->if(a=='.')".";else ""+(a-'0'+(b-'0'))%10})
val g=b(1).setScale(1022)
repeat(1000){println(B);A=g/(A-B);B=A.setScale(0,RoundingMode.FLOOR)}

A bit verbose unfortunately :/

Must be run with JDK 9, as sqrt was added to BigDecimal in this release. Interestingly, I couldn't find a TIO site with both Kotlin 1.1 and JDK 9 features (Ideone and repl.it both run Kotlin 1.0, which did not support destructuring in lambdas, and TIO complains that sqrt does not exist.)

Prints each element separated by a newline.

Edit (-11): moved println to the beginning of the loop body and added an additional iteration to avoid repeating the method call. An extra calculation is done, but it is not used for anything.

\$\endgroup\$
2
\$\begingroup\$

Python 2, 193 ... 179 178 bytes

d=10
u=d**2000
v=u*u
def s(n,a=d,i=9):
 while a-i:i,a=a,(a+n/a)/2
 return a
p,q,r,t=s(2*v),s(3*v),1,0
while p:t+=(p+q)%d*r;p/=d;q/=d;r*=d
for i in range(1000):print t/u;t=v/(t%u)

Try it online!

Calculating sqrt(2) and sqrt(3) to such a precision with a short code is a tough job in Python and other languages.

2000 digits is needed to ensure the expansion is correct (1020 suffices, but I'm not going to modify it because no improvement), and lines 4-6 is the integer square root.

193 > 180 : The digit-wise modulo sum is now carried by a loop instead of array manipulation

180 > 179 : Replaced the 6 occurrences of 10 using d with the cost of defining with 5 bytes, cutting 1 byte in total

179 > 178 : Just realized that a!=i can be replaced by a-i

\$\endgroup\$
1
\$\begingroup\$

Jelly, 32 bytes

ȷ*`
%¢¢²¤:
2,3×Ñ×ÑƽDS%⁵ḌÇȷСṖ:Ñ

Try it online!


Basically use fixed-point arithmetic. M may work better here, but somehow floor(HUGE_NUMBER × sqrt(2) doesn't want to evaluate for too large HUGE_NUMBER. Anyway the fixed-point division is definitely better.


Explanation:

-------
ȷ*`       Calculate the base for fixed-point arithmetic.
ȷ         Number 1000.
 *        Raise to the power of...
  `       self. (so we have 1000 ** 1000 == 1e3000) Let B=1e3000.

-------
%¢¢²¤:    Given f × B, return a number approximately (1/frac(f)) × B.
          Current value: f × B.
%¢        Modulo by B. Current value: frac(f) × B.
  ¢²¤     B² (that is, 1e6000)
     :    integer-divide by. So we get B²/(frac(f)×B) ≃ 1/frac(f) × B.

-------
2,3×Ñ×ÑƽDS%⁵ḌÇȷСṖ:Ñ  Main link.
2,3                    The list [2,3].

    Ñ                  This refers to the next link as a monad, which is the
                       first link (as Jelly links wraparound)
   ×                   Multiply by. So we get [2,3]×1e3000 = [2e3000,3e3000]
     ×Ñ                Again. Current value = [2e6000,3e6000] = [2B²,3B²]

       ƽ              Integer square root.
                       Current value ≃ [sqrt(2B²),sqrt(3B²)]
                                     = [B sqrt(2),B sqrt(3)]

         DS            Decimal digits, and sum together.
           %⁵          Modulo 10.
             Ḍ         Convert back from decimal digits to integer.

                С     Repeatedly apply...
              Ç          the last link...
               ȷ         for 1000 times, collecting the intermediate results.
                  Ṗ    Pop, discard the last result.
                   :Ñ  Integer divide everything by B.
\$\endgroup\$
  • \$\begingroup\$ Unfortunately ×⁺Ñ doesn't work. Alternatively ×Ѳ$. \$\endgroup\$ – user202729 Mar 6 '18 at 4:30
  • \$\begingroup\$ Upvoted. Explanation would be much appreciated. \$\endgroup\$ – Weijun Zhou Mar 6 '18 at 4:34
  • 1
    \$\begingroup\$ @WeijunZhou Done, tell me if you don't understand something. \$\endgroup\$ – user202729 Mar 6 '18 at 4:50
1
\$\begingroup\$

Haskell 207 bytes

I Couldn't find an easy way to compute the continued fraction lazilly, so I worked as well with 2000 digits.

import Data.Ratio
r#y|x<-[x|x<-[9,8..],r>(y+x)*x]!!0=x:(100*(r-(y+x)*x))#(10*y+20*x)
c r|z<-floor r=z:c(1/(r-z%1))
main=print.take 1000.c$foldl1((+).(10*))(take 2000$(`mod`10)<$>zipWith(+)(3#0)(2#0))%10^1999
\$\endgroup\$
  • \$\begingroup\$ What a pity! I was expecting to see a Haskell answer that generates the infinite list and evaluate it lazily ... \$\endgroup\$ – Weijun Zhou Feb 26 '18 at 14:22
  • \$\begingroup\$ @WeijunZhou I will give it a try later when I have some time. At least sqrt generates infinite list. I just need to figure out how to invert decimal number written as infinte list. Maybe someone can help \$\endgroup\$ – Damien Feb 26 '18 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.