Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1047775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:21:14+00:00 2026-05-16T16:21:14+00:00

Possible Duplicate: Help with algorithm problem from SPOJ Came across this interview question. Given

  • 0

Possible Duplicate:
Help with algorithm problem from SPOJ

Came across this interview question. Given two n-digit prime numbers, convert the first prime number to the second changing one digit at a time. The intermediate numbers also need to be prime. This needs to be done in the minimum number of steps (checking for primality and changing a digit are considered steps)

E.g. convert 1033 to 8179 (1033->1733->3733->…….->8179)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T16:21:14+00:00Added an answer on May 16, 2026 at 4:21 pm

    Nice challenge for a rainy Monday evening (it is here, anyway!). This can be done using Dijkstra’s algorithm. The first step is to create a graph containing all 4-digit primes. Then use Dijkstra’s algorithm to find the shortest path between the start/end primes. Here’s an implementation in Python:

    #! /usr/bin/python -tt
    
    # run as: findpath start end
    
    import sys
    
    (start, end) = map(int, sys.argv[1:3])
    
    # http://primes.utm.edu/lists/small/10000.txt
    f = open("10000.txt", "r")
    lines = f.readlines()
    f.close
    lines = lines[4:-1] # remove header/footer
    all = "".join(lines) # join lines
    all = all.split()
    all = map(int, all)
    
    # only want the 4-digit primes
    fourdigit = [p for p in all if 1000 <= p and p <= 9999]
    
    # returns digits in a number
    digits = lambda x: map(int, str(x))
    
    # cache of digits for each prime
    digits_for_nums = {}
    
    # returns digits in a number (using cache)
    def digits_for_num(x):
        global digits_for_nums
        if x not in digits_for_nums:
            digits_for_nums[x] = digits(x)
        return digits_for_nums[x]
    
    # returns 1 if digits are same, 0 otherwise
    diff = lambda pair: 1 if pair[0] == pair[1] else 0
    
    # computes number of identical digits in two numbers
    def distance(a, b):
        pair = (a, b)
        pair = map(digits_for_num, pair)
        pair = zip(pair[0], pair[1])
        pair = map(diff, pair)
        same = sum(pair)
        return same
    
    # adjacency list representation of graph of primes
    edges = {}
    
    # construct graph
    for a in fourdigit:
        edges[a] = []
        for b in fourdigit:
            if distance(a, b) == 3:
                edges[a].append(b)
    
    infinity = sys.maxint
    
    def smallest():
        global dist, Q
        minimum = infinity
        which = None
        for v in Q:
            if dist[v] <= minimum:
                which = v
                minimum = dist[v]
        return which
    
    # Dijkstra's algorithm
    dist = {}
    previous = {}
    Q = edges.keys()
    for v in Q:
        dist[v] = infinity
        previous[v] = None
    dist[start] = 0
    while len(Q) > 0:
        u = smallest()
        if dist[u] == infinity:
            break
        Q.remove(u)
        for v in edges[u]:
            alt = dist[u] + 1
            if alt < dist[v]:
                dist[v] = alt
                previous[v] = u
    
    # get path between start/end nodes
    num = end
    path = [num]
    while num != start:
        num = previous[num]
        path.insert(0, num)
    print path
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Need help solving Project Euler problem 200 Similar to this question Project
Possible Duplicate: web2py url validator would you help me on this code? from urllib2
Possible Duplicate: SQL Server Database query help Hi, I have a problem that I
Possible Duplicate: JavaScript: formatting number with exactly two decimals Can some one please help
Possible Duplicate: How does this CSS triangle shape work? Please help me i need
Possible Duplicate: preg_match() Unknown modifier '[' help I am trying to match this pattern
Possible Duplicate: Absolute path & Relative Path This is a very basic question, but
Possible Duplicate: Linq help - Sql trace returns result, but datacontext returning null Question
Possible Duplicate: Import CSV to mysql Right I need some help with this: I
Possible Duplicate: How to set this Layout in iPhone need Some help I draw

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.