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
    Commented Jul 23, 2018 at 15:25
  • 7
    \$\begingroup\$ @LuisfelipeDejesusMunoz Probably because it is trivial. \$\endgroup\$
    – wastl
    Commented Jul 23, 2018 at 19:26
  • 3
    \$\begingroup\$ Since when we are downvoting trivial challenges? \$\endgroup\$ Commented 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\$ Commented 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
    Commented Jul 25, 2018 at 15:34

74 Answers 74

34
\$\begingroup\$

ArnoldC, 308 298 bytes

IT'S SHOWTIME
HEY CHRISTMAS TREE x
YOU SET US UP 0
GET YOUR ASS TO MARS x
DO IT NOW
I WANT TO ASK YOU A BUNCH OF QUESTIONS AND I WANT TO HAVE THEM ANSWERED IMMEDIATELY
GET TO THE CHOPPER x
HERE IS MY INVITATION x
GET UP 99
HE HAD TO SPLIT 100
ENOUGH TALK
TALK TO THE HAND x
YOU HAVE BEEN TERMINATED

Try it online!

This seemed a challenge easy enough to try to write my first answer in ArnoldC...

\$\endgroup\$
2
  • 8
    \$\begingroup\$ Upvote cause it's ArnoldC \$\endgroup\$ Commented Jul 24, 2018 at 8:02
  • 3
    \$\begingroup\$ Please vote on the solution, not the language. \$\endgroup\$
    – Shaggy
    Commented Jul 27, 2018 at 20:33
11
\$\begingroup\$

JavaScript, 14 bytes

y=>1+--y/100|0

Try it online

\$\endgroup\$
10
\$\begingroup\$

Vim, 13 bytes

3I0<Esc>$3hd0<C-x>xx<C-a>
\$\endgroup\$
1
10
\$\begingroup\$

Taxi, 1327/2433 1057/1835 bytes

Two versions here; the shortest one works but shows the result in decimal format (e.g. 20.000000 instead of 20). I am posting both because, while the longer version is more correct, I still hope to win this code golf with the short one (it could require a little bit of tweaking, though).

Short version

You can test it here.

Go to Post Office: west, 1st left, 1st right, 1st left.
Pickup a passenger going to The Babelfishery.
Go to The Babelfishery: west, 1st left, 1st right.
Pickup a passenger going to Addition Alley.
Go to Starchild Numerology: east, 1st left, 1st left, 1st left, 2nd left.
99 is waiting at Starchild Numerology.
100 is waiting at Starchild Numerology.
Pickup a passenger going to Addition Alley.
Pickup a passenger going to Narrow Path Park.
Go to Addition Alley: east, 1st left, 2nd right, 3rd right, 1st right.
Pickup a passenger going to Divide and Conquer.
Go to Narrow Path Park: north, 1st right, 1st left, 1st right.
Pickup a passenger going to Divide and Conquer.
Go to Divide and Conquer: east, 1st right, 2nd right.
Pickup a passenger going to Trunkers.
Go to Trunkers: east, 1st right, 3rd right, 1st left.
Pickup a passenger going to The Babelfishery.
Go to The Babelfishery: east, 1st right, 1st right.
Pickup a passenger going to Post Office.
Go to Post Office: east, 1st left, 1st right.
Go to Taxi Garage: east, 1st right, 1st left, 1st right.

Long version

You can test it here.:

Go to Post Office: west, 1st left, 1st right, 1st left.
Pickup a passenger going to The Babelfishery.
Go to The Babelfishery: west, 1st left, 1st right.
Pickup a passenger going to Addition Alley.
Go to Starchild Numerology: east, 1st left, 1st left, 1st left, 2nd left.
99 is waiting at Starchild Numerology.
100 is waiting at Starchild Numerology.
Pickup a passenger going to Addition Alley.
Pickup a passenger going to Narrow Path Park.
Go to Addition Alley: east, 1st left, 2nd right, 3rd right, 1st right.
Pickup a passenger going to Divide and Conquer.
Go to Narrow Path Park: north, 1st right, 1st left, 1st right.
Pickup a passenger going to Divide and Conquer.
Go to Divide and Conquer: east, 1st right, 2nd right.
Pickup a passenger going to Trunkers.
Go to Trunkers: east, 1st right, 3rd right, 1st left.
Pickup a passenger going to The Babelfishery.
Go to The Babelfishery: east, 1st right, 1st right.
Pickup a passenger going to Chop Suey.
Go to Chop Suey: east, 6th right, 1st left.
Pickup a passenger going to KonKat's.
[processDigits]
Pickup a passenger going to Cyclone.
Go to Cyclone: north, 1st left, 3rd left.
Pickup a passenger going to Crime Lab.
Go to Writer's Depot: west.
"." is waiting at Writer's Depot.
Pickup a passenger going to Crime Lab.
Go to Crime Lab: east, 1st right, 2nd right, 2nd left.
Switch to plan "notAColon" if no one is waiting.
Switch to plan "print".
[notAColon]
Go to Cyclone: west, 1st right, 1st left, 2nd right.
Pickup a passenger going to KonKat's.
Go to KonKat's: east, 2nd right, 2nd right.
Pickup a passenger going to KonKat's.
Go to Chop Suey: north, 1st right, 1st right.
Switch to plan "processDigits".
[print]
Go to KonKat's: east.
Pickup a passenger going to Post Office.
Go to Post Office: south, 3rd right, 1st left.
Go to Taxi Garage: east, 1st right, 1st left, 1st right.
\$\endgroup\$
1
  • 6
    \$\begingroup\$ Welcome to PPCG! \$\endgroup\$
    – Beta Decay
    Commented Jul 25, 2018 at 10:23
7
\$\begingroup\$

05AB1E,  4  3 bytes

-1 byte thanks to Jonathon Allan

т/î

Try it online!

\$\endgroup\$
1
  • 9
    \$\begingroup\$ I like the extra spaces around the 4 so that the crossed-out 4 is no longer a 4. \$\endgroup\$ Commented Jul 23, 2018 at 19:57
7
\$\begingroup\$

Brachylog, 6 bytes

/₁₀₀⌉₁

Try it online!

Just wanted to try my hand at Brachylog. Uses the "divide by 100 and ceil the result" method that many other answers use.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Now that you are a Brachylog expert :p maybe try Ex-Increasing Set Sequence, it might be quite good for that :) \$\endgroup\$ Commented Jul 23, 2018 at 19:20
  • \$\begingroup\$ It's been a while since I saw a Brachylog answer not posted by me :p \$\endgroup\$
    – Fatalize
    Commented Jul 25, 2018 at 6:31
7
\$\begingroup\$

R, 19 18 16 bytes

1+scan()%/%99.99

Try it online!

Saved 1 byte thanks to digEmAll! -2 bytes thanks to JDL!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Not completely sure... does this work too 1+(scan()-1)%/%100 (18 bytes) or I'm missing something ? \$\endgroup\$
    – digEmAll
    Commented Jul 23, 2018 at 19:06
  • \$\begingroup\$ @digEmAll Nice! Seems to work fine. \$\endgroup\$
    – Robert S.
    Commented Jul 23, 2018 at 21:16
  • 3
    \$\begingroup\$ the challenge doesn't say how far in the future we need to go but 1+scan()%/%99.99 may save 2 bytes (it will start to go wonky after 100 centuries) \$\endgroup\$
    – JDL
    Commented Jul 25, 2018 at 12:26
6
\$\begingroup\$

LOLCODE, 76 bytes

HAI 1.2
I HAS A x
GIMMEH x
VISIBLE QUOSHUNT OF SUM OF x AN 99 AN 100
KTHXBYE

Try it online!

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

Cubix, 11 10 bytes

@)OuI('d,U

Try it online!

This wraps onto the cube as follows

    @ )
    O u
I ( ' d , U . .
. . . . . . . .
    . .
    . .

Watch it run

Not sure what I was thinking when I did the first one.

Excluding the path changes the general process is I('d,)O@ - Input integer, decrement, push 100, integer divide, increment, output integer and exit.

Float version 16 bytes

And since it was an interesting exercise, here's a version that will handle a float input. Try 1900, 1900.0 and 1900.1. Of course it isn't really doing floats as Cubix prefers integers and the format of the float doesn't really matter. 1900 1, 1900-1 and 1900.1 are all treated the same.

WBiWIUd'.(B?@,)O

Try it online!

And wrapped

    W B
    i W
I U d ' . ( B ?
@ , ) O . . . .
    . .
    . .

Watch it run

This one does (excluding path changes)

  • IiI Input integer, input char (-1 for EOI or char value for seperator), Input integer
  • ? Test TOS
    • If 0 B(, reverse stack and decrement TOS
    • If positive B reverse stack
    • ** negative shouldn't happen **
  • 'd,)O@ push 100, integer divide, increment TOS, output and exit
\$\endgroup\$
5
\$\begingroup\$

Hexagony, 11 bytes

?({!@/)d':/

Try it online!

Linear code (9 bytes):

?({d':)!@

Ungolfed code:

  ? ( {
 ! @ / )
d ' : / .
 . . . .
  . . .

It may be possible to save 1 or 2 bytes by using : to terminate (division by zero error), but I can't find out a way.

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

MarioLANG, 821 807 625 595 570 386 bytes

;
-
)
)
+
+
(                >- >- >- >- >- >- >- >- >- >-)
(                "= "= "= "= "= "= "= "= "= "=+
>       >      >[!![!![!![!![!![!![!![!![!![!!(
"======="======"=##=##=##=##=##=##=##=##=##=##
!(![-)+<!(![(< ![ <  <  <  <  <  <  <  <  <  <<
#=#===="#=#==" #=="=="=="=="=="=="=="=="=="=="=
  >(   ! (<   >))-[!(+
  "====#=="   =====#=:
             !     <
             #====="

Try it online!

I'm sure this can be golfier Now this is starting to be quite golfy, though it took me a while to figure out a way to do the division in MarioLANG.

Improvements:

  • Lots of bytes saved by taking advantage of the fact that Mario starts falling from the upper left corner of the code.
  • Code before a fall has been placed during the fall.
  • Great improvement after redesigning the division-by-10 part.

Explanation:

  • First we set a to INPUT as integer and substract 1.
  • Then we set b to 0 and c equals to 2.
  • Then we substract 1 from a 10 times, checking every step if a has reached 0.
  • If we successfully substract 10 from a, increment b.
  • Repeat this until a is 0. Now we have the result of the division in b.
  • Substract 1 from c and move b to a if b is not 0. After moving the value b is set to 0.
  • Repeat the division-by-10 operation one more time, substracting 1 from c again and now that c is 0 the program outputs the result in b and exits.
\$\endgroup\$
4
\$\begingroup\$

SOGL V0.12, 3 bytes

M/U

Try it Here!

\$\endgroup\$
4
\$\begingroup\$

Bash, 20 19 bytes

echo $[($1+99)/100]

Try it online!

-1 thanks to W W

\$\endgroup\$
3
  • 1
    \$\begingroup\$ 19 bytes \$\endgroup\$
    – Wheat Wizard
    Commented Jul 23, 2018 at 15:28
  • \$\begingroup\$ 18 bytes, by getting the 1st two bytes of the string and increment -> echo $[${1:0:2}+1] \$\endgroup\$ Commented Jul 24, 2018 at 7:19
  • \$\begingroup\$ @BastianBittorf That fails for numbers < 1000 and multiples of 100. \$\endgroup\$
    – wastl
    Commented Jul 24, 2018 at 11:32
4
\$\begingroup\$

Japt, 9 4 bytes

-5 from @Shaggy

/L c

Try it online!

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

Perl 5 -p, 18 15 bytes

$_=1+--$_/100|0

Try it online!

(-3 whole bytes, thanks to @Dom Hastings.)

Or with -p -MPOSIX:

15 13 bytes

$_=ceil$_/100

Try it online!

(-2 bytes, again thanks to @Dom Hastings.)

\$\endgroup\$
3
  • \$\begingroup\$ Solid answer, cant think of a shorter approach right now! You can drop the parens on ceil for -2 and reorder the first using |0 for a few savings too: Try it online! \$\endgroup\$ Commented Jul 23, 2018 at 19:08
  • 1
    \$\begingroup\$ @DomHastings That's what the |0 in the JS answer was for! (Trying to learn Brachylog left me seeing "alternate predicate" instead of "coerce to int" there.) Thank you. Just in case you're able to make something of it, here's the regex approach that I started with: /..?$/;$_=$`+($&>0) (I'm now trying to see if I can learn enough Retina to implement this in that). \$\endgroup\$
    – Sundar R
    Commented Jul 23, 2018 at 19:18
  • \$\begingroup\$ Here's a 13 byte solution that doesn't use any command line module inclusions. \$\endgroup\$
    – Xcali
    Commented Sep 12, 2020 at 2:24
4
\$\begingroup\$

Retina, 26 bytes

r`(.*)(.?.)
$2*X$1*
X+
_
.

Try it online!

My first attempt at Retina. Can probably be golfed more (though, I like the (.?.) babyface in this one).

\$\endgroup\$
2
  • \$\begingroup\$ It seems shorter to use arithmetic unfortunately. Hope you enjoy Retina golfing! :) \$\endgroup\$ Commented Jul 23, 2018 at 21:14
  • \$\begingroup\$ @FryAmTheEggman That's pretty neat, learnt a few things from that (I stopped reading the Unary arithmetic tutorial page when I realized it was outdated, didn't want to get confused by old syntax). That final |$ is an especially neat trick for an increment. (And I will, thanks!) \$\endgroup\$
    – Sundar R
    Commented Jul 23, 2018 at 21:34
4
\$\begingroup\$

dc, 6

Assuming input and output on the top-of-stack is OK:

99+A0/

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Java 8, 12 bytes

y->~-y/100+1

Try it online.

Explanation:

y->     // Method with integer as both parameter and return-type
  ~-y   //  Decrease the input by 1
  /100  //  Then integer-divide it by 100
  +1    //  And then add 1
\$\endgroup\$
7
  • \$\begingroup\$ nah, I guess youll have to provide wrapping stuff, too. \$\endgroup\$
    – Luatic
    Commented Jul 24, 2018 at 12:52
  • 1
    \$\begingroup\$ @LMD This has been discussed many times in the meta ever since functions were allowed as alternative for full programs, but Java and C# doesn't need to add the definitions, parameter-types, nor trailing semi-colon to the code for lambdas (recursive methods and imports are a different story of course). \$\endgroup\$ Commented Jul 24, 2018 at 13:09
  • \$\begingroup\$ If I knew those rules... sniff \$\endgroup\$
    – Luatic
    Commented Jul 24, 2018 at 13:10
  • 2
    \$\begingroup\$ +1, though I prefer y->--y/100+1 for the sake of readability. \$\endgroup\$ Commented Jul 24, 2018 at 22:33
  • 1
    \$\begingroup\$ @KevinCruijssen No worries, it's only a personal preference. Your answer is as clear as the suggestion, plus you explain the code, so what more can we ask for? ;) \$\endgroup\$ Commented Jul 25, 2018 at 15:25
4
\$\begingroup\$

C (gcc), 23 22 bytes

f(i){return--i/100+1;}

Try it online!

-1 byte saved thanks to @luser droog

20 bytes

R;f(i){R=--i/100+1;}

Try it online!

-2 bytes saved thanks to @Digital Trauma

18 bytes

f(i){i=--i/100+1;}

Try it online!

Another -2 bytes saved thanks to @ErikF

\$\endgroup\$
5
  • \$\begingroup\$ Any tips on improving the score? I feel like a couple bytes could be saved \$\endgroup\$
    – foemre
    Commented Jul 23, 2018 at 18:58
  • \$\begingroup\$ If you're OK with a bit of undefined behaviour, you can do this for 20. \$\endgroup\$ Commented Jul 23, 2018 at 20:57
  • \$\begingroup\$ Drop the space after return. \$\endgroup\$ Commented Jul 24, 2018 at 3:42
  • 2
    \$\begingroup\$ You can reuse the i as its own return (18 bytes): Try it online! \$\endgroup\$
    – ErikF
    Commented Jul 24, 2018 at 7:09
  • \$\begingroup\$ @ErikF that's absolutely amazing \$\endgroup\$
    – foemre
    Commented Jul 24, 2018 at 8:33
4
\$\begingroup\$

Stax, 5 bytes (packed)

vAJ/^

Run and debug it

Explanation:

vAJ/^ Full program, implicit input
v     Decrement
 AJ   Push 100 (102)
   /  Integer division
    ^ Increment
\$\endgroup\$
3
\$\begingroup\$

Perl 6, 13 bytes

(*+99)/100+|0

Test it

Expanded:

# WhateverCode lambda
(
  *      # this is the parameter
    + 99 # add 99 to adjust the century

) / 100  # divide by 100

+| 0     # Integer bitwise-or with 0 (coerce to Int)
\$\endgroup\$
0
3
\$\begingroup\$

GolfScript, 5 7 bytes

~(100/)

Try it online!

~(100/)
AAA  AA
\\\   \\
 \\\   \Increments the stack
  \\\   Divides the stack
   \\\  
    \\Adds 100 to the stack
     \Decrements the stack
      Turn the input string into an int
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Note that e.g. 2001 is in the 21st century. This one works: ~(100/) \$\endgroup\$
    – wastl
    Commented Jul 24, 2018 at 11:44
2
\$\begingroup\$

Python 2, 18 bytes

lambda x:~-x/100+1

Try it online!

\$\endgroup\$
4
  • \$\begingroup\$ Putting 100 in seems to return 1.99 \$\endgroup\$ Commented Jul 23, 2018 at 23:57
  • \$\begingroup\$ @Acccumulation no? \$\endgroup\$
    – Rod
    Commented Jul 24, 2018 at 0:47
  • \$\begingroup\$ Sorry, I missed that it was Python 2 rather than 3. \$\endgroup\$ Commented Jul 24, 2018 at 15:04
  • \$\begingroup\$ @Acccumulation Duplicate the / and it´s both Python 2 and 3. \$\endgroup\$
    – Titus
    Commented Jul 25, 2018 at 9:37
2
\$\begingroup\$

Haskell, 16 bytes

(`div`100).(+99)

Try it online!

16 bytes

f x=div(x+99)100

Try it online!

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

PHP, 22 bytes

<?=1+--$argv[1]/100|0;

Try it online

<?=ceil($argv[1]/100);

Try it online

\$\endgroup\$
1
  • \$\begingroup\$ It's a bit awkward, but you can save 1 byte using -R: Try it online! \$\endgroup\$ Commented Jul 23, 2018 at 20:21
2
\$\begingroup\$

PHP, 19 bytes

<?=ceil($argn/100);

or

<?=--$argn/100+1|0;

Save to file, run as pipe with -nF.

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

Excel Vba, 89 bytes

Function GetCentury(Year As Long)
GetCentury = Mid((Year - 1) / 100, 1, 2) + 1
End Function

And a version with 65 bytes

Function C(Y As Long)
C = Mid((Y - 1) / 100, 1, 2) + 1
End Function

Honestly I dont even know if this is a valid entry :/

\$\endgroup\$
1
  • 3
    \$\begingroup\$ Welcome to PPCG! Usually you would state your golfed version first and add a longer version as explanation only. Thus your score 65 bytes. I'm not familiar with VBA, but can you remove some of the spaces in C = Mid((Y - 1) / 100, 1, 2) + 1? Finally you may want to check out our Tips for Golfing in VBA. Have fun! \$\endgroup\$
    – Laikoni
    Commented Jul 25, 2018 at 13:18
2
\$\begingroup\$

Shakespeare Programming Language, 195 bytes

,.Ajax,.Page,.Act I:.Scene I:.[Enter Ajax and Page]Ajax:Listen toheart!You is the sum ofa cat the quotient betweenthe sum ofyou a pig the square ofthe sum ofa big big big cat a big cat!Open heart

Try it online!

Explanation:

,.Ajax,.Page,.Act I:.Scene I:.[Enter Ajax and Page]Ajax:   #Header

Listen toheart!   # Set Page to the input

# Set Page to (n-1)/(10)**2+1
You is the sum ofa cat the quotient betweenthe sum ofyou a pig the square ofthe sum ofa big big big cat a big cat!

Open heart  # Print Page's value as a number
\$\endgroup\$
2
\$\begingroup\$

Elixir, 89 78 bytes

Not perfect but I'm just learning Elixir so code-golf it is!

IO.gets("")|>String.to_integer|>(&(Integer.floor_div(100,&1-1)+1)).()|>IO.puts

Try it online!

In a better format:

IO.gets("")                # Get input from terminal
|>String.to_integer        # Make it an int
|>(&(                      # Declare an anonymous function
  Integer.floor_div(100,   # Do a floored division with 100 on the bottom
     &1-1)                  # ... and the integer input minus 1 on top
  +1))                    # Add 1 to the result
  .()                      # Gotta invoke that anonymous for pipes
|>IO.puts                  # Output
\$\endgroup\$
2
\$\begingroup\$

Pyth - 6 7 bytes

.EcQ100

Explanation:

.EcQ100  Implicit print
   Q     Input
  /      Divided by
    100  100
.E       Rounded up

Fixed edge case

\$\endgroup\$
1
  • \$\begingroup\$ I believe this fails the test case for input 1900. Should return 19, instead of 20 \$\endgroup\$
    – JPeroutek
    Commented Jul 25, 2018 at 18:21

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.