16
\$\begingroup\$

We say two positive integers are anagrams of each other if the digits in one of them (in decimal representation) can be rearranged to form the other. Leading zeros don't count. For example, 110020222 is an anagram of 212102020, but not of 221100011; neither is 22002211 even though it can be written as 022002211.

Interestingly enough, every arithmetic sequence of positive integers contains arbitrarily large sets of elements, all anagrams of each other. In this challenge, we use a special case of this fact.

Task

For this challenge, you have to write a program or function in a language of your choice, that takes as input two positive integers: k and N, and outputs N different positive integers, all of which are multiples of k and anagrams of each other.

Rules

  • You can assume N is bigger than 1.
  • Input and output can be taken in any of the standard ways.
  • Standard loopholes are forbidden.
  • Output may contain spaces and newlines.

Winning Criterion

This is , so shortest code in bytes wins.

Examples

Note that there are more than one possible output (infinitely many, in fact) given any k and N. Here are some examples:

  k  |   N   |  Possible output
-----+-------+------------------
  9  |   4   |  234
     |       |  243
     |       |  342
     |       |  432                   
-----+-------+------------------
 351 |   6   | 142857
     |       | 428571
     |       | 285714
     |       | 857142
     |       | 571428
     |       | 714285
\$\endgroup\$

8 Answers 8

21
\$\begingroup\$

Python 2, 48 bytes

lambda k,N:[k*(10**N**k+10**i)for i in range(N)]

Try it online!

For example, k=12,n=4 produces this list of numbers, written out in lines for clarity, with the ...'s hiding a big string of zeroes:

1200...0000012
1200...0000120
1200...0001200
1200...0012000

So, we have a prefix of k, followed by another k in a position that moves one place left each time, and the rest zeroes. We need the prefix because leading zeroes aren't allowed for the anagrams, so we make it so that all the outputs have the same number of digits.

We leave enough zeroes to ensure that the right string to never bump into the left one. N**k zeroes more than suffices for this, giving extremely big numbers; N+k or even N+log_10(k) would be enough. Note that Python right-associates 10**N**k as 10**(N**k).

Unfortunately this variable bound doesn't lend well to recursive solutions that update k as they go. An upper bound on how big k can be, even if enormous, would simplify this.

\$\endgroup\$
2
  • 6
    \$\begingroup\$ This is such an unexpected approach, I love it :) \$\endgroup\$ Apr 28, 2020 at 4:27
  • 1
    \$\begingroup\$ Maybe the challenge should've specified the smallest group of values that qualifies, but this would be significantly harder to find... \$\endgroup\$ Apr 28, 2020 at 14:02
6
\$\begingroup\$

J, 18 bytes

10#.[,.[}.[*=@i.@+

Try it online!

xnor's wonderful technique, adapted for J. Be sure to upvote him.

explanation

Taking n=4, k=12 as an example, we notice that...

12 0 0 0 0 0 0 0 0 0 0 0 0 12  0  0  0
12 0 0 0 0 0 0 0 0 0 0 0 0  0 12  0  0
12 0 0 0 0 0 0 0 0 0 0 0 0  0  0 12  0
12 0 0 0 0 0 0 0 0 0 0 0 0  0  0  0 12

is actually just 12 zipped with a (12 + 4) x (12 + 4) identity matrix, multiplied by 12, with the first 12 rows chopped off.

Then we convert back to numbers with 10#.

\$\endgroup\$
3
\$\begingroup\$

05AB1E, 8 bytes

m°IL<°+*

Port of @xnor's Python 2 answer, so make sure to upvote him!!

Try it online.

A more original (although extremely slow) approach, is this 11-byter:

∞*æIù.Δ€{íË

Try it online. (Will time out if \$N\geq3\$..)

Both take \$k\$ as first input and \$N\$ as second.

Explanation:

m           # Take the 2nd (implicit) input to the power of the 1st (implicit) input: N^k
 °          # Take 10 to the power that: 10^(N^k)
  IL        # Push a list in the range [1, N]
    <       # Decrease it by 1 to make the range [0, N)
     °      # Push 10 to the power for each value: [10^0, 10^1, ..., 10^{N-1}, 10^N]
      +     # Add the earlier 10^(N^k) to each
       *    # And multiply it by the second (implicit) input N
            # (after which the list is output implicitly)

∞           # Push an infinite list of positive integer: [1,2,3,...]
 *          # Multiply each by the first (implicit) input k: [k,2k,3k,...]
  æ         # Take the (lazy) powerset of that infinite list
   Iù       # And only leave the inner lists of a size equal to the second input N
     .Δ     # Find the first inner list which is truthy for:
       €{   #  Sort the digits of each integer in the list
         í  #  in descending order (since we aren't allowed to start with a leading 0)
          Ë #  And check if all are equal
            # (after which the list is output implicitly)
\$\endgroup\$
2
\$\begingroup\$

Python 2, 101 bytes

k,n=input()
l=[];m=0;s=sorted
while 1:m+=k;l+=m,;t=[x for x in l if s(`x`)==s(`m`)];n==len(t)>exit(t)

Try it online!

Generate all multiples m of k, each time checking if there are n anagrams of m in the list of current multiples. The result is printed as exit message.

\$\endgroup\$
2
\$\begingroup\$

Retina 0.8.2, 37 bytes

^.+
$*0
0(?=.*¶(.+))
$1$`$1$%'¶
¶¶.+

Try it online! Takes input in the order N, k. Uses @xnor's algorithm. Explanation:

^.+
$*0

Convert N to N zeros.

0(?=.*¶(.+))

Match each zero in turn.

$1$`$1$%'¶

Output k, the zeros to the left of this zero, k again, then the zeros to the right of this zero.

¶¶.+

Delete k.

Using Retina 1 would save 5 bytes, mostly from the use of L$ which effectively automatically delets k:

^.+
*0
L$`0(?=.*¶(.+))
$1$`$1$%'

Try it online!

\$\endgroup\$
1
\$\begingroup\$

cQuents, 12 bytes

&A(t^n^A+t^$

Try it online!

Port of xnor's Python answer. Go upvote his answer!

Explanation

              takes two inputs, A n
&             output first n terms in sequence
              each term equals:
 A(           A * (                            )
   t^n^A            10 ** n ** A
        +                        +
         t^$                       10 ** index
\$\endgroup\$
1
\$\begingroup\$

C (gcc), 61 65 bytes

Added 4 bytes to fix a bug kindly pointed out by Kevin Cruijssen.

Uses xnor's idea in his Python answer.

i;f(k,n){for(i=0;i<n;)printf("%d%0*d%0*d ",k,n+k+i,k,n+k-i++,0);}

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Glad I could help. Now everything looks fine, so +1 from me. ;) I will delete my comments now. \$\endgroup\$ Apr 28, 2020 at 17:51
1
\$\begingroup\$

APL (Dyalog Classic), 21 bytes

Port of Kevin Cruijssen's 05AB1E answer. Don't forget to upvote him!

{⍺×(10*⍵*⍺)+10*¯1+⍳⍵}

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.