i wish to print sll the combinations of digits of a number
eg 123 gives 321 132 231 132....

class numbers
{
public static void main(String args[])
{
int num=123;
int x=num%10;
int y=num/10;
int z=100*x+y;
System.out.println(z);
}
}

this is all my brain is letting me to do.please help

so ur trying do a permutation if i not wrong (or a combination maybe). it would be a long story to ellaborate it. try to search the net for the permutation algorithm. I've done that same program you should understand the algo and try to code it. if your stucked then that's the time to seek help.

ok ill try.i have the algo in front of me ..reading

Well, I know an algorithm:

The first step is concatenating the number one time after itself.
This will give us the opportunity to easily find the first part of the possibilities.
The second step is, reverse all the possibilities we've got in the first step, and that will give us the second part of the possibilities.

I'll demonstrate this using an example:
I'll take number 123, we paste the number one time after itself, we become this: 123123, this will give us the first part of the possibilities:

We can loop through the whole thing and each time shift one digit to get the next possibility:

  • 1st possibility is: 123 ([123]123)
  • 2nd possibility is: 231 (1[231]23)
  • 3rd possibility is: 312 (12[312]3)

If we now shift one extra time, we get the number where we started with, so here we stop.

Now we just reverse all the possibilities we've got in the first step, to get the second part of the possibilities:

  • 4rd possibility: 321 (reverse of 123)
  • 5th possibility: 132 (reverse of 231)
  • 6th possibility: 213 (reverse of 312)

And now we've got all the possibilities :)

Well, I know an algorithm: ... :)

Is there a version that scales for arbitrary length, or is it just a special case for length = 3?

Is there a version that scales for arbitrary length, or is it just a special case for length = 3?

Well, I guess it works for every number.
But feel free to tell me that I'm wrong
(If you find a number where this algorithm won't operate correctly on).

Edit:: No, it doesn't work for every number :(

If you use it literally for a 4 char string you will get 8 results, when it should be 24, 5 chars = 10 results, not 60, so if there is a general version it obviously needs to be a bit more cunning then the 3-char case...

If you use it literally for a 4 char string you will get 8 results, when it should be 24, 5 chars = 10 results, not 60, so if there is a general version it obviously needs to be a bit more cunning then the 3-char case...

Well, apparently I was wrong, but it will work for numbers which consist out of three digits :)

... it will work for numbers which consist out of three digits :)

Yes, definitely. I was just hoping that it was a special case of a more general algorithm that could save all that tedious recursion, but apparently not. Interesting, anyway.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.