| Bytes | Lang | Time | Link |
|---|---|---|---|
| 007 | Raku Perl 6 rakudo | 250424T143903Z | xrs |
| 006 | SAKO | 250328T063844Z | Acrimori |
| 071 | Bespoke | 250328T082612Z | Josiah W |
| 021 | AWK | 241205T191218Z | xrs |
| 010 | YASEPL | 240507T184129Z | madeforl |
| 005 | Scala 3 | 240305T172936Z | movatica |
| 033 | Swift | 230723T203040Z | macOSist |
| 024 | C gcc O3 | 230718T163322Z | c-- |
| 006 | PowerShell | 230724T194600Z | stackpro |
| nan | Piet + asciipiet | 230724T190156Z | Sanguine |
| 006 | ForWhile C version | 230724T173847Z | bsoelch |
| 016 | Haskell | 230719T024153Z | SuperSto |
| 007 | Perl 5 p | 230724T074021Z | Nahuel F |
| 003 | Dyalog APL | 230724T070615Z | RubenVer |
| 043 | D | 230722T060459Z | The Zip |
| 015 | JavaScript | 230718T161855Z | The Thon |
| 010 | jot1 | 230721T163919Z | roblogic |
| 021 | Thue | 230721T161303Z | Dadsdy |
| 013 | Bash | 230720T124608Z | 12431234 |
| 021 | C# Visual C# Interactive Compiler | 230721T131248Z | KeizerHa |
| 007 | x86 / x8664 machinecode | 230721T055024Z | Peter Co |
| 017 | simply | 230720T170733Z | Ismael M |
| 026 | Batch | 230720T074853Z | Neil |
| 009 | [Matlab] | 230719T123352Z | Ferro Lu |
| 005 | Retina 0.8.2 | 230718T183416Z | Neil |
| 012 | Hexagony | 230719T152354Z | CursorCo |
| 011 | Java JDK | 230718T172134Z | David Co |
| 062 | TypeScript’s Type System | 230718T155817Z | noodle p |
| 012 | Rust | 230719T122322Z | mousetai |
| 009 | Befunge93 | 230718T162219Z | Joe |
| 002 | 05AB1E | 230719T071058Z | Kevin Cr |
| nan | 230719T034537Z | LSM07 | |
| 004 | J | 230719T031220Z | south |
| 015 | Alice | 230718T230650Z | Julian |
| 021 | Lua | 230718T225926Z | bluswimm |
| 096 | Pascal | 230718T224855Z | Kai Burg |
| 008 | jq | 230718T222629Z | c-- |
| 004 | Raku | 230718T193603Z | bb94 |
| 015 | Arturo | 230718T191731Z | chunes |
| 009 | Ruby | 230718T191341Z | Jordan |
| 031 | Swift | 230718T190148Z | Bbrk24 |
| 001 | Charcoal | 230718T183304Z | Neil |
| 049 | C gcc | 230718T172054Z | Saladin |
| 063 | Python 3 | 230718T171153Z | Dileep |
| 011 | Python 3 | 230718T161131Z | The Thon |
| 010 | ><> Fish | 230718T155847Z | mousetai |
| 001 | Thunno 2 | 230718T155148Z | The Thon |
Raku (Perl 6) (rakudo), 7 bytes
{1 x$_}
{1 # string of one(s)
x # times
$_ # input
} # implicit return
SAKO, 6 bytes
SPACJI
Well, this luckily is something that SAKO has a built-in for. Takes an integer. The unary digits are spaces.
Full programme version, 36 bytes
1)CZYTAJ:N
SPACJIENT(N)
STOP1
KONIEC
Reads a number from STDIN.
Bespoke, 71 bytes
UNARY
I do some looping until zero
I repeat a specific quantity of ones
1 is used as the "digit".
AWK, 21 bytes
{printf("%0*d",$1,0)}
Prints a 0s: 5 -> 00000
24 bytes
{for(;i++<$1;)printf"1"}
Prints 1s: 5 -> 11111
39 bytes
$0=gensub(0,1,"g",sprintf("%0*d",$1,0))
Modifies the shorter one to print 1s.
YASEPL, 10 bytes
=i';"1",i<
Swift, 33 bytes
let u={for _ in 1...$0{print(0)}}
The "grouping separator" here is a newline after every digit.
C (gcc) -O3, 24 bytes
-1 byte thanks to @12431234123412341234123
f(n){n&&puts(f)+f(n-1);}
C (gcc), 42 bytes
Full program, input from stdin
main(n){printf("%.*d",n,!scanf("%d",&n));}
C (gcc) -m32, 44 bytes
Full program, input as command line argument
*p;main(n,v){printf("%.*d",atoi(1[p=v]),0);}
Piet + ascii-piet, 22 bytes (2×11=22 codels)
tabrjcdskcK a?kqak kk
Explanation:
Prints 1 a total of n times.
ForWhile (C version), 6 bytes
{[9#)}
prints n tabs
Running the program with input 2000000000 takes a bit over 2 minutes and consumes about 150kBit of memory (when using the debug build of the interpreter) so the program is well withing the time and memory limits.
online interpreter Note that the online interpreter does not satisfy the efficiency requirements
Explanation
{ } anonymous procedure
[ ) repeat n times
9# print tab
calling the procedure
{[9#)} 1$ store procedure at address 1
2000000000 1@? call procedure with argument `2000000000`
ForWhile, 26 bytes
99[_48-:0<!:[.'2*+1]).[9#)
Full program, reads binary integer from standard input
99[ ) \ repeat up to 99 times
_ \ get a byte from standard input
48- \ subtract char-code of 0
:0<!:[ ] \ if value is non-negative
. 1 \ temporarily remove condition from stack
'2*+ \ duplicate current value and add new bit
). \ break loop if last character was less than 48
[9#) \ print tab n times
Haskell, 18 16 bytes
-2 bytes thanks to Bubbler
(`replicate`'a')
On my machine, it finished in 48.68 seconds and had a max RSS of 4656 kb, well under the time and memory limits.
Dyalog APL, 3 bytes
⍴∘0
This reshapes 0 to have the same length as the argument to the function, effectively repeating it input times.
For the same length, there's also /∘0 and ⌿∘0.
D, 43 bytes
(int x){import std;return"1".replicate(x);}
not sure if I can make it much shorter. I can't just do a short lambda (x=>...) because it requires replicate from std.array. I tried doing some stuff with iota and map but that resulted in 4 bytes longer than this simpler solution.
JavaScript, 15 bytes
n=>"".padEnd(n)
Returns the string. Uses a space as the character.
This might not be allowed, but for 9 bytes
"".padEnd
-1 from both thanks to @Arnauld
JavaScript, 22 bytes
n=>print("".padEnd(n))
Prints the string.
jot(1), 10 bytes
jot -b1 $1
Outputs a stream of 1s separated by newlines. TIO times out doing the 2 billion case, but it takes just 4 minutes on my Intel Mac
$ time jot -b 1 2000000000|wc -l
2000000000
jot -b 1 2000000000 243.40s user 0.52s system 99% cpu 4:04.24 total
wc -l 2.61s user 1.70s system 1% cpu 4:04.24 total
Thue, 21 Bytes
0::=x
1::=x*
*x::=x**
Input is on the state and in binary, output is some number of xs (can be ignored) and the output number of asterisks on the state at the end.
Is this allowed?
Bash, 13 bytes (with calling other programs)
yes|head -n$1
Explanation
yes prints lines with the content yes. head -n will filter for the first n lines, n is given with $1.
The line with yes is used as digit.
Pure Bash, 29 34 bytes
for((i=$1;0<i;i--));do [;done
Explanation
The for((i=$1;0<i;i--)) will do the commands between do and done $1 times. [ is a builtin bash command that expects a test and a ], which isn't provided, so it prints a error message every time. The error message is used as digit.
I tried also this one:
$1||pwd;$1||0 $(($1-1))
Which works almost when the script is named 0 and in the current $PATH. But it fails with large inputs, so not a working solution.
Explanation
The || means, if that on the left side fails, try that on the right side instead (till the end of the line or ;). If it doesn't fail ignore the part on the right side. If there is nothing on the left side, the right is also ignored.
This tries to call program $1, i.e. the first parameter provided. Since the script is named 0, it will work with the input 0. so it calls itself without any argument. And then, when it runs without an argument, $1 will expands to nothing, which means do nothing and ignore the part right of ||.
If the parameter $1 is a number>0, it will fail to execute a program because there is no program with that name. This means a error message is printed to stderr, which we ignore, and then the right side of || is executed.
The $1||pwd; part means: If program $1 is not valid, i.e. $1 is a number above 0, it calls pwd. pwd outputs something to stdout and it shouldn't change. So we can use it as a non-changing digit. The $1||0 $(($1-1)) part means: If program $1 is not valid, call 0 / itself with the parameter $(($1-1)). $(($1-1)) means 1 subtracted from $1.
It fails with large input because it recursively calls itself and so a new process starts. But i can't create 2'000'000'000 processes on my PC.
C# (Visual C# Interactive Compiler), 21 bytes
x=>new String('a',x);
My first time writing an answer here!
x86 / x86-64 machine-code, 7 bytes, as fast as memset
Just a function, not a full program, so we only need to memset(buf, '1', n) + append a terminating 0 to make an implicit-length C-style string in the buffer the caller supplies, of size n+1 or larger.
This could be called from C in the x86-64 System V calling convention as void to_unary(char *dst, int dummy, int dummy, size_t n). Size is limited only by address-space limits.
machine code | NASM source
; RDI or ES:EDI = output buffer of size n
; RCX or ECX = n
to_unary:
B031 mov al, '1'
F3AA rep stosb
880F mov byte [rdi], cl ; RCX was zeroed by rep stos
C3 ret
stos stores and then increments, so it leaves RDI pointing after the last byte it stored. We depend on the calling convention for DF=0 (increment not decrement), as is normal for all mainstream 32 and 64-bit calling conventions.
The same machine code bytes will do the same thing in 32-bit mode.
At first I was going to use dec eax / stosb for the terminator, with 1 instead of '1' as the unary digit "character". This would be 2 bytes in 32-bit mode only. But then I realized a mov store wouldn't need a displacement in the addressing mode, and that we had cl=0.
For an explicit-length-string version using separate pointer+length, we could return the input length after filling the input buffer (also leaving a pointer to the end in RDI). This would take the same total machine-code size: push rcx to start, pop rax at the end, instead of mov [rdi], cl.
If the buffer is large and aligned, speed on typical modern CPUs, especially with the ERMSB feature, is basically as fast as typical libc memset, at least on Intel maybe not AMD.
On a typical desktop, single-core memory bandwidth is usually close to maxing out the memory controllers. (Much lower per-core bandwidth on many-core Xeons.) Slower with page faults on the first access to newly allocated RAM, but this runs about as fast as it's possible for a single thread to go (for large aligned buffers), except by playing virtual memory tricks to map a range of virtual pages to the same memsetted page of '1's. With a misaligned buffer it's still probably under a second to fill 2GB. For small n, microcode startup overhead dominates, even on CPUs with the "fast-short-rep" feature. AMD CPUs may have slower rep stos, but even storing 1 byte per clock cycle would be way faster than the 4 hour requirement needs.
simply, 17 bytes
This creates an anonymous anonymous function that returns the expected result:
fn($x)=>&tb($x,1)
This is a full program that does the same thing, but outputs it (also 17 bytes):
out&tb($argv[0]1)
How does it work?
This runs the function &tb, which is an alias to &tobase.
It takes the number and the base.
It relies on JavaScript's Number.prototype.toString(radix) with a special case to handle unary (where it just outputs 1s).
Ungolfed
Ungolfing both versions at the same time.
All examples here are executable on their own, and take a JSON array as input, with the value you wish.
This isn't a limitation of the program at all - it's just how you pass values to the compiler to populate the $argv variable.
All examples will be written with &tobase for clarity.
Code-like
// Anonymous function - assigned to $fn
$fn = fn($x) => &tobase($x, 1);
// Calls the function and outputs the result
echo call $fn(argv[0], 1);
// Full program
echo &tobase($argv[0], 1);
Plain-English-like
// Anonymous function - assigned to $fn
Set the variable $fn to an anonymous function with argument $x
Begin.
Return the result of calling the function &tobase with the arguments $x, 1.
End.
// Calls the function and outputs the result
Display the result of calling the variable $fn with the arguments $argv[0], 1.
// Full program
Display the result of calling the function &tobase with the arguments $argv[0], 1.
Batch, 26 bytes
@for/l %i in (1,1,%1)do cd
The @ is needed to prevent unwanted echo of the for command, but echo of the cd command is per-iteration anyway so no @ is needed. The four spaces are needed so that the code will parse correctly.
[Matlab], 9 bytes
ones(1,n)
ones generates mxn matrixes of 1s
[Matlab], 16 bytes
second version upon remarks in the comments
s=@(n) ones(1,n)
anonymous function that generates mxn matrixes of 1s
Retina 0.8.2, 5 bytes
.+
$*
Try it online! Explanation: Outputs 1 repeated by the input count, with a trailing newline.
For Retina 1, remove the $, and then the program outputs _ repeated by the input count, without a trailing newline.
Hexagony, 14 12 bytes
(}?@_.)<!>*'
Layed out
( } ?
@ _ . )
< ! > * '
. . . .
. . .
Explanation
(}? some irrelevant operations initially, then get the input
< branch, if 0 go to @ and terminate, otherwise point the IP southeast
(_ decrement the input cell and redirect the IP northeast
}*)! move the MP to the cell to the right, set that cell to 1 and print that out
_> redirect the IP back east
*' irrelevant operation, then move the MP back to the input cell
this then loops back to the branch command
TypeScript’s Type System, 62 bytes
type U<N,L extends 1[]=[]>=L extends{length:N}?L:U<N,[...L,1]>
Try it at the TypeScript Playground!
Type definition taking a generic integer type and recursively appending 1 to a tuple type until its length is the integer type.
Befunge-93, 9 15 17 bytes
<,7_@#:+&
This program is pretty simple. It gets a number as input, then it decrements it by adding -1. -1 is the value returned when there is no input left, so I'm able to just use one & symbol to get the numbers I need and just continue adding the current value and -1 to decrement. Each time the number is decremented, the program checks if it is 0, and if it is, it quits. I did this by using a bridge (#) to skip over the stop command (@) and the go backward to the @ if the value is 0 using a horizontal if _. Previous versions of my program weren't linear, but rather 2d, so the control was taking up a lot of space. The bridge command allowed me to keep control linear. The program starts with a < to make the program go backwards, this is because the horizontal if statement _ goes right if 0, but I need it to go left, so I reversed the whole program because that's easier than reversing it's input.
05AB1E, 2 bytes
$×
Uses character 1.
Î×
Uses character 0.
°¦
Uses character 0 as well.
õú
Uses a space as character.
If different multiple output-characters would have been allowed, it could be 1 byte with:
∍
Explanation:
$ # Push 1 and the input-integer
× # Repeat the 1 the input amount of times as string
# (which is output implicitly as result)
Î # Push 0 and the input-integer
× # Repeat the 0 the input amount of times as string
# (which is output implicitly as result)
° # Push 10 to the power of the (implicit) input-integer
¦ # Remove the leading 1 so only the 0s remain
# (which is output implicitly as result)
õ # Push an empty string ""
ú # Pad it with the (implicit) input-integer amount of leading spaces
# (which is output implicitly as result)
∍ # Extend/shorten the (implicit) input to a length of the (implicit) input-integer
# (which is output implicitly as result)
As a whole program:
F# (.NET Core), 66 bytes
[<EntryPoint>]let m a=[for _ in 1..(int(Seq.head a))->printf"1"];0
As a function:
F# (.NET Core), 33 bytes
fun a->[for _ in 1..a->printf"1"]
J, 4 bytes
#&LF
Uses linefeed as the character. The large test case will not work on ATO, but 100 timex '# f 2000000000' yields an average of 0.421414 seconds.
#&LF
# NB. copy
&LF NB. bond LF as the right argument
Alice, 15 bytes
/ M \1*.n$@9ot
The new line at the end of the code is required
Uses tab as the output character
/ M \1*.n$@9ot # Full program
/ M \1* # Reads one argument as a string and multiply it by 1, this is our loop counter
.n$@ # Stop if the counter is down to 0
9o # Output a <tab>
t # Decrement the counter
# Goes back to the beginning of the line
Pascal, 96 characters
program u(input,output);var i:integer;begin read(i);while i>0 do begin write('𝍷');i≔i−1 end end.
Necessary implementation characteristics:
The value of maxInt must be ≥ 2000000000.
Swift, 31 bytes
{.init(repeating:"1",count:$0)}
Try it online! A bit more verbose than some, but hopefully clear enough.
Charcoal, 1 byte
N
Try it online! Link is to verbose version of code. Explanation: Charcoal prints a number as a number of -, |, / or \ symbols depending on the direction of the pivot, the default being horizontal.
Python 3, 63 bytes
import sys
n = int(sys.argv[1])
output = '1' * n
print(output)
Python 3, 11 bytes
'0'.__mul__
Note: 0 could be any other character. Function returning a string.
Python 3, 21 bytes
def f(n):print('0'*n)
Function which prints the string.
