-1
\$\begingroup\$

This is a short one:

Only once in American history has there been a decade in which four of the years were prime numbers.

Your program has to output the decade after 1776 where four its years were primes. This is code golf so the shortest code wins.

The output must be like so:

1710s

or

1980s

Note: The answer must be calculated and not explicitly hardcoded into your program.

\$\endgroup\$
  • 5
    \$\begingroup\$ what exactly counts as "hardcoding the answer"? What is our program allowed to know? Maybe you should ask us to find the first such decade after a given year. \$\endgroup\$ – John Dvorak Aug 30 '14 at 14:57
  • 4
    \$\begingroup\$ I know you know about the sandbox, so why don't you use it? A question which has been edited at least twice in the 15 minutes since it was posted would clearly have benefitted from it. \$\endgroup\$ – Peter Taylor Aug 30 '14 at 15:08
  • 1
    \$\begingroup\$ @Peter I thought I would be a simple enough question to go and post it. \$\endgroup\$ – Beta Decay Aug 30 '14 at 15:11
  • 3
    \$\begingroup\$ @BetaDecay constant-output code-golfs which aren't kolmogorov-complexity golfs are always problematic. \$\endgroup\$ – John Dvorak Aug 30 '14 at 15:12
  • 2
    \$\begingroup\$ I nearly always think my question ideas are clear and worthwhile, but I still post them in the sandbox first. Very few of them get any further... It saves a lot of confusion. \$\endgroup\$ – trichoplax Aug 30 '14 at 23:34
4
\$\begingroup\$

CJam, 24 bytes

1770{A+_A,f+{mp},,4<}g's

At the cost of two extra bytes, this program can be modified to calculate the first decade containing four prime years after (not containing) a user-defined year:

liA/A*{A+_A,f+{mp},,4<}g's

Example runs

$ cjam <(echo "1770{A+_A,f+{mp},,4<}g's"); echo
1870s
$ cjam <(echo "liA/A*{A+_A,f+{mp},,4<}g's") <<< 1776; echo
1870s
$ cjam <(echo "liA/A*{A+_A,f+{mp},,4<}g's") <<< 1870; echo
2080s
$ cjam <(echo "liA/A*{A+_A,f+{mp},,4<}g's") <<< 2080; echo
3250s
$ cjam <(echo "liA/A*{A+_A,f+{mp},,4<}g's") <<< 3250; echo
3460s

How it works

li                          " Read an integer “Y” from STDIN.                 ";
  A/A*                      " Execute “Y = Y / 10 * 10”.                      ";
      {             4<}g    " While the result of the loop is less than four: ";
       A+                   " Execute  “Y += 10”.                             ";
         _A,f+              " Push [ Y Y+1 Y+2 Y+3 Y+4 Y+5 Y+6 Y+7 Y+8 Y+9 ]. ";
              {mp},,        " Compute the number of primes in that range.     ";
                        's  " Push 's'.                                       ";
\$\endgroup\$
1
\$\begingroup\$

Python - 105 103 87

Iterating over ith primes using sympy's prime function:

import sympy
p=sympy.prime
for i in range(275,305):
 if p(i)>p(i+3)-9:print`p(i)-1`+'s'

Alternative solution iterating over years from 1780 to 2010 and counting primes per decade:

r=range
for d in r(1780,2010,10):
 if sum(all([y%i>0for i in r(2,y)])for y in r(d,d+10))>3:print`d`+'s'
\$\endgroup\$

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