HP Forums

Full Version: Brain smoker ...
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This was my challenge of the day:

Find the sum of the absolute values of the products, of all the pairs of numbers, where one is drawn from each of two lists:

Example
a:={2,-3}
b:={4,-5}

This is the goal: (Absolute values of the above): 2*4 + 2*5 +3*4 +3*5 = 45

I tried a few things and came up with this: sum(mat2list(map(a,(a)->ABS(a*b))))

which seems to be my best effort (so far!) If you feel so inclined, can you find other (hp prime) ways, perhaps more efficient in some way?

-Dale-
Nice challenge, it helped me to learn a few things about the Prime !!
Here's what I've found:
  1. first a slightly shorter variation on your excellent solution: sum(map(a,(a)->sum(|a*b|)))
  2. and another way to get to the result: sum(EXECON("sum(abs((&1*b))",a))
The second one is a bit longer but works in both HOME and CAS while the first one works only in CAS.
How about
Code:
sum_list(|a|) * sum_list(|b|)
where I've typed sum_list because I don't know how to type sigma? Or have I missed something?

Nigel (UK)
(12-12-2014 09:18 PM)Nigel (UK) Wrote: [ -> ]How about
Code:
sum_list(|a|) * sum_list(|b|)
where I've typed sum_list because I don't know how to type sigma? Or have I missed something?

Nigel (UK)

sum(abs(a))*sum(abs(b)) will do what you proposed. The multiplication described seems like a simple distribution (unless I misread the original post)
\[
(a_1 + a_2 + a_3 + \dotsm + a_n) ( b_1 + b_2 + b_3 + \dotsm + b_m )
=\]
\[ a_1 \cdot ( b_1 + b_2 + b_3 + \dotsm + b_m ) + a_1 \cdot
( b_1 + b_2 + b_3 + \dotsm + b_m ) + \dotsm
+ a_n \cdot ( b_1 + b_2 + b_3 + \dotsm + b_m ) \]
but with absolute values of each term.
Nigel idea is excellent Big Grin

Another way,less efficient :

Code:

ΣLIST(|a*b|+|REVERSE(a)*b|)
or
ΣLIST(|a|*(|b|+REVERSE(|b|)))
(12-12-2014 11:10 PM)Gilles Wrote: [ -> ]Nigel idea is excellent Big Grin

Another way,less efficient :

Code:

ΣLIST(|a*b|+|REVERSE(a)*b|)
or
ΣLIST(|a|*(|b|+REVERSE(|b|)))

Are you sure this works for any arbitrary lists a and b?
(12-13-2014 12:53 AM)Han Wrote: [ -> ]
(12-12-2014 11:10 PM)Gilles Wrote: [ -> ]Nigel idea is excellent Big Grin

Another way,less efficient :

Code:

ΣLIST(|a*b|+|REVERSE(a)*b|)
or
ΣLIST(|a|*(|b|+REVERSE(|b|)))

Are you sure this works for any arbitrary lists a and b?

Any arbitrary list...of 2 elements.
Several different methods, all accomplishing the objective. Han's breakdown, as a simple distribution, seems to be the easiest, human readable, and concise formula. Each of the other solutions provide very novel approaches, and provide a great way to explore some of the inner reaches of the prime.

This challenge was a spinoff of a problem posed for a Python procedure, using list comprehension. After finally getting a suitable Python solution, I couldn't resist the temptation to find a similar hp prime result.

All told, this is a neat example of distributed processing, in both human and hardware! Thanks for all the great responses!

-Dale-
Reference URL's