Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I want to apply Times on the elements of a list such that:

{2,3,5,7,11,13,17,19} => 
{{2,3,5,7,11,13,17,19},{6,35,143,323},{210,46189},{9699690}}

I wrote this function :

listProduct[list_] := 
 Apply[Times, Partition[list, Ceiling[Length[list]/2]]]

Nest[listProduct, {2, 3, 5, 7, 11, 13, 17, 19}, 1]

Using Nest at each level generate the desired element but I am having difficulties with combining the results. Also, I want to be able to use each element once computed.

share|improve this question
    
For what it's worth your original version is faster than the answers provided. –  Mr.Wizard Jan 10 '13 at 7:28
    
@Mr.Wizard my original version is incomplete so I asked how to combine them at once and also how to have access to them once they're computed not at the end. No answer yet. –  Mohsen Afshin Jan 10 '13 at 8:21
    
I didn't understand this part: "I want to be able to use each element once computed." Could you give an example of what you mean? –  Mr.Wizard Jan 10 '13 at 9:35
    
@Mr.Wizard, it is an iterative computation, as an example when 2*3 => 6 and then 6*35 => 210 I want have access (a simple Print here) to both 6 and 35 in the middle of computation not at the end and using [[]] over the list. –  Mohsen Afshin Jan 10 '13 at 11:33
add comment

2 Answers 2

up vote 8 down vote accepted

You can do :

listProduct2[list_] := Times @@@ Partition[list, 2, 2]

NestList[listProduct2, {2, 3, 5, 7, 11, 13, 17, 19}, 3]
(* {{2, 3, 5, 7, 11, 13, 17, 19}, {6, 35, 143, 323}, {210, 46189}, {9699690}} *)
share|improve this answer
add comment

If you don't want to count how often you have to call the function, you can use FixedPointList which automatically stops when there is nothing more to multiply

FixedPointList[
 Times @@@ Partition[#, 2, 2] &, {2, 3, 5, 7, 11, 13, 17, 19}]
(*
{{2, 3, 5, 7, 11, 13, 17, 19}, {6, 35, 143, 323}, {210, 46189}, 
 {9699690}, {}, {}}
*)

To get rid of the last two empty lists you can use Drop[..,-2]

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.