# How I can find a code to find (m) number form (1, n) and their sum must be different at each time.

87 views (last 30 days)
Rami Ahmed on 25 Sep 2021 at 9:53
Edited: Image Analyst on 26 Sep 2021 at 14:40
Ex : if the n = 5 and m = 4, which mean I want 4 number from (1,5) . The numbers appear are 1,2,3,4 and the sum is : 1+2=3 1+3=4 1+4=5 2+3=5 2+4=6 3+4=7 Clearly that the sum 5 is repeated so these number are rejected.

Image Analyst on 25 Sep 2021 at 14:01
Edited: Image Analyst on 25 Sep 2021 at 14:04
I know it seems obvious but did you try a simple for loop? I assume you did and got something like
n = 5;
m = 4;
numbers = linspace(1, n-1, m) % 4 numbers from 1 to m-1 (4)
for k = 1 : m
num1 = numbers(k);
for k2 = k+1 : m
num2 = numbers(k2);
fprintf('%.1f + %.1f = %f\n', num1, num2, num1+num2);
end
end
numbers =
1 2 3 4
1.0 + 2.0 = 3.000000
1.0 + 3.0 = 4.000000
1.0 + 4.0 = 5.000000
2.0 + 3.0 = 5.000000
2.0 + 4.0 = 6.000000
3.0 + 4.0 = 7.000000
but I'm wondering why this was no good. Did you need something more "sophisticated" for some reason?
Image Analyst on 26 Sep 2021 at 14:39
Again, you can call round(). But you must realize that you cannot divide a range of n long into m segments without the segments being fractional, except for certain special cases. Like I said:
n = 5;
m = 4;
numbers = linspace(1, n, m)
numbers = 1×4
1.0000 2.3333 3.6667 5.0000
numbers = unique(round(linspace(1, n, m)))
numbers = 1×4
1 2 4 5