24
3
A date can be represented by an unsigned integer as such: YYYYMMDD. What you need to do, is write the shortest program or function that figures out the most recent date whose number was divisible by a given number n
(including today's date) and then returns that date in the format showed above. If there has never been a date (between 00000101 and today inclusive) divisible by the given integer, you should return -1.
Examples
Current Date Input Output
30 July, 2014 4 20140728
30 July, 2014 7 20140729
28 July, 2014 4 20140728
28 July, 2014 7 20140722
28 July, 5 90000 -1
Input
You can read from STDIN or take a function argument or even expect the input to be stored in a variable. The input will be an unsigned integer.
Output
Write to STDOUT or return (or save in a variable) the integer representing the date in the format YYYYMMDD.
Restrictions
You may use any standard library your language offers. Standard loopholes apply.
Winning conditions
This is a code-golf, so smallest program (in bytes) wins. In case of a tie, the answer with the most votes wins.
4
Date 00000101 does not exist. Year count starts by 1. http://en.wikipedia.org/wiki/0_%28year%29
– edc65 – 2014-07-30T14:35:52.4501@edc65 can we pretend it does exist? – overactor – 2014-07-30T14:37:08.987
3
What about Feb 29th? Do we need to apply full leap year rules to check for valid dates? http://en.wikipedia.org/wiki/Leap_year
– Digital Trauma – 2014-07-30T14:52:42.2576
What about the days lost due to the Julian-Gregorian calendar switch? Or are we going Gregorian all the way? http://en.wikipedia.org/wiki/Gregorian_calendar
– Digital Trauma – 2014-07-30T14:56:38.4231Your input/output specs are rather loose. For example, should the "expect the input to be stored in a variable" count the variable declaration in a language like C? You say "write a program", yet you say "take a function argument" - does that mean we can write just a function rather than a full program? – Bob – 2014-07-30T16:43:01.813
@Bob, yes, thanks for pointing that out, you CAN just write a function. – overactor – 2014-07-30T16:49:42.337
What calendar is used ? – Emmanuel – 2014-07-31T09:11:20.787
1
@edc65 "ISO 8601 prescribes, as a minimum, a four-digit year [YYYY] to avoid the year 2000 problem. It therefore represents years from 0000 to 9999, year 0000 being equal to 1 BC and all others AD." from Wikipedia http://en.wikipedia.org/wiki/ISO_8601
So year 0000 exists in ISO 8601. Which year it really means is a bit unclear though.