55
\$\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\$
7
  • 1
    \$\begingroup\$ You should probably put a link to the TNB CMC, since that's where this challenge came from. \$\endgroup\$
    – mbomb007
    Commented May 3, 2017 at 20:53
  • \$\begingroup\$ Does the answer need to be a full function, or can it be the body? \$\endgroup\$ Commented May 3, 2017 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
    Commented May 4, 2017 at 1:56
  • \$\begingroup\$ Can we print the output with a leading zero? \$\endgroup\$
    – MD XF
    Commented Dec 26, 2017 at 21:54
  • \$\begingroup\$ @MDXF yes, you can. \$\endgroup\$
    – Leaky Nun
    Commented Dec 26, 2017 at 22:30

164 Answers 164

2
\$\begingroup\$

Desmos, 10 bytes

Improvement suggested by Aiden Chow.

f(x)=0^x+x

Previous 13-byte solution:

f(x)=max(1,x)

Try it here

\$\endgroup\$
4
  • \$\begingroup\$ Desmos tips: you can have lists of integers like this. \$\endgroup\$
    – Leaky Nun
    Commented May 4, 2017 at 1:58
  • 1
    \$\begingroup\$ 10 bytes: f(x)=0^x+x. This works because 0^0 equals 1 in Desmos. \$\endgroup\$
    – Aiden Chow
    Commented Dec 30, 2023 at 8:15
  • \$\begingroup\$ @AidenChow That's wild. Thanks for letting me know! \$\endgroup\$
    – DanTheMan
    Commented Jan 4 at 19:38
  • 1
    \$\begingroup\$ Yea, this 0^x+x trick is actually used to avoid log(0) situations when trying to count the digits of a number. floor(logn)+1 (where n is the number we are trying to get the digits of) works if n is positive, but the shortest way to cover the n=0 case seems to be floor(log(0^n+n))+1. \$\endgroup\$
    – Aiden Chow
    Commented Jan 4 at 22:12
1
\$\begingroup\$

C, 27 Bytes

f(n){printf("%d\n",n?n:1);}
\$\endgroup\$
3
  • \$\begingroup\$ You don't have to output, you can return that too. Also in GCC, you can do n?:1. \$\endgroup\$
    – betseg
    Commented May 2, 2017 at 16:47
  • \$\begingroup\$ @betseg thanks I like that n?:1 trick \$\endgroup\$
    – cleblanc
    Commented May 2, 2017 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
    Commented May 3, 2017 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\$

///, 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\$
2
  • \$\begingroup\$ For your second program, the input has to be less than 100. Maybe you should mention this? \$\endgroup\$
    – boboquack
    Commented May 3, 2017 at 9:57
  • \$\begingroup\$ @boboquack Yes. \$\endgroup\$
    – sporkl
    Commented May 3, 2017 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\$
4
  • \$\begingroup\$ I'm sure there is a much shorter way to do this... \$\endgroup\$
    – Leaky Nun
    Commented May 2, 2017 at 18:56
  • \$\begingroup\$ Me too, but I can't actually work 05ab1e, so... \$\endgroup\$
    – osuka_
    Commented May 2, 2017 at 18:57
  • \$\begingroup\$ You could save 3 bytes with D_i1. \$\endgroup\$
    – Emigna
    Commented May 2, 2017 at 20:25
  • 3
    \$\begingroup\$ Or in two bytes: $M. \$\endgroup\$
    – Adnan
    Commented May 2, 2017 at 21:51
1
\$\begingroup\$

Brain-Flak, 14 bytes

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

Try it online!

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

\$\endgroup\$
1
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\$
3
  • \$\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\$ Commented May 2, 2017 at 19:57
  • \$\begingroup\$ FYI, feel free to ping me in chat if you have any questions about/feature reqs for the language! \$\endgroup\$ Commented May 2, 2017 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\$ Commented May 2, 2017 at 20:36
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\$
1
\$\begingroup\$

C#, 11 bytes

n=>n<1?1:n;

This compiles to a Func<int, int>.

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

Starry, 14 bytes

, +'      +*`.

Try it online!

Explanation

Space shown as _ .

,           Read integer and push to stack
_+          Duplicate
'           If non-zero jump to branch label
______+     Push 1
*           Add
`           Mark branch label
.           Print as a number
\$\endgroup\$
1
  • \$\begingroup\$ My first Starry answer \$\endgroup\$
    – Luis Mendo
    Commented May 2, 2017 at 23:10
1
\$\begingroup\$

Clojure, 12 bytes

#(get[1]% %)

The get function takes an associative data structure, a key, and a default value. Vectors are associative using sequential position as the key. So, [1] is a vector with the value 1 at position 0. If get is called with parameter 0 it will return 1, otherwise no other keys exist in the vector so it returns the default value of the parameter.

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

Clojure, 10 bytes

#(max 1%)

Not much to explain.

\$\endgroup\$
1
  • \$\begingroup\$ I think you can shorten this to #(max 1%) (9 bytes). tio.run/nexus/… \$\endgroup\$
    – Dennis
    Commented May 4, 2017 at 14:37
1
\$\begingroup\$

Julia, 13 12 bytes

f(n)=n<1?1:n

Unfortunately, Julia doesn't do implicit casting from int to bool, so I have to burn an entire 3 characters just to do a comparison to zero. Saved one byte by safely assuming the number isn't negative. Still too verbose for my taste, though.

\$\endgroup\$
1
  • \$\begingroup\$ You can post the answer as an anonymous function and save 2 bytes, n->n<1?1:n Try it online! \$\endgroup\$
    – Sundar R
    Commented Aug 12, 2018 at 21:16
1
\$\begingroup\$

Groovy, 7 bytes

{it?:1}

Elvis operator; if true, return self, else return 1. Only false integer value auto-unboxxed to false in Groovy is 0. Thusly, exactly the spec.

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

Symbolic Python, 11 bytes

_+=_==(_>_)

Try it online!

_+=           # Output = Input +
   _==(_>_)                       Input == 0
\$\endgroup\$
1
\$\begingroup\$

Google Sheets, 9 Bytes

Anonymous worksheet function that takes input from range A1 and outputs to the calling cell

=Max(1,A1
\$\endgroup\$
1
  • \$\begingroup\$ This look very cool mate :) \$\endgroup\$
    – NTCG
    Commented Jan 4, 2018 at 8:07
1
\$\begingroup\$

Bash, 43 bytes

if [ $1 -eq 0 ];then echo 1;else echo $1;fi
\$\endgroup\$
1
\$\begingroup\$

Python 21 Bytes

int(max('1',input()))

Takes input from REPL environment

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

Aceto, 9 8 bytes

rid0=`1p

read an integer and duplicate it, then push 0. Are they =? Then (`) push a 1. print the top element.

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

PowerShell, 24 22 17 Bytes

blatantly stolen from here

!($a=$args[0])+$a

Explanation

Invert the value, returning 0 for non-0 numbers, and 1 for 0, then add the intitial to it.

this makes it basically 1/0 + value, so for 0 the first value is 1, any other numbers it's 0.

examples:
# !0+0 = 1
# !1+1 = 1
# !9+9 = 9
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Could you instead use !$a as your index check? You wouldn't be able to set $a in your current manner, but I think something like this would work? Try it online! \$\endgroup\$
    – Sinusoid
    Commented May 2, 2017 at 21:13
  • 1
    \$\begingroup\$ @Sinusoid that worked perfectly for -2, but turns out the C answer was a bit ahead of us both, didn't think of the method at all. \$\endgroup\$
    – colsw
    Commented May 3, 2017 at 9:46
  • \$\begingroup\$ That is certainly interesting! I will need to keep this method in mind :P \$\endgroup\$
    – Sinusoid
    Commented May 3, 2017 at 13:21
1
\$\begingroup\$

Chip, 20 18 bytes

eaABb
*`\\-!
fcCDd

Try it online!

How?

 aABb
            Copy the low 4 bits from input to output
 cCDd

e
*           Set the higher bits of output, so that the values are ASCII digits
f

eaABb
*           Replicate any ASCII digits on input to output
fcCDd

            -
     !      Produce a high signal, but only during the first byte
            -

 aAB
 `\\-*      Set the lowest bit of output, if the four low bits of input are unset
  CD

 aAB        Set the lowest bit of output, if the four low bits
 `\\-!      of input are unset, and only on the first byte
  CD
\$\endgroup\$
1
\$\begingroup\$

Julia 0.6, 10 bytes

x->x<1?1:x

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ also 10 bytes: x->x+(x<1) \$\endgroup\$
    – Giuseppe
    Commented Jan 17, 2018 at 19:03
1
\$\begingroup\$

SNOBOL4 (CSNOBOL4), 49 47 36 bytes

	N =INPUT
	N =EQ(N) 1
	OUTPUT =N
END

Try it online!

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

><>, 6 bytes

:?!1n;

Could have been 1 byte shorter if ? had the opposite conditional behaviour.

:?       check if nonzero, then either
  !      a) skip the next instruction or
   1     b) push 1 to the stack
    n    print
     ;   terminate
\$\endgroup\$
3
  • \$\begingroup\$ Take the input via the -v flag instead. It saves a byte, and it's less suspect than using the char code of a character \$\endgroup\$
    – Jo King
    Commented Apr 13, 2018 at 8:17
  • \$\begingroup\$ 5 bytes \$\endgroup\$
    – Jo King
    Commented Apr 13, 2018 at 10:34
  • \$\begingroup\$ Terminating with an error wasn't legal when I used to be around. \$\endgroup\$ Commented Apr 13, 2018 at 12:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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