r/dailyprogrammer • u/nint22 1 2 • May 08 '13
[05/08/13] Challenge #123 [Intermediate] Synchronizing Calendars
(Intermediate): Synchronizing Calendars
You're trying to plan out your family's Easter dinners for the next few centuries.
Your grandparents use the Lunar calendar, but your parents use the Julian calender, so you only have dinner with your grandparents when the calendars synchronize.
To help you figure that out, you're going to need to compute when M Julian years has the same amount of days as N Lunar months. As it turns out, these calendars synchronize with cycles of certain numbers of years.
Some information you will need:
- The time between full moons is 29.53059 days, so that is the length of one Lunar month.
- A Julian year is 365 days for three years, the fourth year is a leap year of 366 days, and then the cycle repeats.
- When taking the days in a number of Lunar months, you will likely get a decimal answer. Round to the nearest day.
Author: Zamarok
Formal Inputs & Outputs
Input Description
You will be given two numbers (M, N), whereM is the number of Julian years, andN is the number of Lunar months.
You need to confirm that the number of days in M Julian years is equal to the number of days in N Lunar months.
Output Description
You will take M and N and discover if the calendars synchronize after M Julian years and N Lunar months.
When looking at how many days N Lunar months will have, round to the nearest day.
If they do synchronize with the given input, print out the number of days that will pass before this occurs.
If the calendars don't synchronize with the given input, print 0.
Sample Inputs & Outputs
Sample Input
38, 470Sample Output
13879
Challenge Input
114, 2664
30, 82Challenge Input Solution
41638
0Note
This was a problem in my homework for an astronomy class. I decided to code a solution to generate solutions, rather than figuring out it by hand. Turned out to be a good problem to solve, and I learned a bunch while doing it. It's difficult enough to provide a good challenge and to make you think about how to approach the problem from different angles.
Let me know if anyone wants to see the original homework assignment, or my solution (about 5 lines of Haskell).
Extra Credit (optional):
Right now your program just confirms when the calendars will synchronize. You can modify your program to generate (M, N) to sequentially discover solutions. Find the largest solution for M where M is less than 500.
For even more extra credit, point out the number of years that it takes for one cycle, a cycle being the time between when these calendars synchronize. There are multiple correct answers here.
6
u/skeeto -9 8 May 08 '13 edited May 08 '13
Lisp
JavaScript
I'm also not seeing consistent results with the challenge inputs. Neither of them align.
10
u/Edward_H May 08 '13
Solution in COBOL, with optional code to generate other solutions.
6
u/minrice2099 May 08 '13 edited May 08 '13
So I have a very short JavaScript solution that works on the sample input, but unless I'm missing something (obvious or otherwise), I can't match the first of the challenge results. Debugging shows the Julian years are coming out right, but not the lunar months (getting 78669, expected 41638). Can someone show me what I'm doing wrong?
5
u/Coder_d00d 1 3 May 08 '13 edited May 08 '13
C solution. Including my main which generates a table of matches.
I generate this table for those who want to compare
4
u/KrazyTheFox May 08 '13 edited May 08 '13
In Java, solving for challenge input:
Challenge Output:
And a sequential discovery:
Result of discovery method:
I'd make a more elegant solution, but I've been told the wrong time I need to be somewhere and I have to leave immediately after posting this.
3
u/Cosmologicon 2 3 May 08 '13
bc:
3
u/DonBiggles May 08 '13 edited May 08 '13
Clojure:
Extra Credit 1:
Extra Credit 2:
Edit: Mispasted some code
3
u/cdt5058 May 08 '13
In Ruby, solving for all the problems except the second part of the Extra Credit:
Extra Credit Output:
I just started programming in Ruby last week, so please let me know if there are better, more efficient ways to solve this problem.
5
u/mattryan May 08 '13
Only in this field can you say the above sentence and no one will look at you funny ;)
2
u/Menagruth May 08 '13
Erlang
2
u/Zamarok May 09 '13
Haskell program that generates all solutions sequentially:
2
u/zSanityz May 10 '13
C++, please give criticism! _^
2
u/chilidogg1491 May 26 '13 edited May 26 '13
Perl solution:
Extra Credit 1:
Table:
2
u/[deleted] May 27 '13
My solution in Java:
2
u/untitledthegreat Jul 13 '13
I got the first part in Java, but I'm having problem with the Extra Credit. The program isn't giving me an output. I tried debugging it, but it seems like the while loop is being weird, and I can't step into it. Could I get some help?
1
u/[deleted] May 08 '13
In Python (2.7.3):
1
u/[deleted] May 10 '13
My solution in C. It works for 38, 470 and 30, 82 but not 114, 2664 (I get 0). Can anyone see what I'm doing wrong? I suspect integer overflow of some kind.
1
u/dont_have_soap May 15 '13
My solution in C. Feel free to criticize, I'm pretty new to the language. Using Visual Studio (no round() in MS's math.h) so had to do a quick function for that.
1
u/pteek May 22 '13
Sorry for being so naive.
Does this mean 550.75 days would be 551 days and 550.40 days would be 550 days?
OR
Does it mean 550.xx days means 550 days?
1
u/markus1189 0 1 Oct 13 '13
Some questions: Do we assume the cycle for julian years to always be [365,365,365,366]? I think that is not stated explicitly and other variants, e.g. [365,366,365,365] would give different results if M mod 4 != 0 or did I miss something?
And: nearestDay 42.5 = 43 or nearestDay 42.5 = 42