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 6802933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:16:58+00:00 2026-05-26T19:16:58+00:00

As a single operation between two positive integers we understand multiplying one of the

  • 0

As a single operation between two positive integers we understand
multiplying one of the numbers by some prime number or dividing it by
such (provided it can be divided by this prime number without
the remainder). The distance between a and b denoted as d(a,b) is a
minimal amount of operations needed to transform number a into number
b. For example, d(69,42)=3.

Keep in mind that our function d indeed has characteristics of the
distance – for any positive ints a, b and c we get:

a) d(a,a)==0

b) d(a,b)==d(b,a)

c) the inequality of a triangle d(a,b)+d(b,c)>=d(a,c) is fulfilled.

You’ll be given a sequence of positive ints a_1, a_2,…,a_n. For every a_i of them
output such a_j (j!=i) that d(a_i, a_j) is as low as possible. For example, the sequence of length 6: {1,2,3,4,5,6} should output {2,1,1,2,1,2}.

This seems really hard to me. What I think would be useful is:

a) if a_i is prime, we are unable to make anything less than a_i (unless it’s 1) so the only operation allowed is multiplication. Therefore, if we have 1 in our set, for every prime number d(this_number, 1) is the lowest.

b) also, for 1 d(1, any_prime_number) is the lowest.

c) for a non-prime number we check if we have any of its factors in our set or multiplication of its factors

That’s all I can deduce, though. The worst part is I know it will take an eternity for such an algorithm to run and check all the possibilities… Could you please try to help me with it? How should this be done?

  • 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-26T19:16:59+00:00Added an answer on May 26, 2026 at 7:16 pm

    Indeed, you can represent any number N as 2^n1 * 3^n2 * 5^n3 * 7^n4 * … (most of the n’s are zeroes).

    This way you set a correspondence between a number N and infinite sequence (n1, n2, n3, …).

    Note that your operation is just adding or subtracting 1 at exactly one of the appropriate sequence’s places.

    Let N and M be two numbers, and their sequences be (n1, n2, n3, …) and (m1, m2, m3, …).
    The distance between the two numbers is indeed nothing but |n1 – m1| + |n2 – m2| + …

    So, in order to find out the closest number, you need to calculate the sequences for all the input numbers (this is just decomposing them into primes). Having this decomposition, the calculation is straightforward.


    Edit:
    In fact, you don’t need the exact position of your prime factor: you just need to know, which is the exponent for each of the prime divisors.


    Edit:
    this is the simple procedure for converting the number into the chain representation:

    #include <map>
    
    typedef std::map<unsigned int, unsigned int> ChainRepresentation;
    // maps prime factor -> exponent, default exponent is of course 0
    
    void convertToListRepresentation(int n, ChainRepresentation& r)
    {
        // find a divisor
        int d = 2;
    
        while (n > 1)
        {
            for (; n % d; d++)
            {
                if (n/d < d) // n is prime
                {
                    r[n]++;
                    return;
                }
            }
    
            r[d]++;
            n /= d;
        }
    }
    

    Edit:
    … and the code for distance:

    #include <set>
    
    unsigned int chainDistance(ChainRepresentation& c1, ChainRepresentation& c2)
    {
        if (&c1 == &c2)
            return 0; // protect from modification done by [] during self-comparison
    
        int result = 0;
    
        std::set<unsigned int> visited;
        for (ChainRepresentation::const_iterator it = c1.begin(); it != c1.end(); ++it)
        {
            unsigned int factor = it->first;
            unsigned int exponent = it->second;
            unsigned int exponent2 = c2[factor];
            unsigned int expabsdiff = (exponent > exponent2) ?
                           exponent - exponent2 : exponent2 - exponent;
            result += expabsdiff;
            visited.insert(factor);
        }
    
        for (ChainRepresentation::const_iterator it = c2.begin(); it != c2.end(); ++it)
        {
            unsigned int factor = it->first;
            if (visited.find(factor) != visited.end())
                continue;
            unsigned int exponent2 = it->second;
            // unsigned int exponent = 0;
            result += exponent2;
        }
    
        return result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the difference between a single precision floating point operation and double precision
I often commit files with similar cvs comment but not in a single operation.
Trying to perform a single boolean NOT operation, it appears that under MS SQL
Single Source shortest Path Dijkstra's - directed and undirected - works only for positive
Often I write functions whose operation depends depends on the type of some argument.
If I had a single server and I had two process types A(Many processes
From wikipedia: the cross product is a binary operation on two vectors in a
I'm about writing a CUDA kernel to perform a single operation on every single
The application uses ADO.NET to invoke sprocs for nearly every database operation. Some of
Nested If or single if with And operator, which is better approach? Single If

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.