Here are the first 100 numbers of a sequence:
1,2,33,4,55,66,777,8,99,11,111,12,133,141,1515,1,11,18,191,22,222,222,2232,24,252,266,2772,282,2922,3030,31313,3,33,33,335,36,377,383,3939,44,441,444,4443,444,4455,4464,44747,48,499,505,5151,522,5333,5445,55555,565,5757,5855,59559,6060,61611,62626,636363,6,66,66,676,66,666,770,7717,72,737,744,7557,767,7777,7878,79797,88,888,882,8838,888,8888,8886,88878,888,8898,9900,99119,9929,99399,99494,995959,96,979,988,9999,100
How does this sequence work?
n: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
binary: 1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111 10000 10001
n extended: 1 22 33 444 555 666 777 8888 9999 1010 1111 1212 1313 1414 1515 16161 17171
1-bit digits: 1 2 33 4 5 5 66 777 8 9 9 1 1 1 11 12 13 3 141 1515 1 1 1
result: 1 2 33 4 55 66 777 8 99 11 111 12 133 141 1515 1 11
As you can see, the steps to get the output are as follows:
- Convert integer \$n\$ to a binary-string.
- Extend integer \$n\$ to the same length as this binary-string. (I.e. \$n=17\$ is
10001in binary, which has a length of 5. So we extend the17to this same length of 5 by cycling it:17171.) - Only keep the digits in the extended integer \$n\$ at the positions of the
1s in the binary-string. - Join them together to form an integer†.
Challenge:
One of these options:
- Given an integer \$n\$, output the \$n^{\text{th}}\$ number in the sequence.
- Given an integer \$n\$, output the first \$n\$ numbers of this sequence.
- Output the sequence indefinitely without taking an input (or by taking an empty unused input).
Challenge rules:
- †Step 4 isn't mandatory to some extent. You're also allowed to output a list of digits, but you aren't allowed to keep the falsey-delimiter. I.e. \$n=13\$ resulting in
[1,3,3]or"1,3,3"instead of133is fine, but"13 3",[1,3,false,3],[1,3,-1,3], etc. is not allowed. - Although I don't think it makes much sense, with option 1 you are allowed to take a 0-based index \$m\$ as input and output the \$(m+1)^{\text{th}}\$ value.
- If you output (a part of) the sequence (options 2 or 3), you can use a list/array/stream; print to STDOUT with any non-digit delimiter (space, comma, newline, etc.); etc. Your call. If you're unsure about a certain output-format, feel free to ask in the comments.
- Please state which of the three options you've used in your answer.
- The input (with options 1 and 2) is guaranteed to be positive.
- You'll have to support at least the first \$[1, 10000]\$ numbers. \$n=\text{...},9998,9999,10000]\$ result in \$\text{...},9899989,99999999,10010]\$ (the largest output in terms of length within this range is \$n=8191 → 8191819181918\$).
General rules:
- This is code-golf, so shortest answer in bytes wins.
Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language. - Standard rules apply for your answer with default I/O rules, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
- Default Loopholes are forbidden.
- If possible, please add a link with a test for your code (i.e. TIO).
- Also, adding an explanation for your answer is highly recommended.
PS: For the 05AB1E code-golfers among us, 4 bytes is possible.
32 Answers 32
Wolfram Language (Mathematica), 72 bytes
Pick[Flatten[(i=IntegerDigits)/@Table[#,s=Length[p=#~i~2]]][[;;s]],p,1]&
Here is the plot of the first 30.000 such numbers
And here is the Logarithmic plot
Jelly, 4 bytes
BTịD
Try it online, code that computes the \$n\$th term of the sequence, or check the first 100 terms!
How it works:
B convert number to binary, i.e. 5 -> [1, 0, 1]
T keep the indices of the Truthy elements, i.e. [1, 0, 1] -> [1, 3]
ị and then index safely into...
D the decimal digits of the input number
By "index safely" I mean that indices out of range are automatically converted into the correct range!
-
1\$\begingroup\$ I might start considering porting this answer. Good job! \$\endgroup\$user92069– user920692020年03月18日 11:57:55 +00:00Commented Mar 18, 2020 at 11:57
-
\$\begingroup\$ Byte for byte what I tested with when assessing the sandboxed post :) \$\endgroup\$Jonathan Allan– Jonathan Allan2020年03月18日 21:22:54 +00:00Commented Mar 18, 2020 at 21:22
-
\$\begingroup\$ @JonathanAllan I hope that means I found the optimal solution and not that you think I copied your answer or something like that D: \$\endgroup\$RGS– RGS2020年03月18日 23:00:10 +00:00Commented Mar 18, 2020 at 23:00
-
\$\begingroup\$ These golfing languages never cease to amaze me \$\endgroup\$Indiana Kernick– Indiana Kernick2020年03月18日 23:31:32 +00:00Commented Mar 18, 2020 at 23:31
-
\$\begingroup\$ I think you did find the optimal solution. I didn't post it so copying it would have been impressive! \$\endgroup\$Jonathan Allan– Jonathan Allan2020年03月19日 00:12:53 +00:00Commented Mar 19, 2020 at 0:12
Python 3, (削除) 80 (削除ここまで) \$\cdots\$ (削除) 52 (削除ここまで) 53 bytes
Saved (削除) a (削除ここまで) 2 bytes thanks to Jitse!!!
Saved (削除) 7 (削除ここまで) 6 bytes thanks to Surculose Sputum!!!
Added a byte to fix bugs kindly point out by Jitse and Surculose Sputum.
lambda n:[c for c,d in zip(str(n)*n,f'{n:b}')if'0'<d]
Returns the \$n^{\text{th}}\$ number in the sequence as a list of digits.
-
1\$\begingroup\$ @Arnauld Should be correct now. \$\endgroup\$Noodle9– Noodle92020年03月18日 11:32:44 +00:00Commented Mar 18, 2020 at 11:32
-
-
\$\begingroup\$ @Jitse Very sweet - thanks! :-) \$\endgroup\$Noodle9– Noodle92020年03月18日 11:49:27 +00:00Commented Mar 18, 2020 at 11:49
-
\$\begingroup\$ You can return a list of digits instead, which saves the cost of joining. \$\endgroup\$Surculose Sputum– Surculose Sputum2020年03月18日 12:01:59 +00:00Commented Mar 18, 2020 at 12:01
-
\$\begingroup\$ @SurculoseSputum Thanks, wasn't sure about that. Great minds think alike, yeah! :D \$\endgroup\$Noodle9– Noodle92020年03月18日 12:07:11 +00:00Commented Mar 18, 2020 at 12:07
JavaScript (ES6), 55 bytes
n=>n.toString(2).replace(/./g,(d,i)=>+d?(n+=[n])[i]:'')
Commented
n => // n = input
n.toString(2) // convert n to a binary string
.replace( // replace:
/./g, // for each
(d, i) => // digit d at position i:
+d ? // if d is '1':
(n += [n]) // coerce n to a string (if not already done)
// and double its size to make sure we have enough digits
[i] // extract the i-th digit
: // else:
'' // discard this entry
) // end of replace()
Python 2, 52 bytes
lambda n:[c for c,i in zip(`n`*n,bin(n)[2:])if'0'<i]
Input: An integer \$n\$
Output: The \$n^{th}\$ numbers in the sequence, in the form of a list of digits.
How:
bin(n)is the binary string ofn, e.gbin(2)is"0b10". Thusbin(n)[2:]is the binary string ofnwithout the0b.`n`*ncreates the n-extended string by repreating the decimal string ofnn times. This is longer than needed, but that's ok because extra characters will be truncated later.c,i in zip(`n`*n,bin(n)[2:])takes each pair of corresponding charactersc,ifrom the binary string and the n-extended string.if'0'<ichecks ifiis the character"1", if so the corresponding charactercis kept in the result list.
05AB1E, 4 bytes
×ばつIbÏ
Yeah, I found the mysterious 4-byte 05AB1E answer :D
×ばつ expand the input digits (input 123 -> 123123123123123... )
Ib get the binary value of input (123 -> 1111011)
Ï keep only the digits where the corresponding binary digit is 1
-
1\$\begingroup\$ Ah nice. I actually had
Þinstead of×, with a list of digits as output. But I actually like your approach more, since the output is a single joined string. :) \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2020年03月18日 13:08:47 +00:00Commented Mar 18, 2020 at 13:08 -
3\$\begingroup\$ Actually, I tried to find the
Þfunction, but I didn't know what to search in the info.txt :D \$\endgroup\$Dorian– Dorian2020年03月18日 13:11:23 +00:00Commented Mar 18, 2020 at 13:11
APL (dzaima/APL), 7 bytes
⊤⌿≢∘⊤⍴⍕
How it works
⊤⌿≢∘⊤⍴⍕ ⍝ Input: n
≢∘⊤ ⍝ Length of base-2 digits
⍴⍕ ⍝ Repeat the digits of n (as a string) to the length of above
⊤⌿ ⍝ Take the digits where the corresponding base-2 digit is 1
APL (Dyalog Unicode), 16 bytes
⍕(∊⊢⊆⍴⍨∘≢)2∘⊥⍣ ̄1
How it works
⍕(∊⊢⊆⍴⍨∘≢)2∘⊥⍣ ̄1 ⍝ Input: n
2∘⊥⍣ ̄1 ⍝ Binary digits of n
⍕ ⍝ Stringify n
( ) ⍝ Inner function with the two args above
∘≢ ⍝ Length of binary digits
⍴⍨ ⍝ Cycle the string digits to the length
∊⊢⊆ ⍝ Filter the digits by the binary digits
-
\$\begingroup\$ Incredibly done! Beats my Dyalog answer by eons, and I really spent a lot time on it. Also, I must admit I didn’t realize dzaima’s dialect could make it so much shorter! Which properties/operators in particular allowed you to take such a different (and superior) approach in dzaima’s APL extension? \$\endgroup\$AviFS– AviFS2020年03月19日 01:31:13 +00:00Commented Mar 19, 2020 at 1:31
-
\$\begingroup\$ @AviF.S. Actually it is the same approach. It's just that monadic
⊤in the extension means exactly2∘⊥⍣¯1, and⌿is a pure function "replicate" rather than a mixed function/operator.⊤is shared between various extensions, but pure function⌿is unique to dzaima's (which is usually the sole reason to choose the particular extension). \$\endgroup\$Bubbler– Bubbler2020年03月19日 01:37:20 +00:00Commented Mar 19, 2020 at 1:37 -
C (gcc), (削除) 101 (削除ここまで) \$\cdots\$ (削除) 96 (削除ここまで) 91 bytes
Save (削除) a (削除ここまで) 6 bytes thanks to ceilingcat!!!
b;i;f(n){char s[n];for(b=1;i=n/b;b*=2);for(;b/=2;++i)b&n&&putchar(s[i%sprintf(s,"%d",n)]);}
Outputs the \$n^{\text{th}}\$ number in the sequence.
APL (Dyalog Unicode), (削除) 25 (削除ここまで) 24 bytes
Code
{b←2∘⊥⍣ ̄1⋄(b⍵)/(⍴b⍵)⍴⍕⍵}
Explanation
{ ⍝ Start function definition
b ← 2∘⊥⍣ ̄1 ⍝ Let b ← binary conversion function
⋄ ⍝ Start new clause
(b⍵) ⍝ Binary representation of ⍵ (input)
/ ⍝ Mask boolean list over following string
(⍴b⍵) ⍝ Length of boolean representation of ⍵
⍴ ⍝ Reshape
⍕⍵ ⍝ Stringify ⍵
} ⍝ End function definition
Binary Conversion
This is all much longer than one would expect from APL and is due to the lack of a concise binary conversion function. Unfortunately, the above is the best we can do. Below is the breakdown:
- 'Power' (
⍣) does an operation n times. Sof⍣ ̄1calculates the inverse off, if it can. - 'Decode' (
⊥) converts from an arbitrary base back to decimal;2 ⊥ 1 1 0 1returns13. - 'Jot' (
∘) can compose two functions as in(f∘g) 3or curry as in(1∘+) 3.
Together, 2∘⊥⍣ ̄1 denotes the inverse of the function that converts from binary to decimal.
(Two left-curried with the encoding function, 2∘⊥, converts binary to decimal.)
-
1\$\begingroup\$ Welcome to PPCG and nice first answer! You can try converting the function into a train to save some bytes \$\endgroup\$user41805– user418052020年03月18日 14:07:03 +00:00Commented Mar 18, 2020 at 14:07
-
\$\begingroup\$ Hi, welcome to CGCC! Nice first answer. I don't know APL, but why is the entire code in the input-section instead of code section? Usually we'd have a full program or function, and the input is than taken through STDIN, System-arguments, or function arguments/parameters. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2020年03月18日 14:49:27 +00:00Commented Mar 18, 2020 at 14:49
-
\$\begingroup\$ @user41805 The
/function is often problematic in trains. \$\endgroup\$Adám– Adám2020年03月18日 14:55:25 +00:00Commented Mar 18, 2020 at 14:55 -
\$\begingroup\$ @Adám It can be circumvented with
(/⍨⍨)\$\endgroup\$user41805– user418052020年03月18日 15:06:14 +00:00Commented Mar 18, 2020 at 15:06 -
\$\begingroup\$ @user41805 Thanks! I know it looks likes it's begging to be trainified but I was unable to make it any shorter. If you can golf it further, please do comment; I'll keep looking as well as I'm not completely happy with it. \$\endgroup\$AviFS– AviFS2020年03月18日 15:47:11 +00:00Commented Mar 18, 2020 at 15:47
Ruby, 68 bytes
->n{n.to_s(2).gsub(/./).with_index{|b,i|b>?0?(n.to_s*n)[i]:""}.to_i}
Returns the nth number in the sequence.
I'm no expert golfer, so it's undoubtedly possible there's a better Ruby solution, but I'm (not altogether unpleasantly) surprised to see a challenge where Python and JavaScript both outperform Ruby. I guess python's list comprehensions are a perfect fit for this challenge, and JavaScript passing the index as a parameter to the replace method is very convenient.
-
1\$\begingroup\$
with_indexis really long -- you can save 5 bytes by working around it and then another byte by folding the test into the string multiplication \$\endgroup\$Doorknob– Doorknob2020年03月21日 02:02:37 +00:00Commented Mar 21, 2020 at 2:02
APL+WIN, 25 bytes
Prompts for integer n and outputs nth term
((b⍴2)⊤n)/(b←1+⌊2⍟n)⍴⍕n←⎕
Red, 131 bytes
func[n][i: 0 remove-each _ t: take/part append/dup t: to""n n |:
length? s: find to""enbase/base to#{}n 2"1"|[s/(i: i + 1) =#"0"]t]
PHP, (削除) 75 (削除ここまで) (削除) 86 (削除ここまで) (削除) 81 (削除ここまで) 73 bytes
for(;$i<strlen($d=decbin($a=$argn));$i++)if($d[$i])echo$a[$i%strlen($a)];
Gives the nth number.
Original version didn't handle 0's in input correctly.
05AB1E, 4 bytes
∍IbÏ
How?
∍IbÏ - Full program expecting a single input e.g. 13 stack:
∍ - extend a to length b (stack empty so a=b=input) [1313131313131]
I - push the input [1313131313131, 13]
b - convert to binary [1313131313131, 1101]
Ï - a where b is 1 [133]
- implicit output 133
-
\$\begingroup\$ I'm not used to you posting 05AB1E answers. :) Btw, not sure if you had noticed it, but it's rather similar as this existing 05AB1E answer. Of course it's fine since you came up with it independently, but figured I'd let you know in case you missed it. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2020年03月18日 21:30:35 +00:00Commented Mar 18, 2020 at 21:30
-
\$\begingroup\$ Yeah, I saw not long after I posted (I originally had
srather thanItoo, but realisedIwas more natural) I didn't noticexwhen making it either. (I found this during sandboxing BTW) \$\endgroup\$Jonathan Allan– Jonathan Allan2020年03月18日 21:50:52 +00:00Commented Mar 18, 2020 at 21:50 -
\$\begingroup\$ Well, your
∍is probably slightly better for memory usage thanxanyway, since100 100xwould be 10,000 characters long, whereas100 100∍would be 100 characters long. :) \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2020年03月18日 21:54:46 +00:00Commented Mar 18, 2020 at 21:54 -
\$\begingroup\$ Ah, reading "expand" and "123123..." I assumed it was a lazy infinite generator :) \$\endgroup\$Jonathan Allan– Jonathan Allan2020年03月18日 22:03:01 +00:00Commented Mar 18, 2020 at 22:03
-
1\$\begingroup\$ That would be
Þ;) Although it implicitly converts it to a list of characters/digits. My prepared 4-byter was actuallyÞIbÏ, but I like the approach withxor∍actually more, since the output is than a single string/number instead of a list of characters/digits. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2020年03月18日 22:10:19 +00:00Commented Mar 18, 2020 at 22:10
Perl 5 -n, 75 bytes
map{$i=0;@b=split//,sprintf"%b",$_;say@s=grep{$b[$i++]}split//,$_ x@b}1..$_
Prints the first n numbers in the sequence
Perl 5 -nl, 60 bytes
@b=split//,sprintf"%b",$_;say@s=grep{$b[$i++]}split//,$_ x@b
Shorter version that just prints the nth number
Rust, (削除) 131 (削除ここまで) 129 bytes
|n|format!("{:b}",n).chars().zip(format!("{}",n).chars().cycle()).flat_map(|(b,c)|if'0'<b{Some(c)}else{None}).collect::<String>()
(削除) Try it online! (削除ここまで)
Try it online!
Works according to option 1 and returns the \$n^{\text{th}}\$ number of the sequence as a string.
-2 thanks to Kevin Cruijssen!
Explanation
|n| // Closure taking a parameter n
format!("{:b}",n) // Binary string of n
.chars() // Iterate over the chars
.zip( // Zip iterator with
format!("{}",n) // Decimal string of n
.chars().cycle() // Cycle over the chars
)
.flat_map( // Map with the following iterator-returning
// function and flatten the result
|(b,c)| // Closure taking the char pairs (b,c)
if'0'<b{Some(c) // If '1' then return an iterator yielding c
else{None} // Else return an empty iterator
)
.collect::<String>() // Evaluate into a string
-
\$\begingroup\$ Nice answer! The
if b=='1'can apparently beif'0'<bfor -2 bytes. :) \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2021年11月06日 12:07:32 +00:00Commented Nov 6, 2021 at 12:07 -
\$\begingroup\$ @KevinCruijssen thanks for the tip! \$\endgroup\$cg909– cg9092021年11月06日 16:37:18 +00:00Commented Nov 6, 2021 at 16:37
R, 75 bytes
\(x,b=bitwAnd(x,e<-2^((log(x,2)%/%1):0))/e)(utf8ToInt(c(x,""))-48+0*b)[!!b]
A function which inputs a number \$x\$ and outputs the \$x^{\text{th}}\$ sequence member.
Binary conversion was inspired by this SO answer.
log(x,2)%/%1gives the highest bit of thexand:0is the sequence of all bits;- with the bitwise AND we find which bits are present in the
x; xis turned into a string withc(x,"")and splitted into ASCII codes withutf8ToInt;- to the vector of the decimal digits of
xis added the vector of zeroesb*0. R matches the shortest vector to have the same length as the longer one. - Zero bits are removed by subsetting:
!!0isFALSE,!!with other valuesTRUE.
C (gcc), 79 bytes
V;M;r(n){M<=V?r(n/10?:(M*=2,V)),M/=2,M&V&&putchar(n%10+48):0;}f(n){M=1;r(V=n);}
Generates the nth number
Bash + GNU utilities, 87 bytes
x=1ドル1ドル1ドル1ドル
for((b=`dc<<<2o1ドルp`;b;)){ [ $[b] = $b ]&&printf ${x:0:1};x=${x:1};b=${b:1};}
Input \$n\$ is passed as an argument, and the \$n^\text{th}\$ number in the sequence is output on stdout.
How it works:
x is set to 4 copies of the input in a row, which is more than enough digits to match the binary equivalent of the input. (A number in base 2 can never be longer than 4 times its representation in base 10, since \$\log_2(10)<4.\$)
b is initialized to the binary representation of the input.
The for loop repeats the following as long as b still has at least one 1 in it:
If
bdoesn't start with a0, then the first character inxis printed.The first character is chopped off of
xandb.
The golfiest trick is probably the way I check to see if b starts with a 1: the expression $[b] means: b evaluated as an arithmetic expression. This will omit any initial 0s (except that it will keep a final 0 if all the characters in b are 0). So [ $[b] = b ] is true iff b either starts with a 1 or is equal to "0". But b can't equal "0" since the loop termination condition would have been true in that case, and the loop would have ended already.
Java (JDK), 108 bytes
n->{var s=""+n;for(int b=n.highestOneBit(n),i=0;b>0;b/=2,i++)if((b&n)>0)System.out.print((s+=s).charAt(i));}
Credits
- -7 bytes thanks to Kevin Cruijssen
-
\$\begingroup\$ @KevinCruijssen Nice idea to repeat that way! \$\endgroup\$Olivier Grégoire– Olivier Grégoire2020年03月19日 13:46:12 +00:00Commented Mar 19, 2020 at 13:46
-
\$\begingroup\$ I agree. Not my idea btw, but Arnauld's. When he posted his answer I had to verify whether there was some integer with a binary pattern
10...0...01for which this might fail, but apparently not.9results in exactly enough digits with-> 99 -> 9999, and all integer above that concatenated four times are larger (in terms of length) than their binary-string. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2020年03月19日 13:49:04 +00:00Commented Mar 19, 2020 at 13:49
Husk, 5 bytes
fḋ1¢s
Try it online! This function works according to option 1, outputting the term as a string.
Explanation
fḋ1¢s (Let X denote the argument.)
f Select the elements of
s the string representing X,
¢ cycled infinitely, corresponding to truthy values of
ḋ1 the binary digits of X.
Japt, 8 bytes
Came up with a few approaches but couldn't do better than 8 bytes.
With output as a digit array:
¤¬ðÍ£sgX
With output as a string:
¤ËÍçEgUs
¤¬ðÍ£sgX :Implicit input of integer U
¤ :To binary string
¬ :Split
ð :Truthy indices when
Í : Converted to integer
£ :Map each X
s : Convert U to string
gX : Get digit at index X
¤ËÍçEgUì :Implicit input of integer U
¤ :To binary string
Ë :Map each D at index E
Í : Convert D to integer
ç : That many times repeat
Eg : Index E into
Uì : Digit array of U
AWK, 89 bytes
{for(f=b=1;1ドル>=b;e=e1ドル)c[d++]=and(1,ドルb)/(b*=2);for(;d--;f++)c[d]?g=g substr(e,f,1):0}0ドル=g
Not that short, but AWK doesn't have a print format to generate binary strings from integers, so it has to iterate to get that info...
At a high level, one loop uses bitwise and operations with a single bit moved one position to the left each time to built an array of "binary" settings. It also builds up a string composed of multiple copies of the N number while it's looping. AWK doesn't have a nice string*number operator either.
Then the second loop works backwards through that array and for each entry which is 1, it appends the appropriate character to a "results" accumulator.
The final step just prints the accumulated string.
# Loop 1, build the "binary" array and string of duplicated "N" characters
for(f=b=1;1ドル>=b;e=e1ドル)c[d++]=and(1,ドルb)/(b*=2);
(f=b=1; # Loop init, "f" is used in loop #2
1ドル>=b; # Exit test, goes until bit check > N
e=e1ドル) # End of loop, build "N dup string
c[d++]=and(1,ドルb)/(b*=2); # Body, does a couple of things...
# d++ : increment bit position
# and(1,ドルb) : extract bit
# /(b*=2) : normalize, then shift bit
# c[d++]= : add to "binary" array
# Loop 2, accumulate chars associated with set bits
for(;d--;f++)c[d]?g=g substr(e,f,1):0
d--; # Exit test, when all bits checked stop
f++) # End of loop, increment char pos in "N" dup str
c[d]? : # Ternary, code runs if bit is set
g=g substr(e,f,1) # Append current char to accumulator
0 # No-op "else" from ternary
# Print the result
0ドル=g # Typical AWK golf trick, assign "0ドル" to what you want to print w/o code block
Pip, 11 bytes
a@_X BMETBa
Explanation
a@_X BMETBa
a ; Command-line argument
TB ; Convert to binary
ME ; Map this function to each (index, value) pair:
_ ; The index
a@ ; Character at that index (wrapping) in the original argument
X ; Repeat
B ; 0 or 1 times depending on the digit value
; Concatenate and print (implicit)
Vyxal, 3 bytes
bTİ
So, when attempting to interpret an integer as a list, Vyxal will often cast it to a list of digits. This behaviour is almost never particularly useful, especially compared to casting said integer to a range, but here this somewhat questionable language design choice results in a 3-byte solution.
b # Binary digits of input
T # Find indices of 1s
İ # Index those into [digits of] input
Tcl, 158 bytes
proc X n {join [lmap x [split [string ra [string repe $n [set l [string le [set b [format %b $n]]]]] 0 $l-[expr $n>9]] ""] y [split $b ""] {if $y set\ x}] ""}
-
-
\$\begingroup\$ Failed outgolf: tio.run/##NY3BCsIwEETv/… \$\endgroup\$sergiol– sergiol2025年05月20日 09:57:42 +00:00Commented May 20 at 9:57
CASIO BASIC (CASIO fx-9750GIII), (削除) 148 (削除ここまで) 120 bytes
For 1→N To ᴇ2
Int logab(2,N→E
Seq((N Int÷ 2^(E-K)) Rmdr 2,K,0,E,1→List1
Sum List1→H
0
For 0→K To E
List1[K+1→U
H-U→H
Ans+U10H(Int (10Frac N⌟10(1+(K-1) Rmdr (1+Int Log N
Next
Ans◢
Next
if you need a super-rough explanation, heres my crude and lazily put together attempt at transcribing it to LaTeX:
$$ f(N)=\sum_{k=0}^{\left\lfloor\log_2N\right\rfloor}(o(k+1,N)10^{\text{popcount}(N)-{o_{cuml}(k+1,N)}}\times\left\lfloor 10\times\text{frac}(\frac{N}{10^{(1+(k-1 \mod (1+\left\lfloor\log_{10}N\right\rfloor)))}})\right\rfloor).\\ o(x,y)=\left\lfloor \frac{y}{2^{\left\lfloor\log_2y\right\rfloor-x}} \right\rfloor.\\ o_{cuml}(x,y) = \text{cumulative sum of }o(x,y)\text{ while }y=N. $$ if you can, PLEASE make this better. I know next to nothing about LaTeX and it's the only way I know how to explain my CASIO BASIC code
-
\$\begingroup\$ I guess there isn't an online compiler for Casio Basic anywhere to add to the answer? Is it perhaps possible to make a short gif to see it in action instead? 🤔 \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2025年04月25日 20:00:00 +00:00Commented Apr 25 at 20:00
-
1\$\begingroup\$ Kevin Cruijssen there is one, but it has a bunch of limitations and doesn't support all the commands. I'll try to get a gif for it sometime today \$\endgroup\$madeforlosers– madeforlosers2025年04月28日 12:50:18 +00:00Commented Apr 28 at 12:50
base. \$\endgroup\$