-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
090899e
commit d74682b
Showing
12 changed files
with
175 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
Project Euler Problem 19 | ||
This one ended up being very easy thanks to the time library | ||
Problem: | ||
You are given the following information, but you may prefer to do some research | ||
for yourself. | ||
1 Jan 1900 was a Monday. | ||
Thirty days has September, | ||
April, June and November. | ||
All the rest have thirty-one, | ||
Saving February alone, | ||
Which has twenty-eight, rain or shine. | ||
And on leap years, twenty-nine. | ||
A leap year occurs on any year evenly divisible by 4, but not on a century | ||
unless it is divisible by 400. | ||
How many Sundays fell on the first of the month during the twentieth century | ||
(1 Jan 1901 to 31 Dec 2000)? | ||
*/ | ||
#ifndef EULER_P0019 | ||
#define EULER_P0019 | ||
#include <time.h> | ||
#include <stdio.h> | ||
#include <stdint.h> | ||
#include <inttypes.h> | ||
|
||
uint16_t p0019() { | ||
uint16_t answer = 0; | ||
struct tm date = {0}; | ||
|
||
for (int year = 1901; year <= 2000; ++year) { | ||
for (int month = 0; month < 12; ++month) { | ||
date.tm_year = year - 1900; | ||
date.tm_mon = month; | ||
date.tm_mday = 1; | ||
|
||
if (mktime(&date) == -1) { | ||
fprintf(stderr, "mktime failed to normalize the date.\n"); | ||
return -1; | ||
} | ||
|
||
if (date.tm_wday == 0) { | ||
++answer; | ||
} | ||
} | ||
} | ||
return answer; | ||
} | ||
|
||
|
||
#ifndef UNITY_END | ||
int main(int argc, char const *argv[]) { | ||
printf("%" PRIu16 "\n", p0019()); | ||
return 0; | ||
} | ||
#endif | ||
#endif |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
answers = { | ||
x: get_answer(x) for x in ( | ||
*range(1, 18), | ||
20, | ||
*range(19, 21), | ||
22, | ||
25, | ||
30, | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
Project Euler Problem 19 | ||
This one ended up being very easy thanks to the ctime library | ||
Problem: | ||
You are given the following information, but you may prefer to do some research | ||
for yourself. | ||
1 Jan 1900 was a Monday. | ||
Thirty days has September, | ||
April, June and November. | ||
All the rest have thirty-one, | ||
Saving February alone, | ||
Which has twenty-eight, rain or shine. | ||
And on leap years, twenty-nine. | ||
A leap year occurs on any year evenly divisible by 4, but not on a century | ||
unless it is divisible by 400. | ||
How many Sundays fell on the first of the month during the twentieth century | ||
(1 Jan 1901 to 31 Dec 2000)? | ||
*/ | ||
#ifndef EULER_P0019 | ||
#define EULER_P0019 | ||
#include <ctime> | ||
#include <iostream> | ||
#include <cstdint> | ||
#include <stdexcept> | ||
|
||
uint16_t p0019() { | ||
uint16_t answer = 0; | ||
struct tm date = {0}; | ||
|
||
for (int year = 1901; year <= 2000; ++year) { | ||
for (int month = 0; month < 12; ++month) { | ||
date.tm_year = year - 1900; | ||
date.tm_mon = month; | ||
date.tm_mday = 1; | ||
|
||
if (mktime(&date) == -1) { | ||
throw std::runtime_error("mkdtime failed to normalize the date."); | ||
} | ||
if (date.tm_wday == 0) { | ||
++answer; | ||
} | ||
} | ||
} | ||
return answer; | ||
} | ||
|
||
|
||
#ifndef UNITY_END | ||
int main(int argc, char const *argv[]) { | ||
std::cout << p0019() << std::endl; | ||
return 0; | ||
} | ||
#endif | ||
#endif |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,7 @@ | |
9, | ||
11, | ||
*range(13, 18), | ||
20, | ||
*range(19, 21), | ||
22, | ||
34, | ||
76, | ||
|
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
C Implementation of Problem 19 | ||
============================== | ||
|
||
View source code :source:`c/src/p0019.c` | ||
|
||
Solution | ||
-------- | ||
|
||
.. c:function:: uint16_t p0019() | ||
.. c:function:: int main(int argc, char const *argv[]) | ||
.. note:: | ||
This function is only present in the Python test runner, or when compiling as a standalone program. | ||
It is not present when compiling for the Unity test runner. | ||
.. literalinclude:: ../../../c/src/p0019.c | ||
:language: C | ||
:linenos: | ||
.. tags:: calendar, combinatorics |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
C++ Implementation of Problem 19 | ||
================================ | ||
|
||
View source code :source:`cplusplus/src/p0019.cpp` | ||
|
||
Solution | ||
-------- | ||
|
||
.. cpp:function:: uint16_t p0019() | ||
|
||
.. cpp:function:: int main(int argc, char const *argv[]) | ||
|
||
.. note:: | ||
|
||
This function is only present in the Python test runner, or when compiling as a standalone program. | ||
|
||
.. literalinclude:: ../../../cplusplus/src/p0019.cpp | ||
:language: C++ | ||
:linenos: | ||
|
||
.. tags:: calendar, combinatorics |
This file contains 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