9
\$\begingroup\$

This year my age is a prime number, and so is this year. This conjunction will repeat in 10 years and again in 12. If I live to 100, I will lived exactly 11 years in which my age and the year are both prime. My condolences to those of you born in odd-numbered years, who will experience this phenomenon at most once if at all.

As it turns out, if I tell you that the consecutive gaps between years in which my age and the year were, are, or will be both primes are 4, 2, 4, 14, 10, 2, 10, 14, 16, and 14, then that is enough information for you to tell me exactly which year I was born.

The Challenge

This challenge is to write a program or function that, given a list of even numbers, each representing gaps between consecutive prime-age prime-year conjunctions, print or return all years between 1AD and 2100AD inclusive such that a person born in that year (and living no longer than 100 years) would have exactly that sequence of gaps between prime-year prime-age conjuctions (i.e. no other such conjunctions)

Or, in more mathematical terms, given a list \$a\$, print or return all \$x\$ between 1 and 2100 such that there exists a \$0<b\le100\$ such that \$x+b\$ is prime, \$b\$ is prime, and \$x+b+\sum^n_{i=1}a_i\$ and \$b+\sum^n_{i=1}a_i\$ are both prime for all \$n\$ up to the length of \$a\$ (where the latter sum is guaranteed to be less than 100).

If there are no such years in that range, you may print or return anything falsy or nothing at all.

I/O is flexible: just be able to take a list of numbers as input and produce a list of numbers as output.

Examples

[4,2,4,14,10,2,10,14,16,14]          =>     [1986]
(6,4,2,4)                            =>     ()
6,4,2,4,8,6,10,12,14,10,6            =>     1980
4;4                                  =>     (No output)
[]...................................=>     (A list containing every odd number between 1 and 2099, plus the numbers 1136, 1732, and 1762)

Scoring

Shortest program, in bytes, wins.

Bonus: I will create a bounty to reward the shortest answer that produces the correct output using an algorithm which has an asymptotic worst case running time strictly better than \$O(YA+A^2)\$ where Y is the maximum birth year and A is the maximum age. (Y and A are not inputs to your program or function, but pretend they are for the purposes of determining if your algorithm qualifies.) If you wish to compete in the bonus challenge, it is up to you to convince me your program or function meets the requirements. You may assume that all built-ins use the asymptotically fastest known classical algorithm to achieve their results (e.g. generates primes with a Sieve of Atkin rather than a Sieve of Eratosthenes). Yes, it is possible to receive this bounty even if you win the regular challenge.

\$\endgroup\$
13
  • 1
    \$\begingroup\$ I was born in an odd numbered year, and have a prime age right now. Its not only for people born in even numbered years ;) \$\endgroup\$
    – Wheat Wizard
    May 31, 2017 at 15:35
  • 3
    \$\begingroup\$ Yeah - I was born in a prime year and am currently a prime age. Maybe consider changing it to "will turn a prime age". \$\endgroup\$ May 31, 2017 at 15:36
  • 1
    \$\begingroup\$ What’s special about the numbers 1136, 1732, and 1762? \$\endgroup\$
    – Grimmy
    May 31, 2017 at 15:45
  • 1
    \$\begingroup\$ O(YA+A^2) is the same as O(1), since Y and A are both constants (respectively 2100 and 100, according to the challenge definition). Should answers to the “bonus challenge” take Y and A as input? \$\endgroup\$
    – Grimmy
    May 31, 2017 at 15:49
  • \$\begingroup\$ Since the input is described as "would have exactly that sequence of gaps", then the output for the empty input should not include odd years two less than a prime. \$\endgroup\$ May 31, 2017 at 15:55

3 Answers 3

4
\$\begingroup\$

05AB1E, 18 bytes

2101GтLDpÏN+DpÏ¥Q–

Try it online!

Explanation

2101G               # for each N in [1 ... 2100]
     тL             # push the range [1 ... 100]
       DpÏ          # keep only numbers that are prime
          N+        # add N to each
            DpÏ     # keep only numbers that are prime
               ¥    # calculate delta's
                Q–  # print N if the resulting list is equal to input
\$\endgroup\$
2
\$\begingroup\$

Jelly, 19 bytes

97ÆR+⁸ÆPÐfI⁼
⁽¥cçÐf

Try it online!

How it works

⁽¥cçÐf        Main link. Argument: A (array of integers)

⁽¥c           Yield 2100.
   çÐf        Apply the helper link to each n in [1, ..., 2100], with right
              argument A. Yield all values of n that return 1.


97ÆR+⁸ÆPÐfI⁼  Helper link. Left argument: n. Right argument: A

97ÆR          Prime range; yield all prime numbers in [1, ..., 97].
    +⁸        Add n to each of these primes.
      ÆPDf    Filter by primality, keeping only prime sums.
          I   Increments; compute all forward differences.
           ⁼  Return 1 if the result and A are equal, 0 if not.
\$\endgroup\$
0
\$\begingroup\$

Pyth, 24 bytes

fq.+f&P_+TYP_YU100QU2100

Try it online!

I feel like this can be golfed down quite a bit, but here it is for now.

\$\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.