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

0
\$\begingroup\$

Ruby, 14 bytes

->n{~-n/100+1}

Try it online!

I think it's squeezed as much as possible. I would love to see if anyone has any clever tricks to make this smaller.

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

Ruby, 16 bytes

->i{(i-1)/100+1}
\$\endgroup\$
0
\$\begingroup\$

Swift 4, 18 bytes

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

Try it online!

n is the input of the program

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

Excel, 15 21 18 13 bytes

=INT(99%+A1%)

Input is in cell A1

\$\endgroup\$
  • \$\begingroup\$ Does =LEFT(A1+99,2) work? I don't know Excel. \$\endgroup\$ – Post Rock Garf Hunter Jul 23 '18 at 15:31
  • \$\begingroup\$ @WW It would except it has the same failure I just noted in my original: Fails on years <= 1000. I used that method on the new formula, though, to save a byte. \$\endgroup\$ – Engineer Toast Jul 23 '18 at 15:56
  • \$\begingroup\$ Doesn't =CEILING(A1/100,1) work for 18 bytes? \$\endgroup\$ – Neil Jul 23 '18 at 16:09
  • \$\begingroup\$ @Neil Maybe, yes. Maybe I clearly didn't spend enough time on this because I saw it as a relatively easily challenge and overlooked some pitfalls and optimizations. \$\endgroup\$ – Engineer Toast Jul 23 '18 at 17:19
  • 1
    \$\begingroup\$ @EngineerToast Change A1/100 to A1% for 15 bytes, or adapt the LEFT version to =INT(99%+A1%) for 13 bytes. \$\endgroup\$ – Chronocidal Jul 25 '18 at 14:14
0
\$\begingroup\$

x86_64 machine language on Linux, 12 bytes

0:   97                      xchg   %eax,%edi
1:   ff c8                   dec    %eax
3:   6a 64                   pushq  $0x64
5:   59                      pop    %rcx
6:   99                      cltd   
7:   f7 f9                   idiv   %ecx
9:   ff c0                   inc    %eax
b:   c3                      retq 

Try it online!

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