Consecutive Primes

Define sum ( N ) \text{sum}(N) which returns the sum of digits of a number.

Find the smallest pair of consecutive primes a a and b b which such that sum ( a ) = sum ( b ) \text{sum}(a) = \text{sum}(b) . Enter your answer as a + b a + b .

Details and Assumptions :

  • Consecutive prime numbers refers to a sequence of two prime numbers which don't have any prime number between them. For example: 2 and 3 are consecutive primes, 37 and 41 are consecutive primes.

  • As an explicit example: sum ( 37 ) sum ( 41 ) \text{sum}(37) \neq \text{sum}(41) because 3 + 7 4 + 1 3+7\neq 4+1 . So 37 and 41 are not such numbers.


The answer is 1064.

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.

5 solutions

David Holcer
Apr 8, 2015
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
def primes(n):
    sieve = [True] * n
    for i in xrange(3,int(n**0.5)+1,2):
        if sieve[i]:
            sieve[i*i::2*i]=[False]*((n-i*i-1)/(2*i)+1)
    return [2] + [i for i in xrange(3,n,2) if sieve[i]]
def summm(n):
    summ=0
    for i in str(n):
        summ+=int(i)
    return summ
p=primes(1000)
for i in xrange(len(p)-1):
    if summm(p[i])==summm(p[i+1]):
        print p[i]+p[i+1]

Primes takes n and returns array with prime values less than n, so for this case I used primes(1000) and set it as p which I then added consecutive primes when I found them with summm(number) which was function for sum of digits of number. Lol explanation almost longer than code.

Finn Hulse
Apr 8, 2015

The first step is to realize that if two prime numbers are consecutive, there are several things that can happen.

Case 1:

They both share all of the same digits, except their ones digits. For example, 263 and 269 both are the same except for their ones digit. Clearly, the two of these numbers will never have the same digit sum!

Case 2:

They do not share a tens digit. For example, 19 and 23 or 29 and 31. We know that primes after 2 are always odd, and so if two consecutive primes have tens digits which differ by 1, then their respective digit sums must differ in parity, thus this can never happen.

Although, if the tens digits of the primes differ by 2, it is possible for their digit sum to be the same. Our search is now for an entire group of numbers which have no primes among them and share all but their units digit in common. We find success with the 530's, where all of 530, 531, 532, etc. are composite, and indeed the bordering primes are 523 and 541. The answer is thus 1064.

Bill Bell
Apr 8, 2015

I avoid defining the identifier sum because it's already in use within standard Python.

 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
def sum(a):
    q=len(str(a))
    w=0
    e=0
    while w<q:
        e+=int(str(a)[w])
        w+=1
    return e
def sum(a):
    q=len(str(a))
    w=0
    e=0
    while w<q:
        e+=int(str(a)[w])
        w+=1
    return e

a=2
b=0
d=[]
while b==0:
    c=2
    while c<a:
        if a%c==0:
            c+=a
        else:
            c+=1
    if a==c:
        d.append(a)
        if len(d)==2:
            if sum(d[0])==sum(d[1]):
                b+=1
            else:
                d=[]
                a+=1
        else:
            a+=1
    else:
        a+=1
print d[0]+d[1]

Maxwell Feiner
Sep 30, 2015

I am not a very advanced programmer, so my program is a bit long and inefficient, but for the given parameters of the problem it works fine.(program in python)

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...