gnerates number that has digits from 1 to 9 are not repeated

1 view (last 30 days)
I want a code to generate all the numbers from 1234569 to 987654321
that has the digits 1 to 9 unrepeated
the number of those number should be 9! = 362880 ?
Also: how to generate all 8 digits integers (allowed digits from 1 to 9)?

Answers (3)

Fangjun Jiang
Fangjun Jiang on 5 Jun 2011
Use the perms() function. I need to confirm later whether the last two lines have the right syntax.
a=perms(1:9);
b=char(a);
c=str2num(b);

Matt Fig
Matt Fig on 5 Jun 2011
Are you sure about those equations? Because I get that there is no solution...
P = perms('123456789');
N = (str2num(P(:,4:5))+str2num(P(:,6:7)));
idx = ((str2num(P(:,1:2)).*str2num(P(:,3))) == N) & (N == str2num(P(:,8:9)))
find(idx) % EMPTY!
  4 Comments
Matt Fig
Matt Fig on 6 Jun 2011
@Fangjun, my answer was trying to find the solution to the equation given. I interpret the equation as meaning that we should take the first two digits as one number, multiply that by the third digit and get an value equal to the fourth and fifth digit taken as one number plus the sixth and seventh digits taken as one number. This should also be equal to the eighth and ninth digits taken as one number....
There were no solutions to this set of equations, which is why I asked if they were correct. It was my understanding that the whole question was to find the solution to this set of equations.
Fangjun Jiang
Fangjun Jiang on 6 Jun 2011
I see. So that's why he asked about the permutation. Brute force attack. Interesting. Okay, at least I learned that my code can be simplified as str2num(perms('123')).

Sign in to comment.


Fangjun Jiang
Fangjun Jiang on 6 Jun 2011
My first answer to this question didn't really work as I imagined. Here is a confirmed answer. I reduced the number 9 to 3 to show it with ease.
a=perms(1:3)
b=mat2str(a)
c=strrep(b,' ','')
d=str2num(c)

Categories

Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!