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. #!/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. Clearly, this must be concatenated before $b in the returned string. The binary version works the same way. -- Just my 0.00000002 million dollars worth, Shawn Programming is as much about organization and communication as it is about coding. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/