8
\$\begingroup\$

Your program has to have an year like 1957 to be the input and then output the century of that year.

For example:

In: 1946
Out: 20
In: 1705
Out: 18
In: 1900
Out: 19
In: 100
Out: 1
In: 2001
Out 21

because 1946 is in the 20th century.

Keep in mind that 2000 should be 20th century or 1900 should be in 19th century.

Therefore, the first century spans from the year 1 up to and including the year 100, the second - from the year 101 up to and including the year 200, etc.

Any programming language is allowed and keep your code short and sweet. :)

Additional Challenge: Try to also include float values

\$\endgroup\$
8
  • 11
    \$\begingroup\$ "Try to also include float values" Huh? What do you mean? \$\endgroup\$
    – wastl
    Jul 23, 2018 at 15:25
  • 7
    \$\begingroup\$ @LuisfelipeDejesusMunoz Probably because it is trivial. \$\endgroup\$
    – wastl
    Jul 23, 2018 at 19:26
  • 3
    \$\begingroup\$ Since when we are downvoting trivial challenges? \$\endgroup\$ Jul 24, 2018 at 12:43
  • 4
    \$\begingroup\$ @DeadPossum I downvoted because this challenge is neither interesting nor golf-able. There is no algorithmic complexity in finding the century: nearly all answers simply implement the expression floor((year - 1)/100) + 1, and there aren't other clever optimizations that can be done to shorten the overall program; there aren't any "alternative approaches" to the challenge. Since most answers implement the exact same expression, this challenge looks no more interesting than a list of "floor," "decrement," "divide," and "increment" functions in various languages. \$\endgroup\$ Jul 24, 2018 at 14:51
  • 2
    \$\begingroup\$ How far in the future must our answers be correct until? Is it acceptable to only produce correct input up to the present year? (It matters in the R answer, we can save 2 bytes by only being correct up to the year 9998, possibly 9999) \$\endgroup\$
    – JDL
    Jul 25, 2018 at 15:34

74 Answers 74

2
\$\begingroup\$

Pepe, 31 bytes

rEeEEeeEeeREeEREEEEeeREeEEEReEE

Try it online!

Basically ceil(n/100).

Explanation:

rEeEEeeEeeREeEREEEEeeREeEEEReEE - full program

rEeEEeeEee                      - push 100 to stack B
          REeE                  - input as number to stack A. float gets rounded.
              REEEEee           - A / B, or A / 100. push in stack A.
                     REeEEE     - ceil(A)
                           ReEE - output stack A as number
\$\endgroup\$
2
\$\begingroup\$

Vyxal, 6 4 bytes

‹₁ḭ›

Try it Online!

Explained

‹₁ḭ›
‹    # decrement
 ₁ḭ  # divide floor by 100
   › # increment
\$\endgroup\$
0
1
\$\begingroup\$

MATL, 7 bytes

q100/kQ

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Wouldn’t 100/Xk do? \$\endgroup\$
    – Luis Mendo
    Jul 23, 2018 at 17:10
1
\$\begingroup\$

Jelly, 4 bytes

÷ȷ2Ċ

Try it online!

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

Gaia, 4 bytes

(ℍ/)

Pretty straightforward...

Thanks to Mr. Xcoder and this post for getting me interested in this cool but relatively unused language!

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ @JonathanAllan True, edited. \$\endgroup\$ Jul 23, 2018 at 16:41
1
\$\begingroup\$

Labyrinth, 10 bytes

?(_100/)!@

Same method as my Gaia answer.

Try it online!

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

Kotlin, 19 bytes

{y:Int->(y+99)/100}

Try it online!

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

Scala, 24 bytes

def f(a:Int)=(a-1)/100+1

Try it online!

Scala, 33 bytes, with floats

def f(a:Float)=(Int)((a-1)/100)+1

Try it online!

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

TeaScript, 8 bytes

Mc(x / h

Try it online!

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

CJam, 6 bytes

(100/)

Try it online!

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

K (oK), 7 bytes

Solution:

-_-.01*

Try it online!

Explanation:

Pretty trivial... almost looks like an emoji (-_-):

-_-.01* / the solution
      * / multiply input by
   .01  / 0.01
  -     / negate  \
 _      / floor    - ceiling the result 
-       / negate  /
\$\endgroup\$
1
\$\begingroup\$

TI-Basic, 6 bytes

When given only one argument, sub( divides it by 100.

sub(Ans+99
\$\endgroup\$
1
\$\begingroup\$

Octave/MATLAB, 15 14 bytes

@(x)(x+49)/100

Try it online!

Managed to save a byte by requiring the input to be provided as an integer not a double.

This anonymous function takes an integer as an input, and returns the required value.

MATLAB/Octave when dividing integers performs rounding rather than truncation (Why Mathworks? Why?!). To convert rounding to ceil, we need to add on 49 to the number prior to division.


Original, perhaps uninteresting answer:

@(x)ceil(x/100)

Try it online!

Anonymous function to find the result. Just the standard ceil() of dividing by 100.

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

Excel VBA, 13 bytes

An anonymous function that takes input from cell [A1] and outputs to the console.

?[A1-1]\100+1
\$\endgroup\$
1
  • \$\begingroup\$ Hahaha. I went all way in creating a function \$\endgroup\$
    – Moacir
    Jul 25, 2018 at 12:04
1
\$\begingroup\$

Python 2, 18 bytes

lambda x:-(x/-100)

Taking advantage of the fact / divides towards 0, we make a impromptu ceil division, assuming all inputs are positive which is the case according to the spec. Would work in Python 3 with // but it's golfier in Py2.

Uses ceil(year/100)

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

Whitespace, 51 50 bytes

[S S S T    N
_Push_1][S N
S _Duplicate_1][S N
S _Duplicate_1][T   N
T   T   _Read_STDIN_as_integer][T   T   T   _Retrieve_input][S S S T    N
_Push_1][T  S S T   _Subtract][S S S T  T   S S T   S S N
_Push_100][T    S T S _Integer_divide][T    S S S _Add][T   N
S T _Print_as_integer]

Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

-1 byte thanks to @aschepler.

Try it online.

Explanation in pseudo-code:

Integer n = STDIN as integer
n = n - 1
n = n integer-divided by 100
n = n + 1
Print n as integer to STDOUT
\$\endgroup\$
2
  • 1
    \$\begingroup\$ You can take off one byte by removing one Dup (-3) and replacing the Swap (-3) with a Push 1 (+5). \$\endgroup\$
    – aschepler
    Jul 25, 2018 at 21:42
  • \$\begingroup\$ @aschepler Ah, of course. Thanks! \$\endgroup\$ Jul 26, 2018 at 6:52
1
\$\begingroup\$

Ruby, 24 bytes

def f(n);(n-1)/100+1;end

Try it online!

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

Bash + coreutils, 18 bytes

bc<<<"($1+99)/100"

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ When shell submissions use external programs we typically mention that in the language field to distinguish them from pure shell solutions. So in this case consider specifying the language as "Bash + coreutils" or "Bash + bc". \$\endgroup\$
    – Jakob
    Jul 25, 2018 at 7:09
1
\$\begingroup\$

Common Lisp, 29 19 18 bytes

(ceiling(/ x 100))

Or, if variable definitions aren't allowed, 21 bytes

(ceiling(/(read)100))

Pretty self-explanatory, can be tested with something along these lines:

(setf x 1900)
(print (ceiling (/ x 100)))
\$\endgroup\$
5
  • \$\begingroup\$ a snippet is not allowed (i.e. you can't assume x is defined) \$\endgroup\$
    – ASCII-only
    Jul 26, 2018 at 10:51
  • \$\begingroup\$ @ASCII-only I assumed it was allowed, considering most of the other answers rely on a variable/state being already defined. For example, both Excel answers, TI-Basic, Octave/Matlab \$\endgroup\$
    – JPeroutek
    Jul 26, 2018 at 12:49
  • \$\begingroup\$ Well, Excel is kinda a special case (it's not even a programming language), Ans in TI-BASIC can be considered STDIN (well, at least I think so), and Octave/Matlab defines a lambda \$\endgroup\$
    – ASCII-only
    Jul 27, 2018 at 7:36
  • \$\begingroup\$ I’d argue that Ans in TI basic is definitely a variable. It performs like one in all TI Basic programs. However, when I get home, I’ll amend my answer to better accommodate those rules. \$\endgroup\$
    – JPeroutek
    Jul 28, 2018 at 15:20
  • \$\begingroup\$ Hmm, thinking about it, you're right. Then in that case I'd say that answer is invalid as well \$\endgroup\$
    – ASCII-only
    Jul 30, 2018 at 5:43
1
\$\begingroup\$

C (gcc), 19 bytes

f(x){x=x/100.+.99;}

Try it online!

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

Vim (5 bytes)

99^AXx

This code looks for number at or on the right of the cursor, adds 99 (^A99) and divides it by 100 (Xx).

Note: ^A is single byte (0x1 – Start Of Heading).

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

MAWP, 11 bytes

@1A554WWPM:

Try it!

Saved 3 bytes thanks to @Razetime

\$\endgroup\$
3
  • \$\begingroup\$ 9 bytes \$\endgroup\$
    – Razetime
    Sep 13, 2020 at 15:32
  • \$\begingroup\$ @Razetime your variant doesn't handle edge cases like 1900. However, still saved some bytes by adding 1A \$\endgroup\$
    – Dion
    Sep 14, 2020 at 5:52
  • \$\begingroup\$ oh, silly mistake in that one. \$\endgroup\$
    – Razetime
    Sep 14, 2020 at 5:53
1
\$\begingroup\$

Go, 33 31 bytes

func(y int)int{return^-y/100+1}

Attempt This Online!

  • -2 by @Steffan: (y-1) => ^-y
\$\endgroup\$
1
  • \$\begingroup\$ (y-1) => ^-y \$\endgroup\$
    – naffetS
    Oct 12, 2022 at 1:37
0
\$\begingroup\$

befunge 11 bytes

&1-aa*/1+.@@

  • & read input,
  • 1- subtract one (2000 is 20th century)
  • aa*/ divide by 100
  • 1+ add one
  • . print
  • @ end
\$\endgroup\$
0
\$\begingroup\$

APL (Dyalog), 6 bytes

⌈⎕÷100

My first APL answer!

Thanks to @dzaima for saving me a load of bytes!

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ @dzaima That's a stupid rule... \$\endgroup\$
    – Beta Decay
    Jul 23, 2018 at 15:43
  • 1
    \$\begingroup\$ Well blame the people who didn't believe in year 0 :p \$\endgroup\$
    – dzaima
    Jul 23, 2018 at 15:47
0
\$\begingroup\$

Charcoal, 7 bytes

I⌈∕N¹⁰⁰

Try it online! Link is to verbose version of code. Explanation:

    ¹⁰⁰ Literal 100
   N    Input number
  ∕     Floating-point division
 ⌈      Ceiling
I       Cast to string
        Implicitly print
\$\endgroup\$
0
\$\begingroup\$

Noether, 7 bytes

I100/UP

Try it online

\$\endgroup\$
0
\$\begingroup\$

Python 3, 30 bytes

print((int(input())-1)//100+1)

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Math++, 12 bytes

_(.99+?/100)
\$\endgroup\$
0
\$\begingroup\$

Lua, 21 bytes

print((...-1)//100+1)

Try it online!

I'm not sure if I can get the year as an argument, but that's how I did it.

\$\endgroup\$

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.