# Get the century of a year

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

• "Try to also include float values" Huh? What do you mean? – wastl Jul 23 '18 at 15:25
• @LuisfelipeDejesusMunoz Probably because it is trivial. – wastl Jul 23 '18 at 19:26
• Since when we are downvoting trivial challenges? – Dead Possum Jul 24 '18 at 12:43
• @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. – JungHwan Min Jul 24 '18 at 14:51
• 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) – JDL Jul 25 '18 at 15:34

# 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


# MATL, 7 bytes

q100/kQ


Try it online!

• Wouldn’t 100/Xk do? – Luis Mendo Jul 23 '18 at 17:10

# Jelly, 4 bytes

÷ȷ2Ċ


Try it online!

# 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!

• @JonathanAllan True, edited. – Amphibological Jul 23 '18 at 16:41

# Labyrinth, 10 bytes

?(_100/)!@


Same method as my Gaia answer.

Try it online!

# Kotlin, 19 bytes

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


Try it online!

# 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!

# TeaScript, 8 bytes

Mc(x / h


Try it online!

# CJam, 6 bytes

(100/)


Try it online!

# 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  /


# TI-Basic, 6 bytes

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

sub(Ans+99


# 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.

@(x)ceil(x/100)


Try it online!

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

# Excel VBA, 13 bytes

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

?[A1-1]\100+1

• Hahaha. I went all way in creating a function – Moacir Jul 25 '18 at 12:04

# 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.

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

• You can take off one byte by removing one Dup (-3) and replacing the Swap (-3) with a Push 1 (+5). – aschepler Jul 25 '18 at 21:42
• @aschepler Ah, of course. Thanks! – Kevin Cruijssen Jul 26 '18 at 6:52

# Ruby, 24 bytes

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


Try it online!

# Bash + coreutils, 18 bytes

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


Try it online!

• 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". – Jakob Jul 25 '18 at 7:09

# Common Lisp, 2919 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)))

• a snippet is not allowed (i.e. you can't assume x is defined) – ASCII-only Jul 26 '18 at 10:51
• @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 – JPeroutek Jul 26 '18 at 12:49
• 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 – ASCII-only Jul 27 '18 at 7:36
• 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. – JPeroutek Jul 28 '18 at 15:20
• Hmm, thinking about it, you're right. Then in that case I'd say that answer is invalid as well – ASCII-only Jul 30 '18 at 5:43

# befunge 11 bytes

&1-aa*/1+.@@ 

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

# APL (Dyalog), 6 bytes

⌈⎕÷100


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

Try it online!

• @dzaima That's a stupid rule... – Beta Decay Jul 23 '18 at 15:43
• Well blame the people who didn't believe in year 0 :p – dzaima Jul 23 '18 at 15:47

# Charcoal, 7 bytes

Ｉ⌈∕Ｎ¹⁰⁰


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

    ¹⁰⁰ Literal 100
Ｎ    Input number
∕     Floating-point division
⌈      Ceiling
Ｉ       Cast to string
Implicitly print


# Noether, 7 bytes

I100/UP


Try it online

# Python 3, 30 bytes

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


Try it online!

# Math++, 12 bytes

_(.99+?/100)


# 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.

EXCEL, 17 bytes

A1 as input

=ROUNDUP(A1/100,)

# Java, 19 bytes

y->Math.ceil(y/100)


Try it online.

In java, the ceil function helps. Input and output are both double.

• Hello and welcome to PPCG! This question is code golf, so you want to make your submission as short as possible (I believe you can remove the spaces.) Also please provide a header with the name of the language you submitted and your answer's bytecount. – Amphibological Jul 23 '18 at 18:38
• Welcome to PPCG! Your code currently seems to give a few incorrect answers. – Kevin Cruijssen Jul 24 '18 at 6:58
• @KevinCruijssen it does happen to work if you take input as a double instead of an int... – ETHproductions Jul 24 '18 at 17:29
• @ETHproductions Oh, didn't notice that but you're right. I've edited the answer to our standards. Please take a look at it so you can see how to do it yourself next time akhil7886. And once again welcome to PPCG. – Kevin Cruijssen Jul 25 '18 at 7:09
• @KevinCruijssen BTW you don't need the ds after the year: tio.run/… – Beta Decay Jul 25 '18 at 10:27

# Brain-Flak, 68 bytes

({}[()])<>((((((()()()){}){}){}()){}){})({<(({})){({}[()])<>}{}>()})


Try it online!

Pushing the number 100 is longer than the div algorithm itself! Calculates (n-1)/100+1.

## 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)

# brainfuck, 147 bytes

,>,>,>,>>++++++++[-<++++++>]<[->+<<-<-<-<->>>>]<[-<+>]<[<+>[-]]<[->+>+<<]>>>++++++++++[-<->]+<[<[-<+>]>>-<[-]]>[<<<<+>>>>-]>[-<<<<<+>+>>>>]<<<<<.>.


Try it online!

# Explanation

,>,>,>,>                 input
>++++++++[-<++++++>]     store a '0'
<[- >+<                  save '0'
<-<-<-<->>>>            subtract '0'
]
<[-<+>]                  move 10^0 to 10^1
<[                       check if 10^1 has a non-zero value
<+>                     increment 10^2
[-]                     exit
]
<[->+>+<<]               copy 10^2 twice
>>>
++++++++++               store a ten
[-<->]                   subtract ten
+                        flag for if check
<                        if zero then carry
[                        not zero
<[-<+>]                 put 10^2 one to the left
>>-<[-]
]>
[                        it was zero so carry
<<<<+>>>>-
]
>[-<<<<<+>+>>>>]<<<<<.>. print

• This does not work for years below 1000. I also tried to think of a solution in brainfuck but I couldn't get past this limitation. – Charlie Jul 25 '18 at 15:55
• @Charlie Well, if you fill your input with leading zeros it does. – Jonathan Frech Jul 25 '18 at 16:39

# JavaScript, 14 bytes

n=>n/100+.99|0


Since the century is roughly year/100 + 1, minus an offset of 1 year, it can be calculated like (n+99)/100. This is then rounded down to an integer in the last two bytes.