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.
Do[Print[{x + y + z == 3, x + y > z}], {x, 1, 2, 1}, {y, 1, 2, 1}, {z, 1, 2, 1}]

If I run this syntax I get a series of Outputs, I am trying to Count only the number of outputs which is {True,True}

share|improve this question
1  
list = Table[{x + y + z == 3, x + y > z}, {x, 1, 2, 1}, {y, 1, 2, 1}, {z, 1, 2, 1}]; Count[list, {True, True}, Infinity]. How about using Count when you want to count? –  Öskå May 2 at 10:48

4 Answers 4

Just use Table instead of Do and Print, then Count with an appropriate levelspec:

Count[
 Table[{x + y + z == 3, x + y > z}, {x, 2}, {y, 2}, {z, 2}],
 {True, True},
 {-2}
]
1
share|improve this answer

Here is an approach:

test = Tuples[{1, 2}, 3];
Cases[{{##1}, #1 + #2 + #3 == 3, #1 + #2 > #3} & @@@ test, {_, True, 
  True}]

yielding (as one would expect):{{{1, 1, 1}, True, True}}

or you could use:

Pick[test, And[#1 + #2 + #3 == 3, #1 + #2 > #3] & @@@ test]

To count just use Length (obviously unnecessary in this case)

share|improve this answer
2  
FYI a nice shorthand for #1 + #2 + #3 is +## –  Simon Woods May 2 at 11:00
    
@SimonWoods thank you Simon Woods: always learning –  ubpdqn May 2 at 11:03
    
@Simon out of curiosity did you learn that from me or come up with it yourself? Do you also use 1##? –  Mr.Wizard May 2 at 11:05
    
@Mr.Wizard, I think I must have learnt it from you, sorry I should have acknowledged that. I've used 1## but only for fun. –  Simon Woods May 2 at 11:32
    
@Simon I don't expect acknowledgement; I was just curious. I'm the only one I can recall seeing use that around here but I know we have a similar style. (I love it when you show me how to make my code more terse!) –  Mr.Wizard May 2 at 11:50

Sometimes, storing the whole list before counting may be inappropriate (perhaps the list is huge). In this case, this solution may be useful:

count = 0;
Do[If[And @@ {x + y + z == 3, x + y > z}, count++],
   {x, 1, 2, 1}, {y, 1, 2, 1}, {z, 1, 2, 1}
];
count

1

share|improve this answer
1  
If the list is huge, then speed might be a consideration. And[x + y + z == 3, x + y > z] should be faster; also compiling should save time. –  Michael E2 May 2 at 23:38

(Before Mr.W updates his post:)

Boole[(+##1 == 3) ~And~ (+##2 > #1)] & ~Array~ {2, 2, 2} ~Total~ -1
(* 1 *)

or

cnt = 0; (+##1 == 3)&&(+##2 > #1) & ~Array~ {2, 2, 2}//.{True :> cnt++}; cnt

or

cnt = 0; (cnt += Boole[+##1 == 3 && +##2 > #1]) & ~Array~ {2, 2, 2}; cnt
share|improve this answer
    
I edited your post to make the infix more readable, as I always recommend. Also I don't believe Rule was correct so I replaced it. +1 for syntax fun. p.s. ##1 and #1 can be written ## and #, for those who like to save characters. p.p.s. && and :> are already infix so there's not much point in writing out ~And~ or ~RuleDelayed~, other than being silly. ;-) –  Mr.Wizard May 3 at 2:01

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.