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

65 Answers 65

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\$
1
\$\begingroup\$

MATL, 7 bytes

q100/kQ

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Wouldn’t 100/Xk do? \$\endgroup\$ – Luis Mendo Jul 23 '18 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\$
  • \$\begingroup\$ @JonathanAllan True, edited. \$\endgroup\$ – Amphibological Jul 23 '18 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\$
  • \$\begingroup\$ Hahaha. I went all way in creating a function \$\endgroup\$ – Moacir Jul 25 '18 at 12:04
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\$
  • 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 '18 at 21:42
  • \$\begingroup\$ @aschepler Ah, of course. Thanks! \$\endgroup\$ – Kevin Cruijssen Jul 26 '18 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
    \$\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 '18 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\$
  • \$\begingroup\$ a snippet is not allowed (i.e. you can't assume x is defined) \$\endgroup\$ – ASCII-only Jul 26 '18 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 '18 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 '18 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 '18 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 '18 at 5:43
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\$
  • \$\begingroup\$ @dzaima That's a stupid rule... \$\endgroup\$ – Beta Decay Jul 23 '18 at 15:43
  • 1
    \$\begingroup\$ Well blame the people who didn't believe in year 0 :p \$\endgroup\$ – dzaima Jul 23 '18 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\$
0
\$\begingroup\$

EXCEL, 17 bytes

A1 as input

=ROUNDUP(A1/100,)

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

Java, 19 bytes

y->Math.ceil(y/100)

Try it online.

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

\$\endgroup\$
  • 9
    \$\begingroup\$ 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. \$\endgroup\$ – Amphibological Jul 23 '18 at 18:38
  • 1
    \$\begingroup\$ Welcome to PPCG! Your code currently seems to give a few incorrect answers. \$\endgroup\$ – Kevin Cruijssen Jul 24 '18 at 6:58
  • 3
    \$\begingroup\$ @KevinCruijssen it does happen to work if you take input as a double instead of an int... \$\endgroup\$ – ETHproductions Jul 24 '18 at 17:29
  • \$\begingroup\$ @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. \$\endgroup\$ – Kevin Cruijssen Jul 25 '18 at 7:09
  • 1
    \$\begingroup\$ @KevinCruijssen BTW you don't need the ds after the year: tio.run/… \$\endgroup\$ – Beta Decay Jul 25 '18 at 10:27
0
\$\begingroup\$

Brain-Flak, 68 bytes

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

Try it online!

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

\$\endgroup\$
0
\$\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\$
0
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Charlie Jul 25 '18 at 15:55
  • 1
    \$\begingroup\$ @Charlie Well, if you fill your input with leading zeros it does. \$\endgroup\$ – Jonathan Frech Jul 25 '18 at 16:39
0
\$\begingroup\$

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.

\$\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.