r/dailyprogrammer • u/Cosmologicon 2 3 • Mar 13 '19
[2019-03-13] Challenge #376 [Intermediate] The Revised Julian Calendar
Background
The Revised Julian Calendar is a calendar system very similar to the familiar Gregorian Calendar, but slightly more accurate in terms of average year length. The Revised Julian Calendar has a leap day on Feb 29th of leap years as follows:
- Years that are evenly divisible by 4 are leap years.
- Exception: Years that are evenly divisible by 100 are not leap years.
- Exception to the exception: Years for which the remainder when divided by 900 is either 200 or 600 are leap years.
For instance, 2000 is an exception to the exception: the remainder when dividing 2000 by 900 is 200. So 2000 is a leap year in the Revised Julian Calendar.
Challenge
Given two positive year numbers (with the second one greater than or equal to the first), find out how many leap days (Feb 29ths) appear between Jan 1 of the first year, and Jan 1 of the second year in the Revised Julian Calendar. This is equivalent to asking how many leap years there are in the interval between the two years, including the first but excluding the second.
leaps(2016, 2017) => 1
leaps(2019, 2020) => 0
leaps(1900, 1901) => 0
leaps(2000, 2001) => 1
leaps(2800, 2801) => 0
leaps(123456, 123456) => 0
leaps(1234, 5678) => 1077
leaps(123456, 7891011) => 1881475For this challenge, you must handle very large years efficiently, much faster than checking each year in the range.
leaps(123456789101112, 1314151617181920) => 288412747246240Optional bonus
Some day in the distant future, the Gregorian Calendar and the Revised Julian Calendar will agree that the day is Feb 29th, but they'll disagree about what year it is. Find the first such year (efficiently).
8
u/Groundthug Mar 14 '19
Python, O(1)
Observe that there are 218 leap years every 900 years (900/4 = 225 minus 9 plus 2), then brute-force your way through the last 900 at most years
6
u/Lopsidation Mar 13 '19 edited Mar 13 '19
Python, plus a non-programmed bonus
For the bonus, observe that the two calendars use a 400-year and a 900-year cycle, respectively. This means there's a common cycle of 3600 years. Every 3600 years, the Gregorian calendar gets 9 "double-exception" leap days but the Julian calendar only gets 8. So every 3600 years, the Julian calendar gets one day ahead.
Define a "Gregorian eon" to be 3600 Gregorian years, with the first eon starting on Feb 29, 2000, when both calendars agree. During the Nth Gregorian eon, the Julian calendar alternates between being N-1 and N days ahead.
We're looking for the first time when the two calendars are exactly four years apart. Or more specifically, 365*4+1 days apart. That must be during the the 365*4+1th Gregorian eon.
The start of that eon is Gregorian date Feb 29, 2000+3600*365*4. On that day, the Julian calendar is 365*4 days ahead, on... Feb 28. So close.
The next time the Gregorian calendar gains a day is 800 years from then, when 2800+3600*365*4 is a Gregorian leap year but not a Julian one. So, the Gregorian day of Feb 29, 2800+3600*365*4 is the Julian day of Feb 29, 2804+3600*365*4.
6
u/lukz 2 0 Mar 14 '19 edited Mar 14 '19
8-bit BASIC with line numbers
Screenshot of run in Sharp MZ-800 emulator.
The strange symbol in the expression (E-S)÷4 is an integer division. The computer has a special symbol for it in its screen character generator, I have to substitute it with something else when pasting here as text.
5
u/glubs9 Mar 16 '19
Using Python:
Math based solution
3
u/Gprime5 Mar 13 '19
Python 3
3
u/ASpueW Mar 13 '19
Rust
Playground
3
u/tomekanco Mar 13 '19 edited Mar 14 '19
Python
EDIT: Is flawed, f.e.
leaps(109*450-1,109*450+1)3
u/chunes 1 2 Mar 14 '19 edited Jul 12 '19
A solution in Factor:
Special thanks to /u/Lopsidation, as this is merely a (n unreadable) translation of their
leapsfunction here.Output:
Edit: For fun I decided to write a version that's as readable as possible. It uses lexical variables and even infix notation. Locals are slower and a nightmare to debug, but useful particularly when it comes to arithmetic.
3
u/Redtitwhore Mar 15 '19
This is a math problem
3
u/mudien Mar 17 '19
Python 3 - Easy (but slow) way
Python 3 - More difficult way
3
u/lollordftw Mar 25 '19 edited Mar 25 '19
Haskell
My solution: The number of leap years between a and b is the same as the number of leap years between 0 and b minus the number of leap years between 0 and a.
For some reason, my program spits out 1881474 for the test case (123456, 7891011), whereas the correct output would be 1881475. I don't know why that is, but I would appreciate it if a smart person could explain to me where I went wrong.
3
u/kekekoko23 Apr 06 '19
Python - not pretty but it's fast.
leaps(123456789101112, 1314151617181920) => 288412747246240 in 0.001s
Also I'm new to Python, so any feedback is welcome.
3
u/Medrzec Apr 14 '19
Python3
leaps(123456789101112, 1314151617181920) in 0.000004s
Output:
3
u/[deleted] Apr 30 '19
Python, key is understanding calendar loops around when years are multiples of 900.
2
u/Godspiral 3 3 Mar 13 '19 edited Mar 13 '19
in J, each year in range method
1234567 leaps 1314151
19276
smart version,
2
u/[deleted] Mar 15 '19
I first solved the problem with SWI-Prolog and I ran the test examples and found the wall time to compute them to be 3.6 seconds. I was curious about this so I rewrote the same style of algorithm in Python and found the wall time to compute it was 3.4 seconds.
###Prolog solution
?- time(test).gave 3.6 seconds in the swi-prolog toplevel.###Python Solution
Both
%time test()and%timeit test()gave 3.4 seconds in an ipython console.2
u/odsfab Mar 17 '19
C
Output:
2
u/rodiraskol Mar 29 '19
Python3
(2016, 2017) 1
(2019, 2020) 0
(1900, 1901) 0
(2000, 2001) 1
(2800, 2801) 0
(123456, 123456) 0
(1234, 5678) 1077
(123456, 7891011) 1881475
(123456789101112, 1314151617181920) 288412747246240
2
u/[deleted] Mar 30 '19 edited Mar 30 '19
JAVASCRIPT, node.js with challenge
julian-calendar.js:
Testing script:
All of the outputs are correct, challenge input runs just as fast as any input.
2
u/zeppAnime Apr 18 '19
C, not optimized for large numbers.
2
u/staffinator May 21 '19
Java, O(1)
2
u/voice-of-hermes May 28 '19
Python 2.7+
O(1)
No bonus. Will ponder.
2
u/arinfredwards Jun 01 '19
C#. Didn't go for bonus though.
2
u/clemersonss Jul 21 '19
Scheme, no bonus:
Would love some feedback, just started reading SICP a month ago or so.
2
u/cccons1 Jul 27 '19 edited Jul 31 '19
Common Lisp (SBCL): (Doesn't efficiently handle the largest numbers - yet...)
1
u/octolanceae Mar 14 '19
golang 1.10
A first attempt at golang. I don't know too much about the language at the moment, but, you have to start somewhere. I am sure there is a better way to write this. I will figure it out as I better learn the language
Output:
1
u/garceau28 Mar 14 '19
In Haskell :
1
u/Ayemileto Mar 15 '19 edited Mar 16 '19
Using Javascript:
Testing the examples on an Intel Celeron System with 1.6 GHz Processor Speed and 4GB Ram, I got the following execution Time:
2016, 2017 => 0.0000599... secs.
2019, 2020 => 0.0000450... secs.
1900, 1901 => 0.0000449... secs.
2000, 2001 => 0.0000599... secs.
2800, 2801 => 0.0000400... secs.
123456, 123456 => 0.0000400... secs.
1234, 5678 => 0.0001949... secs.
123456, 7891011 => 0.0370800... secs.
for the last example, leaps(123456789101112, 1314151617181920) => 288412747246240 ,
i have to stop execution as nothing gets returned after over 2mins of script execution.
1
u/y_angelov Mar 18 '19 edited Mar 18 '19
Extremely hacky, but also quick way using recursion in Scala
The above calculates in less than a millisecond
1
u/g00glen00b Mar 19 '19 edited Mar 26 '19
JavaScript
Had to use BigNumber.js as the numbers of the last one are too high to precisely calculate the result.
For the bonus, I just looped over the centuries, until there is a difference of 366 leap years. No clue if that approach is correct though:
The result in that case is the year 5258800 according to the gregorian calendar.
1
u/katamandoo Mar 25 '19
Done in python 3 as a formula, though probably could have done it in C in exactly the same way:
def leaps(year1, year2):
difference = year2 - year1
num_leaps = 0
if difference is not 0:
if year1 % 100 is not 0:
if year1 % 4 is 0:
num_leaps += 1
elif year1 % 900 is 200 or year1 % 900 is 600:
num_leaps += 1
if difference > 1:
num_leaps += difference / 4
num_leaps += (difference + 200) / 900
num_leaps += (difference + 600) / 900
not_leaps = difference / 100
num_leaps -= not_leaps
num_leaps = round(num_leaps)
return num_leaps
Output:
leaps(2016, 2017) => 1
leaps(2019, 2020) => 0
leaps(1900, 1901) => 0
leaps(2000, 2001) => 1
leaps(2800, 2801) => 0
leaps(123456, 123456) => 0
leaps(1234, 5678) => 1077
leaps(123456, 7891011) => 1881476
leaps(123456789101112, 1314151617181920) => 288412747246242
As you can see, my output is out by one or two on the higher numbers but it runs extremely quickly, even for the ludicrous 17 digit numbers
1
u/risent Mar 25 '19 edited Mar 25 '19
I wrote two version leaps.
the
leapsuse exhaustive method, theleaps_perfuse divisible count, but there is a bug in it ,the last test doesn't match.output
1
u/Updownbanana Mar 26 '19
Python 3
Explanation: For 3 numbers a, b, and n, the count of numbers between a and b divisible by n is equal to the range between the first and last divisible numbers divided by n plus 1. This also works for a specific remainder (just offsets the first and last divisible numbers). I made a function to find this and then just ran it 4 times.
1
u/ThiccShadyy Mar 27 '19
Java11. Newbie to the language. All suggestions/criticisms welcome:
1
u/theghks2 Mar 28 '19
Python 3
the helper function
n_pferfect_divhelps find the count of numbers divisible by n in an interval x and y. This is then used to find the appropriate number of leap years.1
u/johnny_pastel Mar 28 '19 edited Mar 28 '19
Java 10 - Any feedback is welcome!
1
u/[deleted] Mar 28 '19
First thing I've done in Rust
1
u/Drossie123 Mar 29 '19
in C#
1
u/GamePlayerCole Apr 06 '19 edited Apr 06 '19
C++
Github Gist if you want that syntax highlighting.
Code
Output
1
u/Luxin_Nocte Apr 09 '19 edited Apr 12 '19
Python 3
My first project in Python, feel free to critique!
Output:
1
u/PrescriptionCocaine Apr 12 '19 edited Apr 12 '19
C++
Ok so first of all, I'm really new to programming, don't know anything past an intro to programming for engineers course.
It works perfectly when it doesnt have to check a shit ton of years, I'm pretty sure it would work if i let it sit for a long ass time, but it's really inefficient. Anyone care to give me a hint or two on how to make it more efficient?
1
u/JamaiKen Apr 16 '19 edited Apr 16 '19
Golang 1.11
Definitely not optimized for the largest numbers, but works nonetheless.
<code> func main() {
</code>
1
u/[deleted] Apr 17 '19
C++ The program finishes in 0.06679 seconds.
>!
!<
1
u/MrThresh Apr 25 '19 edited Apr 25 '19
Kotlin (first bonus done)
I'm a bit late to the party. The way the bonus is implemented actually makes the base implementation worse. Could be fixed by only calling the bonus version for large ranges, but I wanted to keep it simple.
Edit: formatting
1
u/parenthis Apr 25 '19
This was good breakfast coding fun ....
1
u/fredrikaugust Apr 29 '19
C
1
u/alphawinds May 01 '19 edited May 01 '19
C#, The bonus part was based on realteleworm solution with the fact that 218 is the number of leap years in a 900 years period.
1
u/kobepanda May 05 '19 edited May 05 '19
Elixir. Not too happy with my code organization on this one but it works well for the large number case. No bonus
1
u/starxscreamx May 11 '19
Java, works with large numbers
1
u/m3talpillow May 12 '19
Java, O(1) and edge tested.
1
u/gpalyan May 17 '19
1
u/[deleted] May 31 '19
Javascript, O(1) for challenge, O(n) for bonus.
1
u/Khaare Jun 01 '19
Haskell:
1
u/spin81 Jun 21 '19
Rust, without optional bonus:
1
u/dustypaws Jun 24 '19
GO (just the basic challenge)
1
u/normalmighty Jul 15 '19
Python, no bonus
1
u/sebamestre Aug 16 '19
Javascript
1
u/TrackingOP Aug 23 '19
C++ with execusion time return, i'm still really new to C++ so if you have some tips
1
u/2kofawsome Aug 30 '19
! python3.6
No bonus
1
u/ironboy_ Apr 27 '23
JavaScript with bonus, <1ms for very large years: