g | x | w | all
Bytes Lang Time Link
044APLNARS251003T090658ZRosario
057Ruby210930T084223ZG B
038Mathematica210930T063842ZBhoris D
066Factor + math.continuedfractions math.unicode210930T030848Zchunes
014Husk210929T234021ZDominic
047JavaScript ES7210929T220253ZArnauld

APL(NARS), 44 chars

{(≢k)√∣×/k←⍺{(⍺≤0)∨∞≤∣⍵:⍬⋄m,(⍺-1)∇÷⍵-m←⌊⍵}⍵}

Input of the function one float number ⍵, and one positive integer ⍺ and as output one float.

test:

  K←{(≢k)√∣×/k←⍺{(⍺≤0)∨∞≤∣⍵:⍬⋄m,(⍺-1)∇÷⍵-m←⌊⍵}⍵}
  1 K ○1
3
  2 K ○1
4.582575695
  3 K ○1
6.804092116
  7 K ○1
5.11798558
  7 K -○1
5.216552971
  15 K *1
1.815616335
  15 K -*1
1.91647393
  p←1.61803398874989
  20 K p
1
  20 K -p
1.071773463

We define the functions:

  cf←{(⍺≤0)∨∞≤∣⍵:⍬⋄m,(⍺-1)∇÷⍵-m←⌊⍵}
  I←{1=≢⍵:↑⍵⋄+∘÷/⍵}

'cf' is the function that has Input ⍺ (positive integer) and a float ⍵, and output one array of integers of lenght ⍺ that would be the continue fraction expansion of ⍵.

The 'I' function has as input one array of integers numbers (one continue fraction) and as output the number float that contiune fraction represent. Do you note that here

  1÷0
∞
  9999>∞
0

so the division of a number by zero is ∞, and ∞ it is one number that is >= of all other numbers, and cf can terminate before reach the number term ⍺, for example

  10 cf 1.125
1 8 
  I 1 8
1.125

because is found one ∞ as cf ⍵ argument. Note cf terminate when ⍺≤0 or when argument ⍵, is ∞ (not when is 0).

For expansion ⍺ terms of ⍵ float, I suppose that ⍵ has at last ⍺ significative digits, because it seems to me that the ⍵-⌊⍵ means 1 digit lost, so repeated that operation ⍺ times it means ⍺ digits lost. So because here normal Apl float are 64 bits, has to be

 (⍺d⍵)≤64

Where the function d is:

d←{3.322×⍺+1+⌊10⍟1⌈∣⍵}

that has one input one float point as ⍵, and as ⍺ the digits of precision of the number ⍵ in base 10. And as output the necessary digits in base 2 of that number ⍵ of precision ⍺ for store that number in memory (that is the lenght in bit of that number in memory). All example numbers of the question seems ok but the last 2 I would say not.

  20 d 1.61803398874989
69.762

so the last 2 cases for me have continue fraction expansions that has need at last 70 bits, 64 bits are not enought.

But result even in that case seems ok using float 128 bit vtype we can check

  20 K 1.61803398874989v
1 
  20 K ¯1.61803398874989v
1.071773463 

One pheraps better function would be KK function, has as input ⍺ the number of expansion and as input ⍵ one vtype float. As output return ∅ in the error case, it is "not a number" (I see ∅ type, as number type), or a float vtype number.

⎕fpc is the float point bits lenght of vtype float.

  KK←{⎕fpc<⍺d⍵:∅⋄⍺{(≢k)√∣×/k←⍺{(⍺≤0)∨∞≤∣⍵:⍬⋄(⌊⍵),(⍺-1)∇÷⍵-⌊⍵}⍵}⍵}
  ⎕fpc
128
  ⎕fpc÷3.322
38.53100542
  50⍕39 KK ○1v
∅
  50⍕38 KK ○1v
∅
  50⍕37 KK ○1v
3.165275511414812818221962911542966002409

○1v is π approssimate in a float of vtype ⎕fpc bits.

Ruby, 57 bytes

->n,l,r=1{n=n.abs;l.times{r*=n.to_i**(1.0/l);n=1/n%=1};r}

Try it online!

Mathematica, 42 38 bytes

N@1##&@@ContinuedFraction[#2,#]^(1/#)&

Corrected formatting and size reduced by @theorist.

Inputting n=100 and variable as Pi we get the output as

2.69405

You can save 2 bytes by removing N@ but this will give an exact expression and not numeric.

The code also passes all the tests giving the exact value for each number except for -Pi for which it returns a complex number. However the magnitude of this complex number is exactly the same as that given in the expected value.

Factor + math.continued-fractions math.unicode, 66 bytes

[ 1vector over [ dup next-approx ] times 1 head* Π abs nth-root ]

Try it online!

Explanation

It's a quotation (anonymous function) that takes an integer signifying the number of terms to use and a real number and returns a real number. Assuming 3 3.141592653589793 is on the data stack when this quotation is called...

Snippet Comment Data stack (top on right)
1vector Make a vector out of the object on top of the data stack. This is how next-approx expects to take its input. 3 V{ 3.141592653589793 }
over Put a copy of the object second from the top on top of the data stack. 3 V{ 3.141592653589793 } 3
[ dup next-approx ] Push a quotation to the data stack for times to use later. 3 V{ 3.141592653589793 } 3 [ dup next-approx ]
times Take an integer and a quotation and call the quotation that many times. In this case, equivalent to dup next-approx dup next-approx dup next-approx
Inside the quotation now... 3 V{ 3.141592653589793 }
dup Copy the top data stack object 3 V{ 3.141592653589793 } V{ 3.141592653589793 }
next-approx Add the next term in the continued fraction to our vector. next-approx has stack effect ( seq -- ) so we made a copy so we don't lose it 3 V{ 3 7.062513305931052 }
dup next-approx Iteration 2 3 V{ 3 7 15.9965944066841 }
dup next-approx Iteration 3 3 V{ 3 7 15 1.003417231015 }
1 head* Remove last element 3 V{ 3 7 15 }
Π Take the product 3 315
abs Take the absolute value 3 315
nth-root Take the nth root of a number. In this case, take the cube root of 315 6.804092115953367

Husk, 14 bytes

^\¹Π↑¡§,o\%1⌊²

Try it online! for the last test case (or try it here for n=7, number=pi).

     ¡          # Apply function repeatedly to first results, 
                # collecting second results into infinite list:
      §,     ²  #  combine pair of results of functions applied to arg 2:
            ⌊   #   floor
        o\%1    #   reciprocal of fractional part
    ↑           # Now take arg1 elements from list,
   Π            # calculate the product,
^               # and raise to the power of
 \¹             # reciprocal of arg1

JavaScript (ES7), 47 bytes

Expects (n)(real).

n=>v=>(g=n=>n?~~v*g(n-1,v=1/(v%1)):1)(n)**(1/n)

Try it online!

Note: With 40+ terms, the last test case will diverge from 1 because of cumulated floating point errors.