gnerates number that has digits from 1 to 9 are not repeated
Show older comments
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
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
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
on 5 Jun 2011
I noticed that Namo deleted the original equation posted, which the numbers were supposed to satisfy. For those interested, the equation was:
__ * _ = __ + __ = __
Fangjun Jiang
on 6 Jun 2011
@Matt, I recall seeing that equation. I didn't even pay attention. I though I understood that he wanted to permute the digit 1 to 9 to get 9!=362880 numbers. My first answer didn't work out. I've posted a confirmed solution. Do you have a different solution for that task?
I am not following you on your answer.
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
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')).
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!