Post Reply 
Brain smoker ...
12-12-2014, 02:20 PM (This post was last modified: 12-13-2014 10:37 AM by DrD.)
Post: #1
Brain smoker ...
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-
Find all posts by this user
Quote this message in a reply
12-12-2014, 04:25 PM
Post: #2
RE: Brain smoker ...
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.
Find all posts by this user
Quote this message in a reply
12-12-2014, 09:18 PM
Post: #3
RE: Brain smoker ...
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)
Find all posts by this user
Quote this message in a reply
12-12-2014, 10:07 PM (This post was last modified: 12-12-2014 10:08 PM by Han.)
Post: #4
RE: Brain smoker ...
(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.

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
12-12-2014, 11:10 PM (This post was last modified: 12-12-2014 11:28 PM by Gilles.)
Post: #5
RE: Brain smoker ...
Nigel idea is excellent Big Grin

Another way,less efficient :

Code:

ΣLIST(|a*b|+|REVERSE(a)*b|)
or
ΣLIST(|a|*(|b|+REVERSE(|b|)))
Find all posts by this user
Quote this message in a reply
12-13-2014, 12:53 AM
Post: #6
RE: Brain smoker ...
(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?

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
12-13-2014, 08:58 AM
Post: #7
RE: Brain smoker ...
(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.
Find all posts by this user
Quote this message in a reply
12-13-2014, 10:37 AM
Post: #8
RE: Brain smoker ...
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-
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)