| Bytes | Lang | Time | Link |
|---|---|---|---|
| 120 | CASIO BASIC CASIO fx9750GIII | 250425T164415Z | madeforl |
| 158 | Tcl | 250520T095533Z | sergiol |
| 003 | Vyxal | 210705T095910Z | emanresu |
| 051 | R | 240607T193429Z | pajonk |
| 011 | Pip | 240606T215649Z | DLosc |
| 075 | R | 240606T151123Z | Glory2Uk |
| 129 | Rust | 211106T041816Z | cg909 |
| 089 | AWK | 210706T021646Z | cnamejj |
| 008 | Japt | 201019T111332Z | Shaggy |
| 005 | Husk | 201018T155514Z | LegionMa |
| 108 | Java JDK | 200319T100743Z | Olivier |
| 075 | Perl 5 n | 200319T132255Z | andytech |
| 087 | Bash + GNU utilities | 200319T061230Z | Mitchell |
| 058 | Ruby | 200319T063736Z | G B |
| 079 | C gcc | 200319T015935Z | xibu |
| 007 | APL dzaima/APL | 200319T012018Z | Bubbler |
| 072 | Wolfram Language Mathematica | 200318T123423Z | ZaMoC |
| 004 | 05AB1E | 200318T210541Z | Jonathan |
| 091 | C gcc | 200318T140511Z | Noodle9 |
| nan | PHP | 200318T154042Z | Guillerm |
| 053 | Python 3 | 200318T111915Z | Noodle9 |
| 068 | Ruby | 200318T144117Z | IMP1 |
| 131 | Red | 200318T140815Z | Galen Iv |
| 024 | APL Dyalog Unicode | 200318T123853Z | AviFS |
| 006 | 05AB1E | 200318T112840Z | user9206 |
| 004 | 05AB1E | 200318T130722Z | Dorian |
| 010 | J | 200318T123324Z | Galen Iv |
| 025 | APL+WIN | 200318T122853Z | Graham |
| 052 | Python 2 | 200318T112320Z | Surculos |
| 004 | Jelly | 200318T115621Z | RGS |
| 055 | JavaScript ES6 | 200318T111234Z | Arnauld |
| 011 | Charcoal | 200318T112237Z | Neil |
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+U₁₀H(Int (10Frac N⌟₁₀(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
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}] ""}
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
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)
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.
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>()
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
AWK, 89 bytes
{for(f=b=1;$1>=b;e=e$1)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=e$1)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=e$1) # 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
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
Husk, 5 bytes
fḋ¹¢s
Try it online! This function works according to option 1, outputting the term as a string.
Explanation
fḋ¹¢s (Let X denote the argument.)
f Select the elements of
s the string representing X,
¢ cycled infinitely, corresponding to truthy values of
ḋ¹ the binary digits of X.
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
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
Bash + GNU utilities, 87 bytes
x=$1$1$1$1
for((b=`dc<<<2o$1p`;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.
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
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
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
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
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.
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.
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.
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.
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]
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.)
05AB1E, 6 bytes
The Ï< ruins the consecutive word bāsè... (If you don't get it, the hex code 05AB1E converted to base64 would result in base.)
bāsÏ<è
Explanation
b To base 2
ā Length-range to 1
s Prepend
Ï Keep all that's truthy
< -1 due to 0-based indexing ... that's terrible!
è Modular Indexing
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
APL+WIN, 25 bytes
Prompts for integer n and outputs nth term
((b⍴2)⊤n)/(b←1+⌊2⍟n)⍴⍕n←⎕
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.
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!
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()
Charcoal, 11 bytes
⭆↨Iθ²⎇ι§θκω
Try it online! Link is to verbose version of code. Explanation:
θ Input as a string
I Cast to integer
↨ ² Convert to base `2`
⭆ Map over digits
ι Current digit
⎇ If non-zero
θ Input as a string
§ Cyclically indexed by
κ Current index
ω Else empty string

