Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you mean the characters of the numbers (such as 1927 and 9721), there are (at least) a couple of approaches.</p> <p>If you were allowed to sort, one approach is to simply <code>sprintf</code> them to two buffers, sort the characters in the buffers, then see if the strings are equal.</p> <p>However, given your desire to <em>not</em> sort the digits, another alternative is to set up a ten-element array, with all elements initially set to zero, then process each digit in the first number, incrementing the relevant element.</p> <p>Then do the same with the second number but decrementing.</p> <p>If, at the end, it's still all zeros, the numbers were a permutation of each other.</p> <p>This is efficient in that it's an <code>O(n)</code> algorithm where <code>n</code> is the number of digits in the two numbers. The pseudo-code for such a beast would be something like:</p> <pre><code>def arePermutations (num1, num2): create array count, ten elements, all zero. for each digit in num1: increment count[digit] for each digit in num2: decrement count[digit] for each item in count: if item is non-zero: return false return true </code></pre> <p>In C, the following complete program illustrates how this can be done:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #define FALSE (1==0) #define TRUE (1==1) int hasSameDigits (long num1, long num2) { int digits[10]; int i; for (i = 0; i &lt; 10; i++) // Init all counts to zero. digits[i] = 0; while (num1 != 0) { // Process all digits. digits[num1%10]++; // Increment for least significant digit. num1 /= 10; // Get next digit in sequence. } while (num2 != 0) { // Same for num2 except decrement. digits[num2%10]--; num2 /= 10; } for (i = 0; i &lt; 10; i++) if (digits[i] != 0) // Any count different, not a permutation. return FALSE; return TRUE; // All count identical, was a permutation. } </code></pre> <p>&nbsp;</p> <pre><code>int main (int c, char *v[]) { long v1, v2; if (c != 3) { printf ("Usage: %s &lt;number1&gt; &lt;number2&gt;\n", v[0]); return 1; } v1 = atol (v[1]); v2 = atol (v[2]); if (hasSameDigits (v1, v2)) { printf ("%d and %d are permutations\n", v1, v2); } else { printf ("%d and %d are not permutations\n", v1, v2); } return 0; } </code></pre> <p>Simply pass it two (positive) numbers and, assuming they fit in a <code>long</code>, it'll tell you whether they have the same digit counts.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload