0

The problem is as:

The primes 3, 7, 109, and 673, are quite remarkable. By taking any two primes and concatenating them in any order the result will always be prime. For example, taking 7 and 109, both 7109 and 1097 are prime. The sum of these four primes, 792, represents the lowest sum for a set of four primes with this property. Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime.

Now following is the program which is not giving any output in the result list. Please help

LIMIT = 20000

prima = []  #empty list

def Bsearch(lsta,low,high,search):   #Binary search function to search a prime number
    if low>high:
        return 0
    else:
        mid = int((low+high)/2)

        if search<lsta[mid]:
            Bsearch(lsta,low,mid-1,search)
        if search>lsta[mid]:
            Bsearch(lsta,mid+1,high,search)
        if search==lsta[mid]:
            return 1
    return 0

def primes(LIMIT):   #sieve to create prime numbers upto LIMIT
    dic = {}    #empty dictionary
    for i in range(2,LIMIT):
        dic[i] = 1
    for i in range(2,LIMIT):
        for j in range(i,LIMIT):
            if i*j>LIMIT:
                break
            dic[i*j] = 0
    for i in range(2,LIMIT):
        if dic[i]==1:
            prima.append(i)

primes(LIMIT)

result = []

for i in range(0,len(prima)):
    print(str((i/len(prima)*100))+"% list passed")

    tempa = []
    tempa.append(prima[i])
    count = 0
    for j in range(i+1,len(prima)):
        c1 = int(str(prima[j])+str(prima[i]))   #first combination
        c2 = int(str(prima[i])+str(prima[j]))   #second combination
        if(Bsearch(prima,0,len(prima)-1,c1) and Bsearch(prima,0,len(prima)-1,c2)):
            print("small success : "+str(count))
            tempa.append(prima[j])
            count +=1
        if(count==4):
            result.append(tempa)
            print("success!")
            break

for item in result:
    print(item)
5
  • 5
    If you expect it to display something, you might want a print(something) somewhere in your code. Commented Feb 19, 2014 at 14:34
  • @WayneWerner print present at last statement at line 55 Commented Feb 19, 2014 at 14:37
  • You're getting no output at all? Strange, I'm getting IndexError: list index out of range. Is this your most up-to-date code? Commented Feb 19, 2014 at 14:38
  • @Kevin OOps sorry if forgot one amendment please modify Bsearch on line 45 as Bsearch(prima,0,len(prima)-1,c1) -1 should be there in both Bsearch statements Commented Feb 19, 2014 at 14:40
  • Please update your post with those changes. Commented Feb 19, 2014 at 14:51

3 Answers 3

1

Since your search is recursive, you need to return the results of your search.

def Bsearch(lsta,low,high,search):   #Binary search function to search a prime number
  retval = 0
  if low>high:
    retval = 0
  else:
    mid = int((low+high)/2)

    if search == lsta[mid]:
        # Make this test first as allows it to exit at once with success
        retval = 1
    elif search<lsta[mid]:
        retval = Bsearch(lsta,low,mid-1,search)
    else:     # search>lsta[mid] Since only 3 choices elif not needed
        retval = Bsearch(lsta,mid+1,high,search)
  return retval
Sign up to request clarification or add additional context in comments.

Comments

0
LIMIT = 20000

prima = []  #empty list

def Bsearch(lsta,low,high,search):   #Binary search function to search a prime number
    if low>high:
        return 0
    else:
        mid = int((low+high)/2)

        if search<lsta[mid]:
            Bsearch(lsta,low,mid-1,search)
        if search>lsta[mid]:
            Bsearch(lsta,mid+1,high,search)
        if search==lsta[mid]:
            return 1
    return 0

def primes(LIMIT):   #sieve to create prime numbers upto LIMIT
    dic = {}    #empty dictionary
    for i in range(2,LIMIT):
        dic[i] = 1
    for i in range(2,LIMIT):
        for j in range(i,LIMIT):
            if i*j>LIMIT:
                break
            dic[i*j] = 0
    for i in range(2,LIMIT):
        if dic[i]==1:
            prima.append(i)

primes(LIMIT)

result = []

Comments

0
LIMIT = 20000

prima = []  #empty list

def Bsearch(lsta,low,high,search):   #Binary search function to search a prime number
    if low>high:
        return 0
    else:
        mid = int((low+high)/2)

        if search<lsta[mid]:
            Bsearch(lsta,low,mid-1,search)
        if search>lsta[mid]:
            Bsearch(lsta,mid+1,high,search)
        if search==lsta[mid]:
            return 1
    return 0

def primes(LIMIT):   #sieve to create prime numbers upto LIMIT
    dic = {}    #empty dictionary
    for i in range(2,LIMIT):
        dic[i] = 1
    for i in range(2,LIMIT):
        for j in range(i,LIMIT):
            if i*j>LIMIT:
                break
            dic[i*j] = 0
    for i in range(2,LIMIT):
        if dic[i]==1:
            prima.append(i)

primes(LIMIT)

result = []

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.