The Miracle

Those who believe in miracles are as great as miracles themselves. -- From an unknown book

There are many dates when there are miracles. Suppose the date is expressed as an 8-digit number, 1st~4th represents the year, 5th~6th represents the month, 7th~8th represents the day, when the corresponding digit isn't enough, use 0 to pad right, and such date exists in real world. For instance, 20191002 is a valid date , while 20190229 is not. The year ranges from 0001 to 9999.

After observation, the dates when miracles take place have these properties (ignore the zeros before the digits):

  • The number composed by digits represing the day is a prime number . For instance, for 20191002, 02 is a prime number.

  • The number composed by digits represing the month and day is a prime number . For instance, for 20191002, 1002 is not a prime number.

  • The whole date is a prime number . For instance, for 20191002, 20191002 is not a prime number.

Now, you need to figure out how many valid dates are there in total when miracles take place.

Note: Be careful to check whether it is a leap year or not. Year 3200, 6400, 9600 are considered as leap years.


The answer is 55157.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

David Vreken
Oct 3, 2019

The following Python code outputs an answer of 55157 \boxed{55157} .

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# The Miracle
# Python 2.7.3

import datetime
from datetime import timedelta

# function to test if a number is prime
def isprime(n):
    n = abs(int(n))
    if n < 2:
        return False
    if n == 2: 
        return True    
    if not n & 1: 
        return False
    for x in range(3, int(n ** 0.5) + 1, 2):
        if n % x == 0:
            return False
    return True

# function to test if a date is a valid date
def vdate(strdate):
    # get year, month, and date from strdate
    y = strdate[0:4]
    m = strdate[4:6]
    d = strdate[6:8]
    # test if the date is a valid date using the isprime function
    v = False
    if isprime(int(d)):
        if isprime(int(m + d)):
            if isprime(int(strdate)):
                v = True
    # return the results
    return v

# go through each date from 00010101 to 99991231 and count how many
#   how many times it's valid
vtotal = 0
date = datetime.datetime(1, 1, 1)
while True:
    strdate = date.isoformat().replace("-", "")[0:8]
    if vdate(strdate):
        # if it is a valid date then add one to the running total
        vtotal += 1
    if strdate == "99991231":
        # if it is the last date then stop the loop
        break
    # get the next date
    date += timedelta(1)

# display the results
print "Total Valid Dates:", vtotal

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...