Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Adding without Addition

by GotToBTru (Prior)
on Feb 15, 2017 at 18:06 UTC ( #1182083=CUFP: print w/replies, xml ) Need Help??

Wasn't sure if this belongs here, or in Obfuscations.

Having once run:

use strict; use warnings; use Storable; my (%table); foreach my $i (0..9) { foreach my $j ($i..9) { $table{$i}{$j} = $table{$j}{$i} = $i + $j } } store \%table, 'addition_table';

I present to you: addition!

use strict; use warnings; use Storable; my %table = %{retrieve('addition_table')}; my @problem = @ARGV; my (%matrix); foreach my $number (@problem) { my $log = 0; push @{$matrix{$log++}}, $_ for reverse (split //, $number); } my $col = 0; while (exists $matrix{$col}) { my @column = @{$matrix{$col}}; my $first = shift @column; while(scalar @column > 0) { my $second = shift @column; $first = $table{$first}{$second}; if (length($first) > 1) { $first = substr($first,-1,1); push @{$matrix{$col + 1}}, 1; } } $matrix{$col++} = $first; } printf "%s",$matrix{$col - $_} for (1..$col); print "\n";
H:\perl>perl adder.pl 1 1 H:\perl>perl adder.pl 21 14 99 6 12 152 H:\perl>perl adder.pl 999999999999999999999999999999999999999999 1 1000000000000000000000000000000000000000000

Addition tables for other number systems are left as an exercise for the (extremely bored) reader. Vaguely apropos of Multiply Hex values. I started to write a program to do multiplication and realized I needed to figure out how to add first.

But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

Replies are listed 'Best First'.
Re: Adding without Addition
by pryrt (Parson) on Feb 15, 2017 at 18:21 UTC

    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
Node Status?
node history
Node Type: CUFP [id://1182083]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (12)
As of 2019-03-19 16:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How do you Carpe diem?





    Results (92 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!