2009/7/10 Shawn H. Corey <shawnhco...@gmail.com>:
> On Fri, 2009-07-10 at 09:26 +0100, Dermot wrote:
>> The algorithm works perfectly but my understanding of it's workings is amiss.
>>
>> When I look at this I see $E initialised and then concatenate with the
>> the modulus of
>> 37 % 2 =1
>> 18 % 2 = 0
>> 9 % 2 = 1
>> 4 % 2 = 0
>> 2 % 2 = 0
>>
>> That by my reckoning is 10100. Almost the reverse of the answer but I
>> am obviously wrong and I can't see how the final expressions:
>>
>> my $E = binary($k);
>> return $E . $b;
>>
>> work to give the answer.
>>
>> Can someone more enlightened than me give me some guidence.
>
> Perhaps it would be easier to understand if we look at the counterpart
> to this.

I appreciate what your saying but I can't say I find the counterpart
to be more helpful  so I am going to stick to the binary if it's all
the same.

> #!/usr/bin/perl
>
> my $dstr = decimal( 123456789 );
> print "$dstr\n";
>
> sub decimal {
>  my ($n) = @_;
>  return $n if $n < 10;
>  my $k = int($n/10);
>  my $b = $n % 10;
>  my $E = decimal($k);
>  return $E . $b;
> }
> __END__
>
> When dealing with the number 123456789, the first time through, stopping
> just before the sub is called a second time, we have:
>
> $b = 123456789 % 10 = 9;
> $k = int( 123456789 / 10 ) = 12345678;
>
> $E will be assigned the string that represents $k.

Why is $E getting assigned the value from $k? $E is initialised and
then assigned the return value of the binary(18) during the first
invocation (0). Does the subroutine continue and concatenate $b to $E
and then return (1) ? or does it wait until binary exhausts $n?

In my own groping/nonscientific sort of way, what I see emerging is a
pattern where my result (10100) is nearly the reverse of the correct
answer (100101) minus the leading 1. If that is correct I don't know
why the string is reversed,

Clearly, this must
> be concatenated before $b in the returned string.  The binary version
> works the same way.

Lost you a bit there because I can't see $k being concatenated to $E.
Dp.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to