Created
September 15, 2025 12:06
-
-
Save harryposner/b87f787c713d078103b74d1aa171bf5e to your computer and use it in GitHub Desktop.
Prolog solution to the puzzle at math.inc/careers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env swipl | |
| % https://www.math.inc/careers | |
| % We call a date "square" if all of its components (day, month, and year) are | |
| % perfect squares. I was born in the last millennium and my next birthday will | |
| % be the last square date in my life. If we sum the square roots of its | |
| % components (day, month, year), we get my current age. My mother would have | |
| % been born on a square date if the month were a square number. However, it is | |
| % not a square date, but both the month and day are perfect cubes. When was I | |
| % born and when was my mother born? | |
| :- initialization(main, main). | |
| square(N) :- | |
| Root is sqrt(N), | |
| IntRoot is round(Root), | |
| N =:= IntRoot * IntRoot. | |
| cube(N) :- | |
| Root is N ** (1.0 / 3.0), | |
| IntRoot is round(Root), | |
| N =:= IntRoot * IntRoot * IntRoot. | |
| square_date(Year, Month, Day) :- | |
| between(1, 12, Month), | |
| between(1, 31, Day), | |
| square(Year), | |
| square(Month), | |
| square(Day). | |
| last_square_date_in_year(Month, Day) :- | |
| between(1, 12, Month), | |
| between(1, 12, OtherMonth), | |
| between(1, 31, Day), | |
| between(1, 31, OtherDay), | |
| square(Month), square(OtherMonth), | |
| square(Day), square(OtherDay), | |
| \+ (OtherMonth > Month), | |
| \+ (OtherDay > Day). | |
| my_bday(Year, Month, Day) :- | |
| % I was born in the last millennium | |
| between(1900, 2000, Year), | |
| % my next birthday will be the last square date in my life | |
| last_square_date_in_year(Month, Day), | |
| % If we sum the square roots of its components (day, month, year), we get | |
| % my current age. | |
| SqrtYear is sqrt(2025), | |
| SqrtMonth is sqrt(Month), | |
| SqrtDay is sqrt(Day), | |
| CurrentAge is round(SqrtYear + SqrtMonth + SqrtDay), | |
| CurrentAge =:= 2025 - Year - 1. | |
| mom_bday(Year, Month, Day) :- | |
| my_bday(MyBYear, _, _), | |
| between(1, 12, Month), | |
| % My mother | |
| between(1850, MyBYear, Year), | |
| % would have been born on a square date if the month were a square number | |
| square_date(Year, _, Day), | |
| % However, it is not a square date | |
| not(square(Month)), | |
| % but both the month and day are perfect cubes. | |
| cube(Month), | |
| cube(Day). | |
| main :- | |
| my_bday(MyBYear, MyBMonth, MyBDay), | |
| format("~d-~d-~d\n", [MyBYear, MyBMonth, MyBDay]), | |
| mom_bday(MomBYear, MomBMonth, MomBDay), | |
| format("~d-~d-~d\n", [MomBYear, MomBMonth, MomBDay]). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment