Perl script to find two numbers that for a given Number gives the same number when added.

my @words =(1,2,3,45,6,7,34,6,2,3,56,-4,-32);
my $diff;
my $sum =41;
foreach my $first (@words){

$diff = $sum – $first;
if (exists {map { $_ => 1 } @words}->{$diff})
{
print “The two numbers are $first and $diff”;
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s