13 views (last 30 days)

Hello everyone,

Can anybody help me in obtaining random integers, “a”, “b” and “c” ranging from 0 to 100, which satisfy the Law of Cosine c^2=a^2+b^2+a*b, for the fixed corresponding angle equals 120 degrees.

Image Analyst
on 21 Sep 2019

So I guess the triple nested for loop won't work because they need to be obtained from a random draw and not sequentially? Have you heard of the randi() function?

Do you have to find them ALL? Since to make sure you've tested them all, and you're using randi() instead of a triple-for-loop, you're going to have to ask for lots of them. Let's see, a 100x100x100 would take a million test, so taking them randomly might take a few hundred million, which is certainly within MATLAB's power. Then use a single for loop to test them all

N = 200000000; % or whatever.

r = randi(..........

for k = 1 : N

if r(1) + ....................do the test

end

Bruno Luong
on 23 Sep 2019

I don't think you need to check gcd(b,c)~=1, it is redundant to gcd(a,b)~=1.

For the record here is the code generate 14 primitive triples (brute force)

a=1:100;

b=(1:100)';

c=sqrt(a.^2+b.^2+a.*b);

[j,i,c]=find(mod(c,1)==0 & c<=100 & bsxfun(@gcd,a,b)==1);

abc=unique(sort([a(i)',b(j),c],2),'rows')

EDIT: optimization following Guillaume's suggestion

Guillaume
on 23 Sep 2019

Note that you can do:

[j, i, c] = find(mod(c,1)==0 & c<=100);

a = a(i)'; b = b(j);

No need for the ind2sub.

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Direct link to this comment

https://es.mathworks.com/matlabcentral/answers/481468-how-to-obtain-a-list-of-random-integers-satisfying-the-law-of-cosine#comment_748444

⋮## Direct link to this comment

https://es.mathworks.com/matlabcentral/answers/481468-how-to-obtain-a-list-of-random-integers-satisfying-the-law-of-cosine#comment_748444

## Direct link to this comment

https://es.mathworks.com/matlabcentral/answers/481468-how-to-obtain-a-list-of-random-integers-satisfying-the-law-of-cosine#comment_748446

⋮## Direct link to this comment

https://es.mathworks.com/matlabcentral/answers/481468-how-to-obtain-a-list-of-random-integers-satisfying-the-law-of-cosine#comment_748446

Sign in to comment.