1. Gregory Petukhov
  2. euler

Commits

Gregory Petukhov  committed e5b3551

Solve new problems

  • Participants
  • Parent commits 9a799e5
  • Branches default

Comments (0)

Files changed (3)

File problem19.py

View file
+#!/usr/bin/env python
+"""
+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)?
+"""
+import logging
+
+def leap_year(year):
+    """
+    >>> leap_year(1)
+    False
+    >>> leap_year(4)
+    True
+    >>> leap_year(100)
+    False
+    >>> leap_year(1200)
+    True
+    """
+
+    if not year % 4:
+        if not year % 100:
+            return not year % 400
+        else:
+            return True
+    else:
+        return False
+
+
+def days_in_month(month, year):
+    """
+    >>> days_in_month(1, 1900)
+    31
+    >>> days_in_month(2, 1900)
+    28
+    """
+
+    if month in [4, 6, 9, 11]:
+        return 30
+    elif month == 2:
+        if leap_year(year):
+            return 29
+        else:
+            return 28
+    else:
+        return 31
+
+
+def days():
+    "Iterates over all weedays between 1 jan 1901 and 31 dec 2000"
+    day = 1
+    month = 1
+    year = 1900
+    weekday = 1
+    while year < 2001:
+        if year > 1900:
+            yield weekday, day, month, year
+        weekday += 1
+        if weekday > 7:
+            weekday = 1
+        day += 1
+        if day > days_in_month(month, year):
+            day = 1
+            month += 1
+        if month > 12:
+            month = 1
+            year += 1
+        logging.debug('%s.%s.%s' % (day, month, year))
+
+
+def solve():
+    return sum(1 for x in days() if x[0] == 7 and x[1] == 1)
+
+
+if __name__ == '__main__':
+    import doctest;
+    import optparse
+
+    parser = optparse.OptionParser()
+    parser.add_option('-t', '--test', action='store_true',
+                      default=False, help='Run tests')
+    parser.add_option('-d', '--debug', action='store_true',
+                      default=False, help='Output debug info')
+    parser.add_option('-f', '--func', default='solve',
+                      help='Function to run')
+    options, args = parser.parse_args()
+
+    log_level = logging.DEBUG if options.debug else logging.INFO
+    logging.basicConfig(level=log_level, format='%(message)s')
+    if options.test:
+        doctest.testmod()
+    else:
+        print globals()[options.func]()

File problem20.py

View file
+#!/usr/bin/env python
+"""
+n! means n  (n  1)  ...  3  2  1
+
+For example, 10! = 10  9  ...  3  2  1 = 3628800,
+and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
+
+Find the sum of the digits in the number 100!
+"""
+import logging
+from math import factorial
+
+def solve():
+    return sum(map(int, str(factorial(100))))
+
+
+if __name__ == '__main__':
+    import doctest;
+    import optparse
+
+    parser = optparse.OptionParser()
+    parser.add_option('-t', '--test', action='store_true',
+                      default=False, help='Run tests')
+    parser.add_option('-d', '--debug', action='store_true',
+                      default=False, help='Output debug info')
+    parser.add_option('-f', '--func', default='solve',
+                      help='Function to run')
+    options, args = parser.parse_args()
+
+    log_level = logging.DEBUG if options.debug else logging.INFO
+    logging.basicConfig(level=log_level, format='%(message)s')
+    if options.test:
+        doctest.testmod()
+    else:
+        print globals()[options.func]()

File test.py

View file
 import problem17
 import problem18
 import problem19
+import problem20
 #...
 import problem67
 
     def test_problem19(self):
         self.assertEqual(171, problem19.solve())
 
+    def test_problem20(self):
+        self.assertEqual(648, problem20.solve())
+
     # ...
 
     def test_problem67(self):