[Date Index]
[Thread Index]
[Author Index]
Re: Continued Fractions
*To*: mathgroup at smc.vnet.net
*Subject*: [mg75081] Re: Continued Fractions
*From*: "Norbert Marxer" <marxer at mec.li>
*Date*: Mon, 16 Apr 2007 20:13:00 -0400 (EDT)
*References*: <evvbni$b5u$1@smc.vnet.net>
On 16 Apr., 10:22, "Wu Weidong" <ghcgw... at singnet.com.sg> wrote:
> Hi,
> I'm working on a function which takes 2 integer values as input, a and b an=
> d outputs a list of integers corresponding to the continued fraction repres=
> entation of the rational number a/b.
>
> My function is as follows:
>
> build[a_Integer,b_Integer, l_List] :== Module[{r==1,q,temp},
> While[r!==0,
> If[a>==b,
> q==Quotient[a,b];
> l==Append[l,q];
> r == a-b*q;
> a == r,
>
> temp == a;
> a == b;
> b == temp;
> ];
> ];
> l
> ];
>
> Essentially, if the fraction given is less than 1, the first number in the =
> list is 0. The function then proceeds to find the quotient of the numerator=
> and denominator (and add that to the list) and the remainder. If the remai=
> nder is less than one, it takes the reciprocal and splits it into a whole-n=
> umber part plus another fraction which will be less than 1 and repeat. It s=
> tops when the remainder is 0.
>
> When I tried to run the program, I get the following error messages:
>
> Set::shape: Lists {} and {2} are not the same shape.
>
> Set::setraw: Cannot assign to raw object 45.
>
> Set::shape: Lists {} and {2} are not the same shape.
>
> Set::setraw: Cannot assign to raw object 45.
>
> Set::shape: Lists {} and {2} are not the same shape.
>
> General::stop: Further output of Set::shape
> will be suppressed during this calculation.
>
> Set::setraw: Cannot assign to raw object 45.
>
> General::stop: Further output of Set::setraw
> will be suppressed during this calculation.
>
> However, if I just run the While part of the function with a==45, b==16, l=
> =={}, r==1 (so I can enter the while loop), I can find the right result (2,=
> 1,4,3), so I believe my algorithm should be correct. The errors only appear=
> when I put the while loop under the Module structure.
>
> What is wrong with my program?
>
> Thank you.
>
> Regards,
> Rayne
Hello
1. You have to use Set (=) not Equal (==) in your While loop.
2. You have to keep in mind that when you call build[45,16,{}] the
symbol a will be replaced by 45, b by 16 and l by {}. Now you can not
call (e.g.) 45 = r where r is some number.
If you use different symbols for a (input) and a (inside the While
loop) and replace the Equal by Set and ... your code will work. I also
eliminated the variable temp, which is not necessary.
The following code:
build[aIn_Integer, bIn_Integer] :=
Module[{l, r, q, a, b}, l = {}; r = 1; a = aIn;
b = bIn; While[r != 0,
If[a >= b, q = Quotient[a, b];
l = Append[l, q]; r = a - b*q; a = b;
b = r]; ]; l];
build[45, 16]
gives
{2, 1, 4, 3}
Best Regards
Norbert Marxer
Prev by Date:
**Re: Continued Fractions**
Next by Date:
**Re: Re: ImplicitPlot errors**
Previous by thread:
**Re: Continued Fractions**
Next by thread:
**neat way to program minimum of sum**
| |