41
\$\begingroup\$

Task

Given a non-negative integer n, output 1 if n is 0, and output the value of n otherwise.

Input

A non-negative integer.

  • If you would like to accept the string as input, the string would match the following regex: /^(0|[1-9][0-9]*)$/, i.e. it must not have any leading zeroes, except when it is 0.
  • If you accept a real integer as input, you may assume that the integer is within the handling capability of the language.

Output

A positive integer, specified above. Leading zeroes are not allowed. Your output should match the regex /^[1-9][0-9]*$/.

Testcases

input output
    0      1
    1      1
    2      2
    3      3
    4      4
    5      5
    6      6
    7      7

Scoring

This is , so shortest answer in bytes wins.

Standard loopholes apply.

\$\endgroup\$
  • 1
    \$\begingroup\$ You should probably put a link to the TNB CMC, since that's where this challenge came from. \$\endgroup\$ – mbomb007 May 3 '17 at 20:53
  • \$\begingroup\$ Does the answer need to be a full function, or can it be the body? \$\endgroup\$ – Caleb Kleveter May 3 '17 at 20:58
  • 1
    \$\begingroup\$ @CalebKleveter The default rule in PPCG is that the answer is either a function or a full program, but not snippets. \$\endgroup\$ – Leaky Nun May 4 '17 at 1:56
  • \$\begingroup\$ Can we print the output with a leading zero? \$\endgroup\$ – MD XF Dec 26 '17 at 21:54
  • \$\begingroup\$ @MDXF yes, you can. \$\endgroup\$ – Leaky Nun Dec 26 '17 at 22:30

119 Answers 119

3
\$\begingroup\$

Taxi, 517 bytes

Go to Post Office:w 1 l 1 r 1 l.Pickup a passenger going to The Babelfishery.Go to The Babelfishery:s 1 l 1 r.Pickup a passenger going to Cyclone.Go to Cyclone:n 1 l 1 l 2 r.Pickup a passenger going to Addition Alley.Pickup a passenger going to Knots Landing.Go to Knots Landing:n 2 r 2 r 1 l.Pickup a passenger going to Addition Alley.Go to Addition Alley:w 1 r 1 l 2 l.Pickup a passenger going to The Babelfishery.Go to The Babelfishery:n 1 r 1 r.Pickup a passenger going to Post Office.Go to Post Office:n 1 l 1 r.

Formatted for humans:

Go to Post Office:w 1 l 1 r 1 l.
Pickup a passenger going to The Babelfishery.
Go to The Babelfishery:s 1 l 1 r.
Pickup a passenger going to Cyclone.
Go to Cyclone:n 1 l 1 l 2 r.
Pickup a passenger going to Addition Alley.
Pickup a passenger going to Knots Landing.
Go to Knots Landing:n 2 r 2 r 1 l.
Pickup a passenger going to Addition Alley.
Go to Addition Alley:w 1 r 1 l 2 l.
Pickup a passenger going to The Babelfishery.
Go to The Babelfishery:n 1 r 1 r.
Pickup a passenger going to Post Office.
Go to Post Office:n 1 l 1 r.

The key location is Knots Landing which performs the NOT operation so 0 returns 1 and all other numbers return 0. Now, you can go to Addition Alley to add the result from Knots Landing to the original input.

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

x86-64 Assembly, 10 9 bytes

Following the standard System V AMD64 calling convention, this function accepts a 32-bit unsigned integer parameter via the EDI register, and returns the result via the EAX register:

89 F8  |     mov   eax, edi    ; move parameter from EDI to EAX
85 C0  |     test  eax, eax    ; test input and set flags
75 01  |     jnz   Finished    ; if input is non-zero, then jump to end
FF 40  |     inc   eax         ; otherwise, input is 0, so increment it to 1
       |  Finished:
C3     |     ret               ; return, leaving result in EAX

x86-32 Assembly, 8 bytes

We could also write this as 32-bit code, since we're only dealing with 32-bit values.

The nice thing about this is that the INC instruction becomes only 1 byte in length. (In 32-bit mode, inc eax can be encoded simply as 40. In 64-bit mode, 40 is interpreted as a REX prefix, so the leading FF is needed. See Intel's documentation for this instruction.)

The caveat is that most 32-bit calling conventions pass parameters on the stack, rather than in registers, and loading a value from the stack (memory) takes many more bytes. If we can be allowed a __fastcall-style calling convention that passes parameters in registers (supported by virtually all C compilers, so not really cheating, just a bit less standard), then the integer parameter is passed in ECX and we get the following code, for a total of 8 bytes:

89 C8  |     mov  eax, ecx
85 C0  |     test eax, eax
75 01  |     jnz  Finished
40     |     inc  eax
       |  Finished:
C3     |     ret
\$\endgroup\$
  • \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ – Justin May 5 '17 at 15:35
3
\$\begingroup\$

Braingolf, 8 bytes

!?_:1_;

Explanation:

 ?       If last element on stack > 0
!        Prevent if check from consuming last element on stack
  _      Pop last element on stack and print
   :     Else
    1    Add int literal 1 to end of stack
     _   Pop last element on stack and print
      ;  Prevent automatic pop of last element on stack
\$\endgroup\$
  • \$\begingroup\$ Is this your language? \$\endgroup\$ – Leaky Nun May 3 '17 at 14:55
  • 1
    \$\begingroup\$ Yup, literally got bored at work today and decided to make a language. As you can tell it's very WIP, this is one of the only challenges that it can do beyond simply printing a string \$\endgroup\$ – Skidsdev May 3 '17 at 14:55
  • 1
    \$\begingroup\$ Congratulations on your new language! \$\endgroup\$ – Leaky Nun May 3 '17 at 14:56
3
\$\begingroup\$

Ruby, 11 bytes

->n{n|1>>n}
\$\endgroup\$
3
\$\begingroup\$

GCC command line, 9 bytes

-Df(x)x?:

Try it online!

-D in GCC checks if the following content has an =; if not, it add the code

#define <followed_content> 1

which in this case turns into

#define f(x)x?: 1
\$\endgroup\$
  • \$\begingroup\$ On meta I gave a common solution but here I give a pointed one \$\endgroup\$ – l4m2 Dec 20 '17 at 18:20
  • 1
    \$\begingroup\$ Can the downvoter leave a comment? I find this very creative. \$\endgroup\$ – MD XF Dec 26 '17 at 21:36
2
\$\begingroup\$

PHP, 11 Bytes

<?=$argn?:1;

Online Version

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

Jelly, 2 bytes

o1

Try it online!

An alternative to the other Jelly solution. o provides a default value for a zero/empty argument; in this case, we default it to 1.

\$\endgroup\$
  • \$\begingroup\$ And another alternative is »1 \$\endgroup\$ – Jonathan Allan May 2 '17 at 17:39
  • \$\begingroup\$ Now you're making me wonder if it's possible in 1 byte; I doubt it, but you have to wonder. The closest I've got is , which outputs the number itself for all nonzero numbers, and nothing if the input is 0. \$\endgroup\$ – user62131 May 2 '17 at 17:46
  • \$\begingroup\$ Yeah, another close one is m which yields [0,0] for 0 and n for n. \$\endgroup\$ – Jonathan Allan May 2 '17 at 17:52
2
\$\begingroup\$

Binary-Encoded Golfical, 12 bytes

This binary encoding can be converted back to the standard graphical representation using the encoder provided in the Golfical github repo, or run directly using the interpreter by adding the -x flag.

Hexdump of binary encoding:

00 40 02 15 17 14 00 01 23 1D 17 14

Original image:

enter image description here

Magnified 120x, with color labels:

enter image description here

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

Perl 5, 10 +1 byte for -p flag = 11 bytes

$_=$_*1||1

Run with -p flag.

\$\endgroup\$
  • \$\begingroup\$ $_=$_||1 and thus $_||=1 is sufficient \$\endgroup\$ – ikegami May 3 '17 at 16:51
  • \$\begingroup\$ Also, the other answers don't include the costs of outputting the result, so no need to count -p. \$\endgroup\$ – ikegami May 3 '17 at 16:52
  • \$\begingroup\$ You are mistaken. Zero is false, whether stored as a number or as a string. However, if you populate $_ using echo 0 | perl -pe'...', you actually have "0\n", which is true. -l would take care of that. \$\endgroup\$ – ikegami May 3 '17 at 17:30
  • 1
    \$\begingroup\$ @ikegami we consider here that requiring the input to be supplied without trailing newline is valid. (using echo -n ... for instance). Regarding the cost of outputting the result, the rules on PPCG say that the result need to be either printed, or returned by a function (which is what does the answer you linked), or returned some other way (I don't remember all valid options). (so counting -p flag is mandatory) \$\endgroup\$ – Dada May 4 '17 at 12:23
2
\$\begingroup\$

Arnold C, 303 bytes

IT'S SHOWTIME
HEY CHRISTMAS TREE i
YOU SET US UP 1
GET YOUR ASS TO MARS i
DO IT NOW
I WANT TO ASK YOU A BUNCH OF QUESTIONS AND I WANT TO HAVE THEM ANSWERED IMMEDIATELY
BECAUSE I'M GOING TO SAY PLEASE i
TALK TO THE HAND i
BULLSHIT
TALK TO THE HAND 1
YOU HAVE NO RESPECT FOR LOGIC
YOU HAVE BEEN TERMINATED

Trying to explain it:

IT'S SHOWTIME //main()
HEY CHRISTMAS TREE i //int i
YOU SET US UP 1 //i = 1
GET YOUR ASS TO MARS i // ? compiler told me to add that
DO IT NOW // ? compiler told me to add that
I WANT TO ASK YOU A BUNCH OF QUESTIONS AND I WANT TO HAVE THEM ANSWERED IMMEDIATELY // something related to reading from stdin
BECAUSE I'M GOING TO SAY PLEASE i // if(i)
TALK TO THE HAND i //print i
BULLSHIT //else
TALK TO THE HAND 1 //print 1
YOU HAVE NO RESPECT FOR LOGIC //endif
YOU HAVE BEEN TERMINATED //end main()

It even beats this answer!

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

C (gcc), 11 bytes

f(n){n?:1;}

Try it online

\$\endgroup\$
  • \$\begingroup\$ I tried it online with the provided link, and it outputs 0, 6. \$\endgroup\$ – Peter Taylor Dec 17 '17 at 17:00
  • \$\begingroup\$ It seems they've changed the gcc behavior recently. If you switch to tcc it still works. \$\endgroup\$ – Raycho Mukelov Jan 12 '18 at 14:03
2
\$\begingroup\$

x86 opcode, 6 5 bytes

       41                         INC     ECX
entry: E3 FD                      JECXZ   SHORT $-1
       91                         XCHG    EAX,ECX
       C3                         RETN

ECX -> EAX

\$\endgroup\$
  • \$\begingroup\$ You'll need to add a length in bytes, so other users can tell how long it is. Generally assembly or byte/op-code languages are measured in the size of the instructions the source represents. \$\endgroup\$ – Οurous Dec 18 '17 at 21:15
  • \$\begingroup\$ fixed. forgot. ` ` \$\endgroup\$ – l4m2 Dec 18 '17 at 21:26
2
\$\begingroup\$

Java 8, 9 bytes

n->n|1>>n

Try it online.

C# .NET, 9 bytes

n=>n|1>>n

Try it online.

Different approach (and 1 byte shorter) than the existing Java/C# .NET answers. (Also works with negative inputs.)

Explanation:

i->       // Method with integer as both parameter and return-type
  i|      //  Return the input bit-wise OR-ed with:
    1>>i  //  1 bit-wise right-shifted by the input

1>>i will be 1 when the input is 0, and 0 for every other input.
0|1 for input 0 will therefore result in 1, and n|0 for every other input will therefore result in n.

\$\endgroup\$
  • 1
    \$\begingroup\$ See? You're getting better at bitwise operations ;-) \$\endgroup\$ – Olivier Grégoire Apr 13 '18 at 9:30
2
\$\begingroup\$

Sinclair ZX81/Timex TS1000/1500 BASIC ~19 tokenized BASIC bytes

 1 INPUT A
 2 PRINT A+NOT A

This is a more efficient solution than the one below; essentially it takes the NOT value of A (which would be zero in all cases but zero) and adds that to the value of A.

Old answer:

Sinclair ZX81/Timex TS1000/1500 BASIC, ~31 tokenized BASIC bytes

 1 INPUT A
 2 IF NOT A THEN LET A=NOT A
 3 PRINT A

This takes a numeric input from the user and will display 1 if zero is entered by making A NOT A; otherwise it does nothing and displays the numeric value.

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

C, 27 Bytes

f(n){printf("%d\n",n?n:1);}
\$\endgroup\$
  • \$\begingroup\$ You don't have to output, you can return that too. Also in GCC, you can do n?:1. \$\endgroup\$ – betseg May 2 '17 at 16:47
  • \$\begingroup\$ @betseg thanks I like that n?:1 trick \$\endgroup\$ – cleblanc May 2 '17 at 16:50
  • \$\begingroup\$ In this particular case you can also use n||1 which is compiler-independent, or !n+n as seen in several other answers. :) \$\endgroup\$ – fluffy May 3 '17 at 1:48
1
\$\begingroup\$

MATL, 3 bytes

t~+

Try it online!

Explanation

t   % Implicit inupt. Duplicate
~   % Logical negation. Converts zero to 1, and nonzero to 0
+   % Add. Implicit display
\$\endgroup\$
1
\$\begingroup\$

JavaScript (ES6), 8 bytes

Beaten to the shortest solution (as usual!), so here's an alternative.

n=>n?n:1

Try It

f=
n=>n?n:1
console.log(f(0))
console.log(f(1))
console.log(f(8888))

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

///, 11 bytes

/#0/1//#//#

Try it online!

Since there is no other way to take input in ///, hard-code the input after the last #.

Works by replacing #0 with 1. Then it removes any remaining #. The # makes sure that an input of 10 would not output 11.

Version that takes input in Itflabtijtslwi:

GGaGGGGbGG/#0/1//#//#ab

Try it online!

\$\endgroup\$
  • \$\begingroup\$ For your second program, the input has to be less than 100. Maybe you should mention this? \$\endgroup\$ – boboquack May 3 '17 at 9:57
  • \$\begingroup\$ @boboquack Yes. \$\endgroup\$ – Comrade SparklePony May 3 '17 at 12:05
1
\$\begingroup\$

AWK, 10 bytes

!$0{$0=1}1

Try it online!

Example Usage:

awk '!$0{$0=1}1' <<< 978
\$\endgroup\$
1
\$\begingroup\$

Universal lambda, 3 bytes (17 bits)

00010110001100010

It is a function and not a complete program. I think it should work, but didn't actually test it because it doesn't seem easy to do so. It means λx.x(λy.x)(λy.y).

Lazy K, 10 bytes

S(SIK)(KI)

It is a function, untested, too.

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

05AB1E, 7 bytes

D0›i,}1

Try it online!

D           //push two inputs (implicit)
 0›         //push input greater than zero
   i        //if true
    ,}      //print input
      1     //push 1. printing is implicit if there is no previous output
\$\endgroup\$
  • \$\begingroup\$ I'm sure there is a much shorter way to do this... \$\endgroup\$ – Leaky Nun May 2 '17 at 18:56
  • \$\begingroup\$ Me too, but I can't actually work 05ab1e, so... \$\endgroup\$ – osuka_ May 2 '17 at 18:57
  • \$\begingroup\$ You could save 3 bytes with D_i1. \$\endgroup\$ – Emigna May 2 '17 at 20:25
  • 3
    \$\begingroup\$ Or in two bytes: $M. \$\endgroup\$ – Adnan May 2 '17 at 21:51
1
\$\begingroup\$

Brain-Flak, 14 bytes

((){{}[()]}{})

Try it online!

Just computes: 1 + (n ? n-1 : 0).

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

Whirl, 38 bytes

01100011100011110011111100001000111100

Try it online!

Explanation:

01100     op.ccw, op.intio    Mem1 = STDIN
011100    math.ccw, math.=    If (Mem1 = 0) Then (Math.Val = 1) Else (Math.Val = 0)
0111100   op.cw, op.one       Op.Val=1
11111100  math.add            Math.Val = Math.Val + Mem1
00        op.one              Op.Val=1 (Cheapest way to loop back to the Math wheel)
100       math.store          Mem1 = Math.Val
0111100   op.ccw, op.intio    STDOUT = Mem1
\$\endgroup\$
1
\$\begingroup\$

Ohm, 11 6 bytes

ö?┼¿1;

Uses CP-437 character encoding. Run with -c flag

Explanation:

ö?┼¿1;
       ■print(                                            )
  ┼    ■      first_input()
 ?   ; ■                   if(                     )
ö      ■                      int(first_input())!=0
   ¿   ■                                            else 
    1  ■                                                 1
\$\endgroup\$
  • \$\begingroup\$ Here's a list of things you could fix: (1) if you just say this is in CP437 (no need for the -c flag), you only need to count characters. (2) Ohm is written in Ruby, so 0 is a truthy value. However, there is a built-in x != 0 component (ö). (3) There is implicit printing, so , is not necessary. (4) Ohm is on TIO now, so it'd help if you added a link to it. \$\endgroup\$ – Nick Clifford May 2 '17 at 19:57
  • \$\begingroup\$ FYI, feel free to ping me in chat if you have any questions about/feature reqs for the language! \$\endgroup\$ – Nick Clifford May 2 '17 at 20:11
  • \$\begingroup\$ Again, there's no need for the -c flag. That's just for reading files; it's not necessary for PPCG submissions. Just say it's in CP437 and you're good. \$\endgroup\$ – Nick Clifford May 2 '17 at 20:36
1
\$\begingroup\$

k, 2 bytes

1|

Finds the maximum of 1 and whatever number is given.

Try it online!

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

Java, 29 bytes

Try Online

int f(int n){return n<1?1:n;}
\$\endgroup\$
1
\$\begingroup\$

Japt, 2 bytes

w1

Try it online!

Returns the larger of 1 and the input.

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

C++, 22 bytes

[](int i){return!i+i;}

Try it online

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

Excel, 10 bytes

=MAX(A1,1)

Here's another 10-byte solution: link

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

SpecBAS - 20 bytes

1 INPUT n: ?n OR n=0

? is shorthand for PRINT

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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