Continued fraction/Arithmetic/G(matrix ng, continued fraction n)

Revision as of 12:03, 6 February 2013 by Nigel Galloway (talk | contribs) (Created page with "This task investigates mathmatical operations that can be performed on a single continued fraction. This requires only a baby version of NG: : <math>\begin{bmatrix} a_1 & a ...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This task investigates mathmatical operations that can be performed on a single continued fraction. This requires only a baby version of NG:

Continued fraction/Arithmetic/G(matrix ng, continued fraction n) is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

I may perform perform the following operations:

Input the next term of N1
Output a term of the continued fraction resulting from the operation.

I output a term if the integer parts of and are equal. Otherwise I input a term from N. If I need a term from N but N has no more terms I inject .

Ruby

<lang ruby> =begin

 I define a class to implement baby NG
 Nigel Galloway February 6th., 2013

=end class NG

 def initialize(a1, a, b1, b)
   @a1 = a1; @a = a; @b1 = b1; @b = b;
 end
 def ingress(n)
   t=@a; @a=@a1; @a1=t + @a1 * n; t=@b; @b=@b1; @b1=t + @b1 * n;
 end
 def needterm?
   return true if @b1 == 0 or @b == 0
   return true unless @a/@b == @a1/@b1
   return false
 end
 def egress
   n = @a/@b
   t=@a; @a=@b; @b=t - @b * n; t=@a1; @a1=@b1; @b1=t - @b1 * n;
   return n
 end
 def egress_done
   if needterm? then @a=@a1; @b=@b1 end
   return egress
 end
 def done?
   if @b1 == 0 and @b == 0 then return true else return false end
 end

end </lang>