(RGS 1/5) Binary multiples

33

1

(Note: the bounty should read 4 days and not 4 hours!)

A binary multiple of a positive integer k is a positive integer n such that n is written only with 0s and 1s in base 10 and n is a multiple of k. For example, 111111 is a binary multiple of 3.

It is easy to show that a positive integer has infinitely many binary multiples. See here for a construction proof of one binary multiple for each k. Multiplying by powers of 10 you get infinitely many more.

Your task

Given a positive integer k, return the smallest binary multiple of k.

Input

A positive integer k.

Output

A positive integer n, the smallest binary multiple of k.

Test cases

2 -> 10
3 -> 111
4 -> 100
5 -> 10
6 -> 1110
7 -> 1001
8 -> 1000
9 -> 111111111
10 -> 10
11 -> 11
12 -> 11100
13 -> 1001
14 -> 10010
15 -> 1110
16 -> 10000
17 -> 11101
18 -> 1111111110
19 -> 11001
20 -> 100
100 -> 100

This is so shortest submission in bytes, wins! If you liked this challenge, consider upvoting it... And happy golfing!


This is the first challenge of the RGS Golfing Showdown. If you want to participate in the competition, you have 96 hours to submit your eligible answers. Remember there is 450 reputation in prizes! (See 6 of the rules)

Otherwise, this is still a regular challenge, so enjoy!

RGS

Posted 2020-02-24T08:02:19.677

Reputation: 5 047

1

Related: One-zero dividend. I think this isn't a duplicate though because in that challenge all the 1's had to come before all the 0's and you didn't have to output the smallest instance.

– xnor – 2020-02-24T14:54:55.617

@xnor thanks! Looking forward for your submission – RGS – 2020-02-24T14:57:11.227

This is OEIS A004290.

– Giuseppe – 2020-02-25T16:39:15.273

Wow. Being in the RGS indeed attracted a lot of upvotes. – a'_' – 2020-02-27T02:20:38.437

1So which answer surprised you the most? – S.S. Anne – 2020-02-27T14:08:40.437

Answers

12

05AB1E, 6 bytes

∞b.ΔIÖ

Try it online! or verify all test cases (courtesy of @KevinCruijssen)


Explanation

∞b           - Infinite binary list
  .Δ         - Find the first value such that..
    IÖ       - It's divisible by the input

Expired Data

Posted 2020-02-24T08:02:19.677

Reputation: 3 129

Short answer, good job! Do you think you can add the TIO link with all the test cases? – RGS – 2020-02-24T13:49:46.310

@RGS Here ya go. ;)

– Kevin Cruijssen – 2020-02-24T13:51:49.230

Thanks @KevinCruijssen was going to have to go work out how to do that ;) – Expired Data – 2020-02-24T13:51:55.077

@ExpiredData Ah, shall I delete it so it'll be a learning curve for you? ;) – Kevin Cruijssen – 2020-02-24T13:52:55.200

2@KevinCruijssen nah I would have just gone and stolen it from another one of your answers – Expired Data – 2020-02-24T13:53:17.000

11

Python 2, 42 bytes

f=lambda k,n=0:n*(max(`n`)<'2')or f(k,n+k)

Try it online!


Full program, same length:

a=b=input()
while'1'<max(`b`):b+=a
print b

Try it online!

ovs

Posted 2020-02-24T08:02:19.677

Reputation: 21 408

Nice Python submission! If the full program is the same length and doesn't exceed the Recursion Depth, why don't you use it as the "main" submission? :) – RGS – 2020-02-24T13:54:04.090

8

JavaScript (ES6), 37 bytes

Looks for the smallest \$n\$ such that the decimal representation of \$p=n\times k\$ is made exclusively of \$0\$'s and \$1\$'s.

f=(k,p=k)=>/[2-9]/.test(p)?f(k,p+k):p

Try it online! (some test cases removed because of recursion overflow)


JavaScript (ES6), 41 bytes

Looks for the smallest \$n\$ such that \$k\$ divides the binary representation of \$n\$ parsed in base \$10\$.

k=>(g=n=>(s=n.toString(2))%k?g(n+1):s)(1)

Try it online! (all test cases)

Arnauld

Posted 2020-02-24T08:02:19.677

Reputation: 111 334

Cool recursive submission! +1 – RGS – 2020-02-24T13:56:01.823

8

R, 50 38 bytes

-4 bytes thanks to Giuseppe.

grep("^[01]+$",(k=scan())*1:10^k)[1]*k

Try it online!

It follows from this blog post (linked in the question) that the smallest binary multiple of \$k\$ is smaller than \$2\cdot10^{k-1}\$; this answer uses the larger bound \$k\cdot10^k\$ instead.

Creates a vector of all multiples of \$k\$ between \$k\$ and \$k\cdot10^k\$. The regexp gives the indices of those made only of 0s and 1s; select the first index and multiply by \$k\$ to get the answer.

Will time out on TIO for input greater than 8, but with infinite memory it would work for any input.

Robin Ryder

Posted 2020-02-24T08:02:19.677

Reputation: 6 625

Use grepl! 40 bytes. I came here to comment a grepl approach based on my SNOBOL answer and found your regex to be better :-) I had !grepl("^[10]+$",F) for the record.

– Giuseppe – 2020-02-24T18:38:00.417

@Giuseppe Thanks! I knew about grepl but somehow convinced myself that it led to a longer answer... Your version is cleaner! – Robin Ryder – 2020-02-24T22:07:12.830

1@Giuseppe Changed again, this time using your original regexp in another grep solution. – Robin Ryder – 2020-02-24T23:45:02.767

1Quite clever! Glad I could be of assistance with that! :-) – Giuseppe – 2020-02-25T01:27:51.193

8

MATL, 10 bytes

`@YBUG\}HM

Try it online! Or verify all test cases.

Explanation

`      % Do...while
  @    %   Push iteration index (1-based)
  YB   %   Convert to binary string (1 gvies '1', 2 gives '10,  etc).
  U    %   Convert string to number ('10' gives 10). This is the current
       %   solution candidate
  G    %   Push input
  \    %   Modulo. Gives 0 if the current candidate is a multiple of the
       %   input, which will cause the loop to exit
}      % Finally: execute on loop exit
  H    %   Push 2
  M    %   Push input to the second-last normal function (`U`); that is,
       %   the candidate that caused the loop to exit, in string form
       % End (implicit). If top of the stack is 0: the loop exits.
       % Otherwise: a new iteration is run
       % Display (implicit)

Luis Mendo

Posted 2020-02-24T08:02:19.677

Reputation: 87 464

8"Ah, why is there a bug in my program? Hm..." – Luis Mendo – 2020-02-24T09:48:56.950

I removed the bug but I don't think I fixed it :( +1!

– RGS – 2020-02-24T13:57:25.977

8

PHP, 38 bytes

while(($n=decbin(++$x))%$argn);echo$n;

Try it online!

Counts up n in binary and divides it's decimal representation by k until there is no remainder; indicating the first, smallest multiple.

640KB

Posted 2020-02-24T08:02:19.677

Reputation: 7 149

1Thanks for your PHP submission and thorough test case coverage! +1 – RGS – 2020-02-24T14:39:56.367

7

T-SQL, 57 bytes

This script is really slow when using 18 as input

DECLARE @z INT = 18

DECLARE @ int=1WHILE
@z*@ like'%[^10]%'SET @+=1PRINT @z*@

T-SQL, 124 bytes

T-SQL doesn't have binary conversion

This will execute fast:

DECLARE @ int=1,@x char(64)=0,@a int=2WHILE 
@x%@z>0or @x=0SELECT
@x=left(concat(@%2,@x),@),@a-=1/~@,@=@/2-1/~@*-~@a
PRINT @x

Try it online

t-clausen.dk

Posted 2020-02-24T08:02:19.677

Reputation: 2 874

I just can't seem to accept that SQL solves these challenges +1... What is the T- in T-SQL? – RGS – 2020-02-24T14:00:13.210

@RGS sql really is horrible for golfing, but it is the only language I know well enough to golf at this level, sql is also it is quite readable(maybe not in this case, but that is not the sql part). T stands for Transact. – t-clausen.dk – 2020-02-24T14:17:18.597

maybe it wasn't clear, but I am impressed by your submissions! I wasn't trying to put you down or anything! keep up the really interesting work :D – RGS – 2020-02-24T14:31:35.847

@RGS not to worry, I took it as a compliment. It's cool that you post so many interesting questions. – t-clausen.dk – 2020-02-24T14:42:28.997

Thanks for your feedback! I try to, at least :) See you next challenge! – RGS – 2020-02-24T14:44:59.753

7

Charcoal, 19 bytes

≔1ηW﹪IηIθ≔⍘⊕⍘粦²ηη

Try it online! Link is to verbose version of code. Explanation:

≔1η

Start at 1.

W﹪IηIθ

Repeat until a multiple of n is found, treating the values as base 10.

≔⍘⊕⍘粦²η

Convert from base 2, increment, then convert back to base 2.

η

Output the result.

Neil

Posted 2020-02-24T08:02:19.677

Reputation: 95 035

7

Haskell, 44 38 36 bytes

Saved two bytes by using filter as suggested by @ovs.

f k=filter(all(<'2').show)[0,k..]!!1

Try it online!

This checks all multiples of k and is slow for input 9 and 18.

I much prefer this version which defines the list of all "binary" numbers and searches for the first multiple of k among them. It quickly handles all test cases, but needs 52 bytes:

b=1:[10*x+d|x<-b,d<-[0,1]]
f k=[m|m<-b,mod m k<1]!!0

Try it online!

Christian Sievers

Posted 2020-02-24T08:02:19.677

Reputation: 6 366

2 bytes shorter with filter. – ovs – 2020-02-24T10:44:44.653

@ovs Thanks, that still applies to my newer version. Somehow I always feel that list comprehension must be shorter... – Christian Sievers – 2020-02-24T10:55:35.150

I really like Haskell! Really nice submission +1 – RGS – 2020-02-24T13:56:39.387

6

Python 3.8 (pre-release),, 75\$\cdots\$ 50 52 bytes

Saved 2 bytes thanks to Mukundan!!!
Added 2 bytes to fix error kindly pointed out by Giuseppe.

f=lambda k,n=1:(i:=int(f"{n:b}"))%k and f(k,n+1)or i

Try it online!

Noodle9

Posted 2020-02-24T08:02:19.677

Reputation: 2 776

you can save 2 bytes by doing f'{n:b}' instead of bin(n)[2:] Try it online!

– Mukundan – 2020-02-24T11:50:24.850

@Mukundan Clever use of f-strings - thanks! :-) – Noodle9 – 2020-02-24T11:55:42.467

5

Japt, 11 9 bytes

_¤%U}f1 ¤

Try it

Shaggy

Posted 2020-02-24T08:02:19.677

Reputation: 24 623

Really cool solution, and it is crazy fast +1! Is it fast because of the algorithm used or because of the Japt machinery? – RGS – 2020-02-24T14:01:13.243

I think this fails for 11. – my pronoun is monicareinstate – 2020-02-25T09:10:00.593

It's probably because 11 is crossed out in the program length part. – a'_' – 2020-02-28T01:41:04.097

5

W, 7 6 bytes

-1 because I realized that W has operator overloading over t.

•B⌡≡kü

Uncompressed:

*Tt!iX*

repl.it is quite slow and you need to type in the program in code.w.

Explanation

        % For every number in the range
    i   % from 1 to infinity:
     X  % Find the first number that satisfies
*       %     Multiply the current item by the input
 T      %     The constant for 10
  t     %     Remove all digits of 1 and 0 in the current item
        %     Both operands are converted to a string, just like in 05AB1E.
   !    %     Negate - checks whether it contains only 1 and 0.

      * % Multiply that result with the input (because it's the counter value).
```

a'_'

Posted 2020-02-24T08:02:19.677

Reputation: 1 099

1Thanks for your W submission! I never understood why you include some code, and then right after it you say "uncompressed" and then include some other code. What does it mean? – RGS – 2020-02-24T14:43:38.417

2The first code snippet is a compressed form of the W program, compressed in order to use up all 256 codepoints of CP437. However, there's also another non-compressed form that is human-writable (I compress my source code from on this form). The next code snippet is a human-readable non-compressed form of the W program (hence the "uncompressed"), which is part of the explanation. – a'_' – 2020-02-24T14:46:36.507

Ok, I think I understand better now. – RGS – 2020-02-24T14:48:46.580

Where is the "get smallest" part? – Jonathan Allan – 2020-02-24T19:43:36.970

The wiki says X is SW and S is swap while W is described as "keeps the specified items...", is something in the wiki wrong or have I misinterpreted or missed something? Also could you link to an online evaluator (e.g. get it on TIO)? (By the way I did not know you replied, do use @) – Jonathan Allan – 2020-02-27T00:57:53.190

1@JonathanAllan Whoops, I forgot to add that W's behavior is different with an infinity value. Nice catch. There isn't an online interpreter with a permalink for W yet, I'm waiting for Dennis to recover. – a'_' – 2020-02-27T02:11:03.310

5

Bash + Unix utilities, 52 bytes

for((n=1;n%$1;));do n=`dc<<<2dio1d$n+p`;done
echo $n

Try it online!

This counts in binary, views the resulting numbers in base 10, and stops when a multiple of the input is reached.

Mitchell Spector

Posted 2020-02-24T08:02:19.677

Reputation: 3 392

Good job! +1 for your cool submission! – RGS – 2020-02-24T14:32:32.307

5

Retina 0.8.2, 68 bytes

.+
$*1:1,1;
{`^(1+):\1+,(.+);
$2
T`d`10`.1*;
,0
,10
1+,(.+)
$1$*1,$1

Try it online! Somewhat slow so no test suite. Explanation:

.+
$*1:1,1;

Initialise the work area with n in unary, k in unary, and k in decimal.

{`^(1+):\1+,(.+);
$2

If n divides k then delete everything except the result. This causes the remaining matches to fail and eventually the loop exits because it fails to achieve anything further.

T`d`10`.1*;
,0
,10

Treat k as a binary number and increment it.

1+,(.+)
$1$*1,$1

Regenerate the unary conversion of k.

Neil

Posted 2020-02-24T08:02:19.677

Reputation: 95 035

Cool Retina submission! +1 – RGS – 2020-02-24T14:18:32.577

5

Bash + Core utilities, 40 bytes

seq $1 $1 $[10**$1]|grep ^[01]*$|head -1

Try it online!

Mitchell Spector

Posted 2020-02-24T08:02:19.677

Reputation: 3 392

Nice bash submission +1! Also harnessing the power of regexs, aren't you? – RGS – 2020-02-24T14:44:09.747

This is really nice. Is it mathematically guaranteed that the solution will be less than or equal to 10^input? – Jonah – 2020-02-28T14:03:44.387

1

@Jonah Yes, in the question, RGS posted a link to a proof that there is always some value that works, and the proof actually yields that upper bound. Here’s the link: https://mathspp.com/blog/binary-multiples

– Mitchell Spector – 2020-02-28T14:58:30.783

1@Jonah In fact, the upper bound proven is 10^input - 1 but it would take more bytes in the bash code to stop at that point. Of course, there's no harm done by producing the one extra value. – Mitchell Spector – 2020-02-28T15:39:17.107

@Jonah Sorry, the upper bound proven is the number consisting of n 1s, which is even less than I had said. – Mitchell Spector – 2020-02-28T19:49:01.333

4

Ruby, 42 40 36 bytes

->k{z=k;z+=k until z.digits.max<2;z}

Try it online!

Very slow for 18, but ultimately gets the job done.

4 bytes golfed down by G B.

Kirill L.

Posted 2020-02-24T08:02:19.677

Reputation: 6 693

Cool ruby submission! +1 – RGS – 2020-02-24T14:15:25.667

Shorter by using z=k;z+=k until z.digits.max<2;z (can't TIO right now, sorry) – G B – 2020-02-24T14:46:53.917

4

Perl 5, 21 +2 (-ap) = 23 bytes

$_+=$F[0]while/[^01]/

Run with -a and -p, input to stdin. Just keeps repeatedly adding the input for as long as the result contains anything other than digits 0 and 1.

Try it online!

andytech

Posted 2020-02-24T08:02:19.677

Reputation: 189

4

C (gcc), 80 bytes

r,m,n;b(h){for(r=0,m=1;h;h/=2)r+=h%2*m,m*=10;h=r;}f(k){for(n=1;b(++n)%k;);b(n);}

This can probably be improved some but I have yet to find a way.

Converts the integer into binary and checks if it's a multiple.

Brute-force.

Try it online!

S.S. Anne

Posted 2020-02-24T08:02:19.677

Reputation: 1 161

1Very clever how f() only needs to call b() (which assigns return value to parameter) rather than assign return value to parameter! – Noodle9 – 2020-02-24T19:52:03.313

@Noodle9 I wish I could omit the double-call but div puts the result of modulo in eax (which means eax will always be 0 after the loop). – S.S. Anne – 2020-02-24T19:54:26.717

Do you mean the modulo result is in edx? – 640KB – 2020-02-27T03:09:47.813

@640KB Yes. I didn't look up the docs beforehand. – S.S. Anne – 2020-02-27T14:03:04.483

4

Jelly,  8  7 bytes

‘¡DṀḊƊ¿

Try it online!

How?

Note that in Jelly empty lists are falsey, while other lists are truthy. Also dequeue, , is a monadic atom which removes the first item from a list, but when presented with only an integer Jelly will first convert that integer to a list by forming the range [1..n] thus yields [2..n].

‘¡DṀḊƊ¿ - Link: integer, k
      ¿ - while...
     Ɗ  - ...condition: last three links as a monad:
  D     -     decimal digits   e.g. 1410 -> [1,4,1,0]  or 1010 -> [1,0,1,0]
   Ṁ    -     maximum                       4                     1
    Ḋ   -     dequeue (implicit range of)   [2,3,4]               []
        -                                   (truthy)              (falsey)
 ¡      - ...do: repeat (k times):
‘       -     increment

For some reason, when the body of a while loop, ¿, is a dyad each iteration sets the left argument to the result and then sets the right argument to the value of the left argument, so the 6 byte +DṀḊƊ¿ does not work. (For example given 3 that would: test 3; perform 3+3; test 6; perform 6+3; test 9; perform 9+6; test 15; perform 15+9; etc...)


Previous 8:

DḂƑȧọð1#

(DḂƑ could be DỊẠ too.)

An alternative 8:

DṀ+%Ịð1#

Jonathan Allan

Posted 2020-02-24T08:02:19.677

Reputation: 67 804

1Added more commentary, I hope it helps. – Jonathan Allan – 2020-02-24T22:23:19.267

Thanks a lot! It does help. Now I get what the "implicit range" means. – RGS – 2020-02-24T22:31:11.440

4

J, 24 23 22 bytes

+^:(0<10#@-.~&":])^:_~

Try it online!

Add the number to itself + while ^:...^:_ the following is true:

(0<10#@-.~&":]) - Something other than the digits 0 and 1 appear in the stringified number.

Jonah

Posted 2020-02-24T08:02:19.677

Reputation: 8 729

1I like your submission. Can't even tell why – RGS – 2020-02-28T13:47:59.140

4

Python 3, 50 48 bytes

f=lambda k,n=0:n*({*str(n)}<={*"01"})or f(k,n+k)

Try it online!

Explanation

  • n represents the current multiple of k.
  • {*str(n)}<={*"01"} checks if n only contains digits 0 or 1. This is done by creating a set of characters of n, then checks if that set is a subset of \$\{0,1\}\$.
  • n*({*str(n)}<={*"01"}) or f(k,n+k) is arranged such that the recursive call f(k,n+k) is only evaluated when n is 0 or n is not a binary multiple of k. Here multiplication acts as a logical and.

Surculose Sputum

Posted 2020-02-24T08:02:19.677

Reputation: 317

Uhhh, really nice python answer! Good job – RGS – 2020-02-25T00:22:07.290

3

Wolfram Language (Mathematica), 49 bytes

(x=#;While[Or@@(#>1&)/@IntegerDigits@x,x=x+#];x)&

Try it online!

J42161217

Posted 2020-02-24T08:02:19.677

Reputation: 15 931

This fails on 11 (and, likely, 9) - the code should output the smallest binary multiple, and this one outputs the first one that has both zeroes and ones. 0-byte fix

– my pronoun is monicareinstate – 2020-02-24T13:58:01.973

This does not work for 3! Ping me as soon as you fix it! – RGS – 2020-02-24T14:14:11.190

@RGS Ping...... – S.S. Anne – 2020-02-24T18:10:35.300

@S.S.Anne thanks! My pinger is broken – J42161217 – 2020-02-24T19:58:57.357

3

Burlesque, 13 bytes

rimo{>]2.<}fe

Try it online!

9 & 18 do work, but take a while because they're such large multiples. So I've taken them out of tests.

ri    # Read to int
mo    # Generate infinite list of multiples
{
 >]   # Largest digit
 2.<  # Less than 2
}fe   # Find the first element s.t.

DeathIncarnate

Posted 2020-02-24T08:02:19.677

Reputation: 916

3

Java 10, 62 59 58 bytes

n->{var r=n;for(;!(r+"").matches("[01]+");)r+=n;return r;}

Try it online.

Explanation:

n->{             // Method with long as both parameter and return-type
  var r=n;       //  Result-long, starting at the input
  for(;!(r+"").matches("[01]+");)
                 //  Loop as long as `r` does NOT consists of only 0s and 1s
    r+=n;        //   Increase `r` by the input
  return r;}     //  After the loop is done, return `r` as result

This method above only works for binary outputs \$\leq1111111111111111111\$. For arbitrary large outputs - given enough time and resources - the following can be used instead (99 70 64 bytes):

n->{var r=n;for(;!(r+"").matches("[01]+");)r=r.add(n);return r;}

Try it online.

Kevin Cruijssen

Posted 2020-02-24T08:02:19.677

Reputation: 67 575

Isn't the reason the first code fails numeric type limitations, and doesn't everybody pretend they do not exist? – my pronoun is monicareinstate – 2020-02-24T13:03:45.240

@mypronounismonicareinstate You're correct. I just figured I'd add a lambda function using java.math.BigInteger instead of int/long to prove it is possible to support an arbitrary large output if you'd want to. – Kevin Cruijssen – 2020-02-24T13:05:54.930

3

C# (Visual C# Interactive Compiler), 53 bytes

I tried a recursive version, but it was longer. Can't think of a way to replace the loop with LINQ...

a=>{int r=a;while($"{r}".Any(c=>c>49))r+=a;return r;}

Try it online!

my pronoun is monicareinstate

Posted 2020-02-24T08:02:19.677

Reputation: 3 111

I don't think LINQ will be better – Expired Data – 2020-02-24T14:10:49.323

1@ExpiredData Well, that "can't think of a way" part was implicitly followed by "without doubling the byte count" :) – my pronoun is monicareinstate – 2020-02-24T14:13:23.930

It's not that far off 16 bytes more.. maybe there's a way to golf it down. – Expired Data – 2020-02-24T14:21:41.290

Thanks for your C# submission! What is the LINQ thing? – RGS – 2020-02-24T14:37:55.577

2

@RGS: Language INtegrated Query (LINQ) is an series of APIs in .Net that allow you to handle collections with a query language. It has two syntaxes: 1. SQL-like syntax 2. Fluent syntax on the IEnumerable<T> interface. See here for the official documentation: https://docs.microsoft.com/en-us/dotnet/standard/using-linq

– Malivil – 2020-02-25T16:43:50.590

3

MathGolf, 9 bytes

0ô+_▒╙2<▼

Try it online. (Test cases n=9 and n=18 are excluded, since they time out.)

Explanation:

0          # Start with 0
        ▼  # Do-while false with pop,
 ô         # using the following 6 commands:
  +        #  Add the (implicit) input-integer to the current value
   _       #  Duplicate it
    ▒      #  Convert it to a list of digits
     ╙     #  Pop and push the maximum digit of this list
      2<   #  And check if this max digit is smaller than 2 (thus 0 or 1)
           # (after the do-while, the entire stack joined together is output implicitly)

Kevin Cruijssen

Posted 2020-02-24T08:02:19.677

Reputation: 67 575

Question: if you answered 10 seconds ago, how come you already golfed it down from 10 to 9 bytes..? +1 for the mathgolf submission though – RGS – 2020-02-24T14:45:57.487

1@RGS Yeah, could have just gone with the 9-byter, haha. I golfed it while adding the explanation (thus before posting) and realizing it could be shorter. EDIT: I've removed the strike-through 10. – Kevin Cruijssen – 2020-02-24T14:46:55.623

If I golf my answer within the grace period then I never strike through because there's no way to recover the previous version. – Neil – 2020-02-26T10:25:58.460

3

Red, 52 bytes

func[n][i: 0 until[""= trim/with to""i: i + n"01"]i]

Try it online!

Red, 54 bytes

func[n][i: 0 until[parse to""i: i + n[any["0"|"1"]]]i]

Try it online!

Galen Ivanov

Posted 2020-02-24T08:02:19.677

Reputation: 13 815

@RGS You are welcome! – Galen Ivanov – 2020-02-24T15:20:57.507

3

Stax, 9 bytes

ü◘ø⌠Δ>0↔å

Port of my MathGolf answer. This is only my second Stax answer, so there might be a shorter alternative.

Try it online or try it online unpacked (10 bytes).

Explanation (of the unpacked version):

0             # Start at 0
 w            # While true without popping, by using everything else as block:
  x+          #  Add the input-integer
    c         #  Duplicate the top of the stack
     E        #  Convert it to a list of digits
      |M      #  Get the maximum of this list
        1>    #  And check that it's larger than 1
              # (after the while, the top of the stack is output implicitly as result)

Kevin Cruijssen

Posted 2020-02-24T08:02:19.677

Reputation: 67 575

Cool Stax submission +1! Was stax created by someone from CG.SE? – RGS – 2020-02-24T15:19:19.567

@RGS yep, by recursive. :)

– Kevin Cruijssen – 2020-02-24T16:44:54.460

Nice! Didn't know about that meta question. – RGS – 2020-02-24T17:20:04.720

I'm not a really good Stax golfer. Run and debug it

– a'_' – 2020-02-25T02:14:12.107

@a'_' That unfortunately won't work for inputs that are already correct, like 1, 10, 11,100, etc. – Kevin Cruijssen – 2020-02-25T08:23:15.867

2

GolfScript, 31 bytes

:i;1{).{2 base}:b~{`+}*~i%}do b

Worked for 2-15 wen I tried them out, didn't bother doing more. May post explanation later, just wanted to get a brute submission down.

Try it online!

Mathgeek

Posted 2020-02-24T08:02:19.677

Reputation: 408

Thanks for your GolfScript submission! Looks good to me +1! – RGS – 2020-02-24T17:24:17.950

2

Gaia, 10 9 bytes

⟨:$2…⁻⟩+↺

Try it online!

		| implicit input, n
⟨     ⟩		| (1) monadic link:
 :$		| dup, and get decimal digits
   2…⁻		| remove all 1s and zeros
	↺	| if the result is truthy (non-empty)
       +	| add n and repeat from (1)
		| implicitly print result.

Times out on n=9...

Gaia, 10 bytes

1⟨bdĖ⟩#ebd

Try it online!

Somewhat more interesting and also a lot faster; finds the first integer where: converting it to binary and interpreting as decimal digits are divisible by the input (and ebd converts it to the decimal form).

Giuseppe

Posted 2020-02-24T08:02:19.677

Reputation: 21 077

2

Wolfram Language (Mathematica), 40 bytes

(For[x=#,Max@IntegerDigits@x>1,x+=#];x)&

Try it online!

Ignoring maximum iteration limits, 37 bytes

#//.a_?(Max@IntegerDigits@#>1&):>a+#&

Try it online!

This will halt when the depth of 65536 is reached (i.e. if the result is larger than 65536 * (input)).

JungHwan Min

Posted 2020-02-24T08:02:19.677

Reputation: 13 290

#//.a_?(Max@IntegerDigits@#>1&):>a+#& could have worked with 37 bytes if it didn't have the built-in iteration limit of 65536. – JungHwan Min – 2020-02-24T19:52:53.760

I am pretty sure it is ok to use the 37 byte solution. Recursive Python solutions also have a recursion depth of 1000 and those are valid solutions. – RGS – 2020-02-24T22:33:08.753

2

Icon, 50 bytes

procedure f(n)
i:=seq(n,n)&0=*(i--10)
return i
end

Try it online!

seq(n,n) generates an "infinite" (seq operator doesn’t work with large integers) sequence starting at n with step n

&is conjunction - Icon evaluates the next expression and if it fails, than backtracks to the expression to its left - so generates the next integer.

i--10 finds the difference of the string representation of i with the string 10 - Icon automatically casts number to strings when an operation on strings is used.

*(1--10) finds the length of the difference of i and 10 (which is a string)

0=*(1--10) - if the length is 0, the number is composed only of 1's and 0's.

Galen Ivanov

Posted 2020-02-24T08:02:19.677

Reputation: 13 815

Another uncommon language :D – RGS – 2020-02-25T08:05:38.980

1@RGS Yes, it's true! Icon is an old language with some nice features, especially for its time. I'll write a short explanation of my code. – Galen Ivanov – 2020-02-25T08:07:12.773

2

Pyth, 10 bytes

.Bf!%s.BTQ

Try it online!

f: Filter of all integers in increasing order, starting from an input of 1. Output the first matching input.

.BT: Convert the input to binary, as a string.

s: Convert the string to a base 10, as a integer.

% ... Q: Modulo the input

!: Boolean negation - returns true only if divisible.

.B: Convert the output of the filter back to a binary string and print it.

isaacg

Posted 2020-02-24T08:02:19.677

Reputation: 39 268

Thanks for your Pyth submission! +1 – RGS – 2020-02-25T20:47:35.500

1

There is also a much less efficient 10 byter.

– FryAmTheEggman – 2020-02-26T17:21:37.910

1

SNOBOL4 (CSNOBOL4), 68 55 bytes

	N =INPUT
A	X =X + N
	X NOTANY(10)	:S(A)
	OUTPUT =X
END

Try it online!

Adds N to X; if X contains any non-binary digits, repeat. Then print X.

Giuseppe

Posted 2020-02-24T08:02:19.677

Reputation: 21 077

This feels like a very exotic submission! Thanks :D – RGS – 2020-02-24T22:34:53.130

1@RGS I like the challenge of golfing in a half-century old language. – Giuseppe – 2020-02-25T01:29:59.670

1

Jelly, 9 bytes

1BḌọɗ1#BḌ

Try it online!

A monadic link taking an integer \$n\$ and returning an integer.

Two bytes longer than @JonathanAllan’s solution, but a different approach so I still thought worth posting.

Explanation

1   ɗ1#   | Starting with 1, find the first integer for which the following is true, using n as the right argument:
 B        | - Convert to binary
  Ḍ       | - Convert from decimal digits to integer
   ọ      | - Number of times divisible by n
       B  | Convert to binary
        Ḍ | Convert from decimal digits to integer

Another alternative Jelly, 9 bytes

2*B€ḌọƇ⁸Ḣ

Try it online!

Nick Kennedy

Posted 2020-02-24T08:02:19.677

Reputation: 11 829

(now at 2 bytes longer :P ) Thanks for posting the alternative solution! – RGS – 2020-02-24T22:33:56.780

1

Perl 6, 29 bytes

{first /^[0|1]+$/,(1..*X*$_)}

Try it online!

Find the first multiple of the input that is only composed of ones and zeroes

Jo King

Posted 2020-02-24T08:02:19.677

Reputation: 38 234

Thanks for your Perl submission! Using regexs here, hun? :D – RGS – 2020-02-24T22:33:25.583

1

C (gcc), 53 48 bytes

n,m;F(k){for(n=m=k;m>1;m=m%10>1?n+=k:m/10);n=n;}

Try it online!

Explanation:

int F(int k)
{
    int n = k;
    int m = k;

    for (; m > 1;)
    {
        if (m % 10 > 1)     // no solution if least significant digit nether 0 nor 1. Try next multiple of k.
        {
            n += k;
            m = n;
        }
        else                // if the least significant digit is 0 or 1 divide by ten
        {
            m /= 10;
        }
    }

    return n;
}

Edit: removed return

xibu

Posted 2020-02-24T08:02:19.677

Reputation: 361

Thanks for your C submission! Looking good! +1 – RGS – 2020-02-25T20:48:15.850

@S.S.Anne It's cheaper than return n;. I think it's one of the C (gcc) tips. – Neil – 2020-02-26T10:30:02.447

@S.S.Anne no, it is not. If you remove the n=n the output is always 1, because the reassignment of m happens after n is updated. – xibu – 2020-02-26T12:40:29.890

@S.S.Anne Just try it online TIO

– xibu – 2020-02-26T13:00:41.137

OK, sorry. I have never seen a case where n=n; helps. I should've tested it before saying anything. – S.S. Anne – 2020-02-26T13:02:01.430

1

APL (Dyalog Unicode), 13 bytesSBCS

+⍣{∧/2>⍎¨⍕⍺}⍨

Try it online!

Straightforward algorithm: Repeat adding self until all digits are less than 2.

How it works

+⍣{∧/2>⍎¨⍕⍺}⍨  ⍝ Input: n
+⍣{        }⍨  ⍝ Repeat adding n with starting value of n, until ... is true:
       ⍎¨⍕⍺    ⍝   Extract the digits
   ∧/2>        ⍝   All of the digits are less than 2

Bubbler

Posted 2020-02-24T08:02:19.677

Reputation: 16 616

1

Japt, 9 bytes

_%U}f_Ä ¤

Try it

Embodiment of Ignorance

Posted 2020-02-24T08:02:19.677

Reputation: 7 014

1

Python 2, 42 bytes

f=lambda k,n=0:max(`n`)!='1'and k+f(k,n+k)

Try it online!

A recursive function. We terminate when the maximum character of the string representation is 1. We can't quite use '<2' instead because zero would trigger it, and we don't have a good way not to start at zero.

The larger outputs run out of recursion depth, at least with the default value. We'll also eventually get another issue with huge inputs when Python 2's backticks string representation appends an L for "long" to numbers starting with 2**63, which will always be the maximum.


Python 2, 42 bytes

n=k=input()
while'1'<max(`n`):n+=k
print n

Try it online!

A program, using a pretty straightforward loop.

xnor

Posted 2020-02-24T08:02:19.677

Reputation: 115 687

1

Haskell, 38 bytes

f x=filter(all(<'2').show)[x,x+x..]!!0

Try it online!

Explanation

We make all the multiples of x with [x,x+x..], filter them to contain only digits smaller than two filter(all(<'2').show) and then take the first one !!0

Post Rock Garf Hunter

Posted 2020-02-24T08:02:19.677

Reputation: 55 382

Really cool solution +1 Thanks for it. – RGS – 2020-02-27T22:14:49.530

You may want to compare with mine.

– Christian Sievers – 2020-02-28T12:26:56.420

1

JavaScript (Node.js), 50 bytes

(x,i=1,g=v=>v.toString(2))=>g(i)/x%1?f(x,++i):g(i)

Try it online!

chau giang

Posted 2020-02-24T08:02:19.677

Reputation: 725

Thanks for your submission! Have you checked out this JS answer? :D

– RGS – 2020-02-28T13:47:21.357

0

Julia 1.0, 38 bytes

f(k,n=k)="10"⊇repr(n) ? n : f(k,n+k)

Try it online!

wilkben

Posted 2020-02-24T08:02:19.677

Reputation: 321

1It is fairly annoying that you need spaces around ?:, isn't it? – RGS – 2020-02-28T19:35:44.080

-2

Updated

PowerShell, 31 bytes

Thank you mazzy!

[convert]::ToString($args[0],2)

Try it online!

Original:

PowerShell, 43 bytes

{[convert]::ToString($args[0],2)}.Invoke()

You would put the value in the final brackets :-) e.g {[convert]::ToString($args[0],2)}.Invoke(2)

Try it online!

I.T Delinquent

Posted 2020-02-24T08:02:19.677

Reputation: 165

3

you can save some bytes if you remove invoke. this code works well too.

– mazzy – 2020-02-26T12:18:31.667

1And thank you @mazzy <3 – I.T Delinquent – 2020-02-26T14:27:02.580

4This doesn't work in the slightest. All you're doing is converting the input integer into its binary representation, which is not what the challenge is asking for. – AdmBorkBork – 2020-02-27T17:09:33.770