Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Adding without Addition

by pryrt (Abbot)
on Feb 15, 2017 at 18:21 UTC ( [id://1182085]=note: print w/replies, xml ) Need Help??


in reply to Adding without Addition

I had started down a similar path inspired by the same node, but focusing on hex -- and stopping abruptly when I got busy on more pressing ($work-related) issues :-). I built the times and addition tables, and had started on my addition subroutine... but this is as far as I got. Posted for entertainment value only. :-)

package HexMath; # add or multiply hexadecimal my (%times, %sums); BEGIN { # create tables for my $x ( 0 .. 15 ) { my $hx = sprintf '%x', $x; for my $y ( 0 .. 15 ) { my $hy = sprintf '%x', $y; next if exists $times{$hx}{$hy}; $times{$hx}{$hy} = $times{$hy}{$hx} = sprintf '%x', $x*$y; $sums{$hx}{$hy} = $sums{$hy}{$hx} = sprintf '%x', $x+$y; } } printf "\n%4s: ", 'x '; printf "%4x ", $_ for 0..15; for my $x ( 0 .. 15 ) { my $hx = sprintf '%x', $x; printf "\n%4.4s: ", $hx; for my $y ( 0 .. 15 ) { my $hy = sprintf '%x', $y; printf '%4.4s ', $times{$hx}{$hy}; } } print "\n"; printf "\n%4s: ", '+ '; printf "%4x ", $_ for 0..15; for my $x ( 0 .. 15 ) { my $hx = sprintf '%x', $x; printf "\n%4.4s: ", $hx; for my $y ( 0 .. 15 ) { my $hy = sprintf '%x', $y; printf '%4.4s ', $sums{$hx}{$hy}; } } print "\n"; } sub add { my $answer = shift; while( defined(my $addend = shift)) { substr($answer,0,0,'0'x(length($addend)-length($answer))) if l +ength($answer) < length($addend); substr($addend,0,0,'0'x(length($answer)-length($addend))) if l +ength($addend) < length($answer); print "$answer + $addend = "; my $c = 0; for my $hexit ( 1 .. length($addend) ) { my $x = lc substr($answer, -$hexit, 1); my $y = lc substr($addend, -$hexit, 1); my $s = $sums{$x}{$y}; #my $u = printf "\n\t#%d: %s + %s = %s\n", $hexit, $x, $y, $s; } print "$answer\n" } } package main; HexMath::add('a','b','cqcq','d');

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1182085]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-03-28 12:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found