Group Abstract Group Abstract

Message Boards Message Boards

0
|
5.9K Views
|
7 Replies
|
0 Total Likes
View groups...
Share
Share this post:
GROUPS:

Listing objects under conditions

Posted 11 years ago
Attachments:
POSTED BY: Richard KAUSCH
7 Replies
Posted 11 years ago

I think this may be what you are trying to do. where the n can be any number.

f[n_] := Cases[
  Tuples[Range[n], {3}], {a_, b_, c_} /; 
   a <= b <= c && c <= a + b && IntegerQ[Sqrt[2 a^2 + 2 b^2 - c^2]/2]]


 f[10]

 {{1, 1, 2}, {1, 3, 4}, {1, 5, 6}, {1, 7, 8}, {1, 9, 10}, {2, 
  2, 4}, {2, 4, 6}, {2, 6, 8}, {2, 8, 10}, {3, 3, 6}, {3, 5, 8}, {3, 
  7, 10}, {4, 4, 8}, {4, 6, 10}, {5, 5, 6}, {5, 5, 8}, {5, 5, 10}, {6,
   8, 10}}

Paul.

I noticed that some of those results were zero, so if we want to test for the Integer part to be greater than zero we can add another test like so

f[n_] := Cases[
  Tuples[Range[n], {3}], {a_, b_, c_} /; 
   a <= b <= c && c <= a + b && c > 0 && 
    IntegerQ[p = Sqrt[2 a^2 + 2 b^2 - c^2]/2] && p > 0]

Notice I set the sqrt part to equal p and then tested the p to be greater than zero afterwards.

P

POSTED BY: Paul Cleary
Posted 11 years ago

Thank you for your answer. Indeed,I am only concerned by non zero values. But this is easily obtained by changing in your code c ? a+b into c < a+b. (in this problem which origin is geometrical a, b, c are sides of a real non flat triangle, thus the stict inequality ). Now, I would modify the code to add the value of the expression Sqrt[2 a^2 + 2 b^2 - c^2]/2] associated to each solution {a,b,c} .

My idea was: (case n=12) in the attached notebook :

f1[aInteger, bInteger, c_Integer] := If[0 < a <= b <= c < a + b, 1/2 Sqrt[2 a^2 + 2 b^2 - c^2]]

Table[If[IntegerQ[f1[a, b, c]], Print[{a, b, c, f1[a, b, c]}]], {a, 1, 12}, {b, 1, 12}, {c, 1, 12}]

This gives the asked values but not only these !!!! And for increasing values of n, time used is growing tremendously. Since the problem is now to count the solutions, I tried this :

k[{aInteger, bInteger, cInteger, nInteger}] := If[(0 < a <= b <= c < a + b && c <= n && EvenQ[c]), 1/2 Sqrt[2 a^2 + 2 b^2 - c^2]];

ll[n_Integer] := Cases[Flatten[ Table[k[{a, b, c, n}], {a, 1, n}, {b, 1, n}, {c, 1, n}]], _Integer];

and then use Count

This code is not very "economical" : it is overwhelmed by great values of n.

POSTED BY: Richard KAUSCH
Posted 11 years ago

Thank you . I do not master properly the language. In your code ,I tried to change # + #2 into #1 +#2 and that changes nothing, (??) The initial condition a ? b ? c < a+b is not fullfilled by the function LessEqual but if this one is replaced by Less, we loose all cases where a = b or b = c. What I absolutely do not see is the way your code gives a ? b ? c, because the function which operates seems to give only that every composant is less than the sum of the two others. (# ? #1 + #2) In my diophantine problem,I have in fact c necessarily even. I cannot add this condition because the third composant c is not identified with the symbol #. Do not waste your patience if my questions are childish. Thank you for your help if you take time to teach.

Best regards

POSTED BY: Richard KAUSCH
Posted 11 years ago

Sorry, I missed that the last condition is indeed <. Moreover, # is the same as #1 that is why there was no differenc after your change. Is this ok now? Don't hesitate to ask if something is still not clear.

POSTED BY: Kuba Podkalicki
Posted 11 years ago

The only question that puzzles me is that I may read the code erroneously because the condition a ? b ? c is not visible ( I understand that the code gives a ? b+c, b ? c+a , c ? a+b ). This reading must be wrong.

POSTED BY: Richard KAUSCH
Posted 11 years ago

Select applies function in second argument to each triple {a,b,c}. When we have # < #3 & @@ #& it means we check if a&lt;c in that triple. You can write it #[[1]] < #[[3]] & to skip function composition.

Ok, now in our code we are doing a<=b<=c<a+b check which checks those conditions simultaneuosly, like e.g. 1 <= 2 <= 3 < 4.

POSTED BY: Kuba Podkalicki
Posted 11 years ago

Semi automatic, brute force approach, but works for less memory consuming examples:

Select[Tuples[Range[10], {3}], # <= #2 <= #3 < # + #2 & @@ # &   ]
POSTED BY: Kuba Podkalicki
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard