11
\$\begingroup\$

You know what a palindrome, a prime and a date are.

Your task is to list all dates in 100 thousands of years that fulfill all three characteristics.

Nevermind anything but the numbers, use the following formats: YYYYMMDD and YYYYYMMDD.

Dates between 0000-01-01 and 9999-12-31 should be printed as 8 digit palindromes dates (If there is any?) and dates between 10000-01-01 and 99999-12-31 should be printed as 9 digit palindromes.

It's not mandatory to list the dates in chronological order.

Example part of valid output.

First three 9 digit prime palindromic dates:

...
100111001
100131001
100161001
...

Rules

Standard loopholes apply.

\$\endgroup\$
9

13 Answers 13

5
\$\begingroup\$

Ruby, 144 141 bytes (134+7 for the -rprime flag)

Saved 3 bytes thanks to benj2240!

('01'..'12').map{|m|('01'..'31').map{|d|(?0..?9).map{|k|q=m+d
y=q.reverse+k
r=y+q
Time.new(y,m,d).day==d.to_i&&r.to_i.prime?&&(p r)}}}

Try it online!

The algorithm:

  • generate all possible MMDD combinations, from "0101" to "1231"
  • generate all years for that date that would result in a palindrome, by reversing the MMDD string and adding in the middle, in turn, all chars in the (0..9) range
  • check if that is a valid date by creating a Time instance with the given y, m, d values. If the resulting time object has a #day value equal to d, then that was a valid date. Otherwise, it would shift the date (for example, Time.new 2018,2,30 returns 2018-03-02).
  • check if the valid palindrome date is also a prime and display it if it is.

The inner loop was initially a function that was called for each element in the (?0..?9) range, as well as for the empty string.

Since the empty string produced no results (there are no valid 8 digit prime palindromes), I decided to remove it and refactor to this version.

\$\endgroup\$
2
  • \$\begingroup\$ I think you can save a few bytes by removing the t variable: TIO \$\endgroup\$
    – benj2240
    Feb 10, 2018 at 0:26
  • \$\begingroup\$ @benj2240 That's right! Thanks! \$\endgroup\$ Feb 10, 2018 at 8:04
4
\$\begingroup\$

Python 2, 116 107 128 122 119 bytes

def g(n=9**8):
 while n<1e9:
  n+=2;m=n/100%100
  if 0<m<13and n%100<31+(m+m/8)%2and`n`[::-1]==`n`and 2**n%n==2:print n

The second half of the 4th line is inspired by mxdsp's answer here to another golf question.

Explanation

The function g() takes an argument only to initialize the n variable using its default value. The initial value is an odd number that's as short as possible and as large as possible while still being less than the first valid answer 100111001.

Loop until n reaches the end of the date range 109. Increment n by 2. m is the month of date n.

If n is a valid date, palindrome and prime, print it:

  • Date:
    • 0 < m < 13 checks that m is a valid month.
    • n % 100 < 31 + (m+m/8)%2 checks that n's day of the month is valid. (m+m/8)%2 adds 1 for all months with 31 days. Credit for that goes to ArmanX's answer. There are no primes for February 29-30.
  • Palindrome: `n`[::-1] == `n`. The backticks stringify n. [::-1] reverses the string.
  • Prime: 2**n % n == 2 is a Fermat primality test. That test is only probabilistic. There are also non-primes that match. But not in the range of numbers we're looking at.
\$\endgroup\$
1
  • \$\begingroup\$ Nice one using the Fermat’s primality test! \$\endgroup\$
    – agtoever
    Feb 10, 2018 at 8:51
3
\$\begingroup\$

APL (Dyalog Unicode), 155 bytes

⎕CY'dfns'
n←⍕x
m←(⍎2↑¯4↑n)
d←(⍎¯2↑n)
:If n≡⌽n
:AndIf 1 pco x
:AndIf m<13
:AndIf d<32
:If m d≡2 29
:AndIf (400|⍎((≢n)-4)↑n)=0
⎕←x
f x+72
:End
⎕←x
:End
f x+1

Try it online!

This is a Tradfn (traditional function) that takes one argument arg = yyyymmdd or arg = yyyyymmdd. Usage is f arg.

This will not output anything when the argument starts at 10000101 because it doesn't find a prime palindrome date in 60 seconds.

Here is a less golfed approach that will output the OP's example output of

100111001
100131001
100161001

(Try it Online!)

Notice that both codes are the exact same until right before calling the function recursively for the next date. While the golfed version just calls it as f arg+1, the less golfed code jumps from day 31 to day 01 and from month 12 to month 01, which speeds it up quite a bit.

How it works:

⎕CY'dfns'                   ⍝ Copy (⎕CY) all dfns to enable the use of pco.
n←⍕x                        ⍝ Assign (←) the input to the variable n.
m←(⍎2↑¯4↑n)                 ⍝ Take (↑) the last 4 (¯4) elements of n, then take the first 2 elements of that and assign to m. 
d←(⍎¯2↑n)                   ⍝ Take the last 2 (¯2) elements of n, and assign to d.
:If n≡⌽n                    ⍝ If n matches (≡) its reverse (⌽) (is a palindrome)
:AndIf 1 pco x              ⍝ And a prime (1 pco)
:AndIf m<13                 ⍝ And month < 13
:AndIf d<32                 ⍝ And day < 32
:If m d≡2 29                ⍝ If it is 2/29
:AndIf (400|⍎((≢n)-4)↑n)=0  ⍝ And the year mod 400 = 0
⎕←x                         ⍝ Print x
f x+72                      ⍝ call f with arg year0301
:End
⎕←x                         ⍝ Print x
:End
f x+1                       ⍝ call f for the next date.
\$\endgroup\$
2
\$\begingroup\$

Python 3, 163 bytes

r=range
for m in r(1,13):
 for d in r(1,31+(m%2==(m<8))-2*(m==2)):
  t="%02d"*2%(m,d)
  for i in r(10):x=int(t[::-1]+str(i)+t);all(x%i for i in r(2,x))and print(x)

Solution is fairly long (and probably can get improved upon), but doesn't use any built-in functions for prime/date/palindrome checking. A somewhat ungolfed version for clarity:

for month in range(1,13):
    for day in range(1,31 + (month%2==(month<8)) - 2*(month==2)):
        t = "%02d%02d" % (month, day)
        for i in range(10):
            x = int(t[::-1] + str(i) + t)
            if all(x%i for i in range(2,x)):print(x)

Valid dates are generated by choosing a month and day. As commented before, only size 9 need be considered. Also note that leap years aren't considered. This isn't required because of the lucky coincidence that palindrome primes of length 9 which end on 0229 simply don't exist (other date anomalies like the 30th February of 1712 can be discarded for the same reason).

Next the middle digit is chosen freely, and a prime test is performed. Because of the prime test had to be as short as possible, it is very naive and thus cripplingly slow. Using an external library could solve this (and save some bytes), but as mentioned before I didn't want to use any.

\$\endgroup\$
4
  • \$\begingroup\$ You should make your code look exactly how it does when you counted bytes (in this case by collapsing indent spacing). Also, it's great that you included an ungolfed version, but usually the golf version is listed first \$\endgroup\$
    – wnnmaw
    Jan 15, 2018 at 19:28
  • \$\begingroup\$ @wnnmaw The only difference between the version I counted on the one supplied in the answer, is I used tabs instead of the spaces that get used here. As I'm aware tabs get auto-converted, so I don't see any way to fix this. \$\endgroup\$
    – Def
    Jan 15, 2018 at 19:36
  • \$\begingroup\$ @Def IIRC Python allows you to use spaces as indentations too, that way it could look the same in your answer too. Correct me if I'm wrong on the first part though. \$\endgroup\$ Jan 15, 2018 at 19:43
  • \$\begingroup\$ @elementbound It does indeed, thanks for the suggestion. \$\endgroup\$
    – Def
    Jan 15, 2018 at 19:49
2
\$\begingroup\$

WolframLanguage (Mathematica) 187 bytes

There may be some reduction in size to be found. Explanation to follow...

t=ToString;p=PadLeft;d=DateObject;Cases[""<>{t/@p[#,If[Length@#<5,4, 5]],t/@ p[#2,2],t/@p[#3,2]}&@@@(IntegerDigits/@#[[1]]&/@DayRange[d@#,d@#2]),x_/;PalindromeQ@x&&PrimeQ@ToExpression@x]&

Test cases

t = ToString; p = PadLeft; d = DateObject;
Cases["" <> {t /@ p[#, If[Length@# < 5, 4, 5]], t /@ p[#2, 2], 
   t /@ p[#3, 2]} & @@@ (IntegerDigits /@ #[[1]] & /@ DayRange[d@#, d@#2]), 
   x_ /; PalindromeQ@x && PrimeQ@ToExpression@x] &[{10011, 10, 1}, {10017, 1, 1}]

(* {"100111001", "100131001", "100161001"} *)

Explanation of code

DayRange[d@#,d@#2] returns all of the dates between {10011, 10, 1}and {10017, 1, 1}. In this case, it returns approximately 5 years, 4 months of dates (precisely 1920 dates). Leap years are taken into account.

The dates are returned in Wolfram-standard formatting. For example, the first date will appear as DateObject[List[1,1,1],"Day","Gregorian",-5.]`

#[[1]] & /@ will remove the part of the date, in each date, that concerns us. In the example, DateObject[List[1,3,7],"Day","Gregorian",-5.] returns the abbreviated date, {1,3,7}.

t/@p[#3,2]} or ToString/@Padleft[#3,2] pads the third element, namely, the 7 standing "for 7th day of the month" as "07". Similar padding is provided for the single digit symbol for the month of March, namely, 3 is returned as "03".

p[#, If[Length@# < 5, 4, 5]] pads the year with zeros to reach the length of a 4 or 5 digit string. In this case, January, namely 1, is returned as `"00001"'.

"" <>... joins the strings. In this case, it returns "000010307".

Cases[...x_ /; PalindromeQ@x && PrimeQ@ToExpression@x] returns those cases, among the 1920 dates, that are palindromes and primes.

\$\endgroup\$
2
\$\begingroup\$

Javascript, 187 177

Assumptions: no matching 4 digit years; no matching days in February between 29-30

p=n=>(n<10?'0':'')+n;f=n=>n[3]+n[2]+n[1]+n[0];for(m=13;--m;)for(d=31+(m+(0|m/8))%2;--d;){for(y=10;y--;){z=p(m)+p(d);Y=+(f(z)+y+z);for(i=2;Y%i&&i*i<Y;i++);if(Y%i)console.log(Y)}}

It works like this:

p=n=>(n<10?'0':'')+n;       //Prepend a 0 for 1-digit numbers and convert to a string
f=n=>n[3]+n[2]+n[1]+n[0];   //Flip four digits
for(m=13;--m;)              //Month loop, from 12 to 1
 for(d=
       31+(m+(0|m/8))%2     //Calculate the days in the month, allowing  Feb. 29 & 30
                       ;--d;){ //Day loop
  for(y=10;y--;){           //Middle digit loop
   z=p(m)+p(d);             //Prepend zeros to the month and day
   Y=+(f(z)+y+z);           //Flip the digits; append the middle digit,
                            //month, and day; convert back to an integer
   for(i=2;Y%i&&i*i<Y;i++); //Check if it's a prime
    if(Y%i)console.log(Y)}} //If it doesn't divide evenly, it's not prime. Print it!

History:

  • 187 to 177: There are no prime palindrome dates that fall on Feb. 29 or 30, so we can pretend Feb. has 30 days and save 10 characters.

Notes:

Through testing, I found there are no valid matches that are 4-digit years or fall on Feb. 29 or 30. There are, unfortunately for the sake of the code, exactly five (invalid) results that fall on the 31st of various months that only have 31 days.

\$\endgroup\$
2
\$\begingroup\$

Java 10, 329 327 320 318 312 308 307 264 bytes

v->{for(int y=9999,m,d;++y<1e5;)for(m=0;++m<13;)for(d=0;++d<32;)try{java.time.LocalDate.of(y,m,d);var t=y+"".format("%02d%02d",m,d);long n=new Long(t),x=1;for(;n%++x%n>0;);if(t.contains(new StringBuffer(t).reverse())&n==x)System.out.println(t);}finally{continue;}}

-1 byte thanks to @assylias.

Explanation:

Try it online (note: the prime-checking part has been replaced with a more efficient separated method, although even with that it still times out after 60 seconds, only outputting the first ~115 palindromic prime dates).
Pastebin of all 197 outputs from a local run.

v->{                           // Method without empty unused parameter and no return-type
  for(int y=9999,m,d;++y<1e5;) //  Loop over the years in the range [1000,9999]:
    for(m=0;++m<13;)           //   Inner loop over the months in the range [1,12]:
      for(d=0;++d<32;){        //    Inner loop over the days in the range [1,31]:
        try{java.time.LocalDate.of(y,m,d);
                               //     If it's a valid date:
          var t=y+"".format("%02d%02d",m,d);
                               //      Convert the numbers to a String in format "yyyyyMMdd"
          long n=new Long(t),  //      Convert this String to a long
          x=1;for(;n%++x%n>0;);//      Prime-checking loop
          if(t.contains(new StringBuffer(t).reverse())
                               //      If the string is palindromic
             &n==x)            //      and the long is a prime:
            System.out.println(t);
                               //       Print the string with trailing newline
        }finally{              //     If it isn't a valid date:
          continue;}}}         //      Continue to the next iteration of the inner-most loop
\$\endgroup\$
2
  • 1
    \$\begingroup\$ if(t.equals(new StringBuffer(t).reverse()+"") -> if(t.contains(new StringBuffer(t).reverse()) to save 1 character (works because we know both strings have the same length). That's not much :-( \$\endgroup\$
    – assylias
    Jan 17, 2018 at 15:25
  • \$\begingroup\$ @assylias Smart, I like it. Thanks! And although 1 byte is not much, it's still 1 byte. Codegolf is always about making it as short as possible, so every byte counts. :) \$\endgroup\$ Jan 17, 2018 at 15:31
1
\$\begingroup\$

VBA, 347

Sub PalindromeDate()
Dim DateString As String
For i = 0 To 9999
    For j = 1 To 12
        For k = 1 To 31
        DateString = Format(i, "0000") & Format(j, "00") & Format(k, "00")
        If DateString = StrReverse(DateString) Then
        Debug.Print DateString
        Else
        End If
        Next k
        Next j
        Next i

End Sub
\$\endgroup\$
5
  • \$\begingroup\$ Welcome to PPCG! I don't know VBA, but it looks like you could golf some whitespace off. \$\endgroup\$
    – qqq
    Jan 15, 2018 at 21:37
  • \$\begingroup\$ I also don't really know VBA, but I think DateString is an arbitrary variable name, so you should be able to reduce that to a single character, right? \$\endgroup\$ Jan 15, 2018 at 22:23
  • 3
    \$\begingroup\$ And I think you missed the prime part of "palindromic prime dates". \$\endgroup\$
    – mercator
    Jan 15, 2018 at 23:09
  • \$\begingroup\$ There would be some code for calculate leap years (the one has February 29 days) \$\endgroup\$
    – user58988
    Jan 16, 2018 at 17:51
  • \$\begingroup\$ 5-digit years are also missing, and Else is not necessary. \$\endgroup\$ Feb 10, 2018 at 10:42
1
\$\begingroup\$

Deadfish~, 10529 bytes

{iiiii}dcdcicdciiicdddcicdcic{dddd}ic{iiii}dcdcicdciiiiiicddddddcicdcic{dddd}ic{iiii}icdddcicdciiiiicdddddcicdciiic{dddd}dc{iiii}iiiiic{d}iiicicdcccicdc{i}dddc{dddd}dddddc{iiiii}dddc{d}icicdciiiiiicddddddcicdc{i}dc{ddddd}iiic{iiii}dcccdcicdciccc{dddd}ic{iiii}dcccdciicddciccc{dddd}ic{iiii}dcccdciiiiicdddddciccc{dddd}ic{iiii}dcccdc{i}dddc{d}iiiciccc{dddd}ic{iiii}iiiiicddddddccdciicddcicciiiiiic{dddd}dddddc{iiiii}dddc{d}iiccdc{i}dc{d}icicc{i}ddc{ddddd}iiic{iiii}dcicdcdc{i}ddc{d}iicicicdc{dddd}ic{iiii}iiiiicdddddcdcdciiicdddciciciiiiic{dddd}dddddc{iiii}iiiiicdddddcdcdc{i}ddc{d}iiciciciiiiic{dddd}dddddc{iiiii}dddc{d}iiicdcdciiiiicdddddcicic{i}dddc{ddddd}iiic{iiii}dciicddcdciiicdddciciicddc{dddd}ic{iiii}dcdciicddciiiicddddciicddcic{dddd}ic{iiii}dcdciicddc{i}dddc{d}iiiciicddcic{dddd}ic{iiii}icdddciicddciiicdddciicddciiic{dddd}dc{iiii}icdddciicddc{i}dddc{d}iiiciicddciiic{dddd}dc{iiii}iiiiic{d}iiiciicddcicdciicddc{i}dddc{dddd}dddddc{iiii}iiiiic{d}iiiciicddc{i}dddc{d}iiiciicddc{i}dddc{dddd}dddddc{iiii}iiiiic{d}iiiciicddc{i}ddc{d}iiciicddc{i}dddc{dddd}dddddc{iiii}dccicddciiicdddciicdcc{dddd}ic{iiii}dccicddciiiiiicddddddciicdcc{dddd}ic{iiii}icddcicddciicddciicdciic{dddd}dc{iiii}icddcicddciiiicddddciicdciic{dddd}dc{iiii}icddcicddciiiiicdddddciicdciic{dddd}dc{iiii}icddcicddc{i}ddc{d}iiciicdciic{dddd}dc{iiii}iiiiicddddddcicddccciicdciiiiiic{dddd}dddddc{iiii}iiiiicddddddcicddciicddciicdciiiiiic{dddd}dddddc{iiii}iiiiicddddddcicddciiicdddciicdciiiiiic{dddd}dddddc{iiii}dciccddciiiicddddciiccdc{dddd}ic{iiii}icdccddciiicdddciiccic{dddd}dc{iiii}icdccddc{i}ddc{d}iiciiccic{dddd}dc{iiii}iiiiicdddddccddciiicdddciicciiiiic{dddd}dddddc{iiii}dcdciiicdddccciiicdddcic{dddd}ic{iiii}dcdciiicdddciiiiiicddddddciiicdddcic{dddd}ic{iiii}icdddciiicdddciiiiicdddddciiicdddciiic{dddd}dc{iiii}iiiiic{d}iiiciiicdddccciiicdddc{i}dddc{dddd}dddddc{iiii}iiiiic{d}iiiciiicdddciiiiiicddddddciiicdddc{i}dddc{dddd}dddddc{iiiii}dddc{d}iciiicdddciicddciiicdddc{i}dc{ddddd}iiic{iiii}dcciicdddciiicdddciiicddcc{dddd}ic{iiii}dcciicdddc{i}dc{d}iciiicddcc{dddd}ic{iiii}icddciicdddciicddciiicddciic{dddd}dc{iiii}iiiiicddddddciicdddcicdciiicddciiiiiic{dddd}dddddc{iiii}iiiiicddddddciicdddciiiiiicddddddciiicddciiiiiic{dddd}dddddc{iiii}iiiiicddddddciicdddc{i}dddc{d}iiiciiicddciiiiiic{dddd}dddddc{iiiii}dddc{d}iiciicdddciiiiicdddddciiicddc{i}ddc{ddddd}iiic{iiii}icdcicdddc{i}dddc{d}iiiciiicdcic{dddd}dc{iiii}icdcicdddc{i}dc{d}iciiicdcic{dddd}dc{iiii}iiiiicdddddcicdddciicddciiicdciiiiic{dddd}dddddc{iiiii}dddc{d}iiicicdddciiiicddddciiicdc{i}dddc{ddddd}iiic{iiiii}dddc{d}iiicicdddc{i}dddc{d}iiiciiicdc{i}dddc{ddddd}iiic{iiii}dciiccdddciicddciiiccddc{dddd}ic{iiii}dciiccdddciiiiiicddddddciiiccddc{dddd}ic{iiii}dcdciiiicddddccciiiicddddcic{dddd}ic{iiii}dcdciiiicddddciiicdddciiiicddddcic{dddd}ic{iiii}dcdciiiicddddciiiicddddciiiicddddcic{dddd}ic{iiii}icdddciiiicddddciiiiicdddddciiiicddddciiic{dddd}dc{iiii}icdddciiiicddddc{i}dc{d}iciiiicddddciiic{dddd}dc{iiii}iiiiic{d}iiiciiiicddddc{i}dddc{d}iiiciiiicddddc{i}dddc{dddd}dddddc{iiii}iiiiic{d}iiiciiiicddddc{i}dc{d}iciiiicddddc{i}dddc{dddd}dddddc{iiiii}dddc{d}iciiiicddddc{i}ddc{d}iiciiiicddddc{i}dc{ddddd}iiic{iiiii}dddc{d}iciiiicddddc{i}dc{d}iciiiicddddc{i}dc{ddddd}iiic{iiii}dcciiicddddciiiicddddciiiicdddcc{dddd}ic{iiii}iiiiicddddddciiicddddciiiicddddciiiicdddciiiiiic{dddd}dddddc{iiii}iiiiicddddddciiicddddc{i}ddc{d}iiciiiicdddciiiiiic{dddd}dddddc{iiii}dciciicddddccciiiicddcdc{dddd}ic{iiii}dciciicddddc{i}ddc{d}iiciiiicddcdc{dddd}ic{iiii}icdciicddddciiiiicdddddciiiicddcic{dddd}dc{iiii}icdciicddddc{i}dddc{d}iiiciiiicddcic{dddd}dc{iiiii}dddc{d}iiiciicddddciicddciiiicddc{i}dddc{ddddd}iiic{iiiii}dddc{d}iiiciicddddc{i}dddc{d}iiiciiiicddc{i}dddc{ddddd}iiic{iiiii}dddc{d}iiiciicddddc{i}ddc{d}iiciiiicddc{i}dddc{ddddd}iiic{iiii}icdddciiiiicdddddc{i}dc{d}iciiiiicdddddciiic{dddd}dc{iiiii}dddc{d}iciiiiicdddddciiicdddciiiiicdddddc{i}dc{ddddd}iiic{iiiii}dddc{d}iciiiiicdddddc{i}dc{d}iciiiiicdddddc{i}dc{ddddd}iiic{iiii}dcciiiicdddddccciiiiicddddcc{dddd}ic{iiii}dcciiiicdddddciicddciiiiicddddcc{dddd}ic{iiii}dcciiiicdddddciiiiiicddddddciiiiicddddcc{dddd}ic{iiii}icddciiiicdddddcicdciiiiicddddciic{dddd}dc{iiii}icddciiiicdddddciiiiicdddddciiiiicddddciic{dddd}dc{iiii}icddciiiicdddddc{i}dddc{d}iiiciiiiicddddciic{dddd}dc{iiii}iiiiicddddddciiiicdddddciiiiicdddddciiiiicddddciiiiiic{dddd}dddddc{iiiii}dddc{d}iiciiiicdddddcicdciiiiicddddc{i}ddc{ddddd}iiic{iiiii}dddc{d}iiciiiicdddddciiiiicdddddciiiiicddddc{i}ddc{ddddd}iiic{iiii}dciciiicdddddccciiiiicdddcdc{dddd}ic{iiii}dciciiicdddddcicdciiiiicdddcdc{dddd}ic{iiii}icdciiicdddddciiiiiicddddddciiiiicdddcic{dddd}dc{iiii}icdciiicdddddc{i}ddc{d}iiciiiiicdddcic{dddd}dc{iiii}icdciiicdddddc{i}dc{d}iciiiiicdddcic{dddd}dc{iiii}iiiiicdddddciiicdddddciiiicddddciiiiicdddciiiiic{dddd}dddddc{iiii}dciiciicdddddcicdciiiiicddcddc{dddd}ic{iiii}dciiciicdddddciiiicddddciiiiicddcddc{dddd}ic{iiii}dcdciiiiiicddddddciiiiiicddddddciiiiiicddddddcic{dddd}ic{iiii}icdddciiiiiicddddddc{i}ddc{d}iiciiiiiicddddddciiic{dddd}dc{iiii}dcciiiiicddddddccciiiiiicdddddcc{dddd}ic{iiii}dcciiiiicddddddcicdciiiiiicdddddcc{dddd}ic{iiii}dcciiiiicddddddciiiicddddciiiiiicdddddcc{dddd}ic{iiii}icddciiiiicddddddccciiiiiicdddddciic{dddd}dc{iiii}iiiiicddddddciiiiicddddddciiiicddddciiiiiicdddddciiiiiic{dddd}dddddc{iiiii}dddc{d}iiciiiiicddddddccciiiiiicdddddc{i}ddc{ddddd}iiic{iiiii}dddc{d}iiciiiiicddddddciiiiiicddddddciiiiiicdddddc{i}ddc{ddddd}iiic{iiii}iiiiicdddddciiiicddddddciicddciiiiiicddddciiiiic{dddd}dddddc{iiiii}dddc{d}iiiciiiicddddddccciiiiiicddddc{i}dddc{ddddd}iiic{iiiii}dddc{d}iiiciiiicddddddciiiicddddciiiiiicddddc{i}dddc{ddddd}iiic{iiii}dcdc{i}dddc{d}iiiccc{i}dddc{d}iiicic{dddd}ic{iiii}dcdc{i}dddc{d}iiic{i}dddc{d}iiic{i}dddc{d}iiicic{dddd}ic{iiii}icdddc{i}dddc{d}iiiciiiiicdddddc{i}dddc{d}iiiciiic{dddd}dc{iiiii}dddc{d}ic{i}dddc{d}iiiccc{i}dddc{d}iiic{i}dc{ddddd}iiic{iiii}dcciiiiiic{d}iiic{i}dddc{d}iiic{i}dddcddddddcc{dddd}ic{iiii}icddciiiiiic{d}iiiciiiiiicddddddc{i}dddcddddddciic{dddd}dc{iiii}iiiiicddddddciiiiiic{d}iiiciiiiicdddddc{i}dddcddddddciiiiiic{dddd}dddddc{iiiii}dddc{d}iiciiiiiic{d}iiiccc{i}dddcddddddc{i}ddc{ddddd}iiic{iiiii}dddc{d}iiciiiiiic{d}iiic{i}dc{d}ic{i}dddcddddddc{i}ddc{ddddd}iiic{iiii}dciciiiiic{d}iiiciiiiiicddddddc{i}dddcdddddcdc{dddd}ic{iiii}dciciiiiic{d}iiic{i}dc{d}ic{i}dddcdddddcdc{dddd}ic{iiii}icdciiiiic{d}iiiciiiiicdddddc{i}dddcdddddcic{dddd}dc{iiii}dciiciiiic{d}iiiciiicdddc{i}dddcddddcddc{dddd}ic{iiii}dciiciiiic{d}iiiciiiicddddc{i}dddcddddcddc{dddd}ic{iiii}dciiciiiic{d}iiiciiiiiicddddddc{i}dddcddddcddc{dddd}ic{iiii}dciiciiiic{d}iiic{i}dc{d}ic{i}dddcddddcddc{dddd}ic{iiii}dcdc{i}ddc{d}iic{i}dddc{d}iiic{i}ddc{d}iicic{dddd}ic{iiii}iiiiic{d}iiic{i}ddc{d}iiciiiicddddc{i}ddc{d}iic{i}dddc{dddd}dddddc{iiii}iiiiic{d}iiic{i}ddc{d}iiciiiiicdddddc{i}ddc{d}iic{i}dddc{dddd}dddddc{iiiii}dddc{d}ic{i}ddc{d}iiccc{i}ddc{d}iic{i}dc{ddddd}iiic{iiiii}dddc{d}ic{i}ddc{d}iiciiiiiicddddddc{i}ddc{d}iic{i}dc{ddddd}iiic{iiiii}dddc{d}ic{i}ddc{d}iic{i}dddc{d}iiic{i}ddc{d}iic{i}dc{ddddd}iiic{iiii}icddc{i}dddc{d}iicicdc{i}ddc{d}iiiciic{dddd}dc{iiii}icddc{i}dddc{d}iiciiiiicdddddc{i}ddc{d}iiiciic{dddd}dc{iiii}icddc{i}dddc{d}iic{i}ddc{d}iic{i}ddc{d}iiiciic{dddd}dc{iiii}iiiiicddddddc{i}dddc{d}iiccc{i}ddc{d}iiiciiiiiic{dddd}dddddc{iiii}iiiiicddddddc{i}dddc{d}iic{i}dc{d}ic{i}ddc{d}iiiciiiiiic{dddd}dddddc{iiiii}dddc{d}iic{i}dddc{d}iiciicddc{i}ddc{d}iiic{i}ddc{ddddd}iiic{iiii}dciciiiiiic{d}iic{i}dddc{d}iiic{i}ddcddddddcdc{dddd}ic{iiii}dciciiiiiic{d}iic{i}dc{d}ic{i}ddcddddddcdc{dddd}ic{iiii}icdciiiiiic{d}iiccc{i}ddcddddddcic{dddd}dc{iiii}icdciiiiiic{d}iiciiicdddc{i}ddcddddddcic{dddd}dc{iiii}iiiiicdddddciiiiiic{d}iiciiicdddc{i}ddcddddddciiiiic{dddd}dddddc{iiiii}dddc{d}iiiciiiiiic{d}iiciicddc{i}ddcddddddc{i}dddc{ddddd}iiic{iiii}dciiciiiiic{d}iiciiiicddddc{i}ddcdddddcddc{dddd}ic{iiii}dcdc{i}dc{d}iccc{i}dc{d}icic{dddd}ic{iiii}icdddc{i}dc{d}icicdc{i}dc{d}iciiic{dddd}dc{iiii}icdddc{i}dc{d}ic{i}dddc{d}iiic{i}dc{d}iciiic{dddd}dc{iiii}icdddc{i}dc{d}ic{i}ddc{d}iic{i}dc{d}iciiic{dddd}dc{iiii}iiiiic{d}iiic{i}dc{d}iciiiiiicddddddc{i}dc{d}ic{i}dddc{dddd}dddddc{iiii}iiiiic{d}iiic{i}dc{d}ic{i}ddc{d}iic{i}dc{d}ic{i}dddc{dddd}dddddc{iiiii}dddc{d}ic{i}dc{d}ic{i}dddc{d}iiic{i}dc{d}ic{i}dc{ddddd}iiic{iiii}dcc{i}ddc{d}icicdc{i}dc{d}iicc{dddd}ic{iiii}icddc{i}ddc{d}iciiiiicdddddc{i}dc{d}iiciic{dddd}dc{iiii}icddc{i}ddc{d}ic{i}dc{d}ic{i}dc{d}iiciic{dddd}dc{iiii}iiiiicddddddc{i}ddc{d}icicdc{i}dc{d}iiciiiiiic{dddd}dddddc{iiiii}dddc{d}iic{i}ddc{d}ic{i}ddc{d}iic{i}dc{d}iic{i}ddc{ddddd}iiic{iiii}dcic{i}dddc{d}ic{i}ddc{d}iic{i}dc{d}iiicdc{dddd}ic{iiii}iiiiicdddddc{i}dddc{d}ic{i}ddc{d}iic{i}dc{d}iiiciiiiic{dddd}dddddc{iiii}dcdccicccdccic{dddd}ic{iiii}dcdcciciicddcdccic{dddd}ic{iiii}dcdcciciiiiicdddddcdccic{dddd}ic{iiii}icdddccicdcicdcciiic{dddd}dc{iiii}icdddcciciiiicddddcdcciiic{dddd}dc{iiii}icdddccic{i}dddc{d}iiicdcciiic{dddd}dc{iiii}dccdcicccdcicc{dddd}ic{iiii}icddcdciciicddcdciciic{dddd}dc{iiii}dcicddcic{i}ddc{d}iicdciicdc{dddd}ic{iiii}icdcddciciiicdddcdciicic{dddd}dc{iiii}icdcddciciiiicddddcdciicic{dddd}dc{iiii}iiiiicdddddcddcicdcicdciiciiiiic{dddd}dddddc{iiiii}dddc{d}iiicddcicccdciic{i}dddc{ddddd}iiic{iiii}dciicdddciciiicdddcdciiicddc{dddd}ic{iiii}dciicdddciciiiiiicddddddcdciiicddc{dddd}ic{iiii}dcdcicciiicdddccdcic{dddd}ic{iiii}dcdcicciiiiiicddddddccdcic{dddd}ic{iiii}icdddcicccccdciiic{dddd}dc{iiii}iiiiic{d}iiicicciiicdddccdc{i}dddc{dddd}dddddc{iiii}iiiiic{d}iiicicciiiicddddccdc{i}dddc{dddd}dddddc{iiiii}dddc{d}icicciicddccdc{i}dc{ddddd}iiic{iiii}dcccc{i}dddc{d}iiicccc{dddd}ic{iiii}dcccc{i}ddc{d}iicccc{dddd}ic{iiii}iiiiicddddddccciicddccciiiiiic{dddd}dddddc{iiii}iiiiicddddddccciiiiicdddddccciiiiiic{dddd}dddddc{iiiii}dddc{d}iiccciiiiiicddddddccc{i}ddc{ddddd}iiic{iiii}dcicdcccccicdc{dddd}ic{iiii}dcicdcciicddccicdc{dddd}ic{iiii}dcicdcciiiiicdddddccicdc{dddd}ic{iiii}iiiiicdddddcdcc{i}ddc{d}iicciciiiiic{dddd}dddddc{iiiii}dddc{d}iiicdcc{i}ddc{d}iiccic{i}dddc{ddddd}iiic{iiiii}dddc{d}iciicdciiiicddddcicddc{i}dc{ddddd}iiic{iiiii}dddc{d}iciicdc{i}dddc{d}iiicicddc{i}dc{ddddd}iiic{iiii}dccicdcccicdcc{dddd}ic{iiii}dccicdciiiiicdddddcicdcc{dddd}ic{iiii}dccicdciiiiiicddddddcicdcc{dddd}ic{iiii}iiiiicddddddcicdcccicdciiiiiic{dddd}dddddc{iiii}iiiiicddddddcicdciiiiicdddddcicdciiiiiic{dddd}dddddc{iiiii}dddc{d}iicicdciiiiicdddddcicdc{i}ddc{ddddd}iiic{iiii}icdccdciiiiicdddddciccic{dddd}dc{iiii}dciicdcdc{i}ddc{d}iicicicddc
\$\endgroup\$
0
\$\begingroup\$

Clean, 262... 213 bytes

import StdEnv
@ =(rem)
f=[d\\d<-[a*10^4+b*100+c\\a<-[10^4..99999],b<-[1..12],c<-[1..28+[0,3,if(@a 400<1|| @a 4<1&& @a 100>0)1 0,3,2,3,2,3,3,2,3,2,3]!!b]]|(\k=k==reverse k)[p\\p<-:toString d]&&all((<)0o@d)[2..d-1]]

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Javascript, 234 229 bytes

A bit bulky, but posting it to get the JS ball rolling. Any suggestions welcome!

f=n=>100+10*n+n/10|0
p=n=>{for(i=2;i<n;n=n%i++<1?0:n);return n>1}
q=n=>(''+(100+n)).slice(-2)
r=_=>{for(m=13;--m;)for(d=32;--d;)for(x=10;--x+1;){s=q(f(d))+q(f(m))+x+q(m)+q(d);if(p(s|0)&&d<(m==2?29:31+(m+m/8|0)%2))console.log(s)}}

Ungolfed:

// Flip a one- or two-digit number
f=n=>100+10*n+n/10|0

// Primality test
// For initial testing, you can replace this line with:
//      p=require('primality')
// This uses the primality npm module and is way faster
p=n=>{for(i=2;i<n;n=n%i++<1?0:n);return n>1}

// Convert number to string, pad with zeroes if necessary
q=n=>(''+(100+n)).slice(-2)

r=_=>{
    // Loop months
    for(m=13;--m;)
        // Loop days
        for(d=32;--d;)
            // Loop middle digit
            for(x=10;--x+1;) {
                // Construct 'date'
                s = 
                    // Start with day and month, each flipped
                    q(f(d))+q(f(m)) + 
                    // Add middle digit ( will be casted to string since the previous expression is also a string)
                    x + 
                    // Add month and date as they are
                    q(m)+q(d);

                if(
                    // Check for primality
                    p(s|0) && 
                    // Check if it's a valid date by validating day ( month and year will always be valid)
                    d<(
                        // For February, we always assume 28 days ( check for 29 because we use less than)
                        m==2?29 : 
                        // For other months, it alternates between 31 and 30
                        // EXCEPT July and August both have 31 days, then continues alternating
                        31+(m+m/8|0)%2))
                    console.log(s)
            }
}

How it works:

The digit flipping magic is based mostly on experimenting.
I started by figuring out what number to subtract from to get the flipped version. I only cared about the last two digits.
So, if we take n, find k so that n+k=flip(n). For 10<n<20 k started at 101 and increased in increments of 9. However, for n<10, this was 100. I assumed k increased for every jump of 10, and after a bit of fiddling I figured it was correct.
So, k=100+9*n+n//10 where // means integer division.

Thus, we get n+k = n+(100+9*n+n//10) = 100+10*n+n//10 = flipped(n).

I cannot prove, nor claim that this works for any number, but it produced correct results for the numbers used here.

For the primality test, credit to Kevin Cruijssen's answer. I had a bit shorter version, but I could not get it right:

p=n=>{for(i=n;--i-1;)if(!(n%i))return 1;}

I skipped on the palindrome test, by looping over months, days and a middle digit so I can build strings as dDmMxMmDd, where D is the day's first digit, d is the second, etc.

History

Saved 5 bytes by getting rid of the conditional part of q

q=n=>n<10?'0'+n:(''+n).slice(-2) // from
q=n=>(''+(100+n)).slice(-2)      // to
\$\endgroup\$
2
  • \$\begingroup\$ Sorry for messing around with the byte counts. Slipped in some soft-tabs accidentally. Should be correct now. \$\endgroup\$ Jan 16, 2018 at 11:31
  • \$\begingroup\$ You're only ever using f's result as a parameter to q, so cut out the middle man and write f=n=>''+n%10+(n/10|0), and q's result is always used as a string, so you can write q=n=>n<10?'0'+n:n. \$\endgroup\$
    – Neil
    Jan 17, 2018 at 9:58
0
\$\begingroup\$

APL NARS 626 bytes, 313 chars

f;y;m;d;i;k;v;x;t
t←{60⊥3↑3↓⎕TS}⋄t0←50+t
x←2 12⍴v,v←31 28 31 30 31 30 31 31 30 31 30 31
x[2;2]←29⋄m←d←1⋄y←10000
X:  i←{(0=4∣⍵)∧0≠100∣⍵:1⋄0=400∣⍵:1⋄0}y
A:  →0×⍳y≥1e5
    →0×⍳t≥t0
    k←d+100×m+y×100
    →B×⍳∼k=⍎⌽⍕k⋄→B×⍳∼0πk⋄⎕←k
B:  d+←1
    →A×⍳d≤x[1+i;m]
    d←1⋄→C×⍳∼m=12⋄m←1⋄y+←1⋄→X
C:  m+←1⋄→A

this print what find in 50 second, than stop itself (because otherwise i can not stop the program for copy paste the test, because i not know how to stop the program without close the windows of the interpreter) test:

  f
100111001
100131001
100161001
101030101
101060101
101141101
101171101
102040201
102070201
103000301
103060301
104000401
104030401
104040401
\$\endgroup\$
0
\$\begingroup\$

Julia 0.6, 109 bytes

Link goes to a longer version with two differences:

  1. Checks for primes with hand written function, since the Primes package is not available on TIO.
  2. Iterates over a different date range to not time out.
[s for s in Dates.format(Date(0,1,1):Date(100000,1,1),"YYYYmmdd") if Primes.isprime(parse(s))&&s==reverse(s)]

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.