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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:03:33+00:00 2026-05-15T17:03:33+00:00

So here’s the challenge. Given 2 integers named a and b: // Find the

  • 0

So here’s the challenge. Given 2 integers named a and b:

// Find the 2 largest numbers, smaller than a and b, that are divisible by each other.

// input: a:102,b:53
// output: (102,51)

// input: a:7,b:4
// output: (6,3)

// input: a:3,b:2
// output: (2,2)

The catch is, I don’t want to brute force it. I think it comes out to O(n²).

Here’s the signature for the method:

static Tuple<int, int> Analyze(int a, int b)
{
    if (a % b == 0)
        return new Tuple<int, int>(a, b);
    else
        // Here’s where the algorithm kicks in
}

Here’s a sample implementation:

static Tuple<int, int> Analyze(int left, int right)
{
    if (left % right == 0)
        return new Tuple<int, int>(left, right);
    else
    {
        for (int i = left; i >= right; i--)
        {
            for (int j = right; j >= 0; j--)
                if (i % j == 0)
                    return new Tuple<int, int>(i, j);
                else
                    i--;
        }
    }

    return null;
}

Here’s the test client:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(Analyze(102, 53));
        Console.WriteLine(Analyze(6, 4));
        Console.WriteLine(Analyze(3, 2));
    }
}

There are obviously problems with my implementation, but it’s not bad for starters. For instance when I use 106 and 54 for my arguments, had I not decremented the outter loop variable (i– in the else block), I’d have found a match with 106 and 53. Instead I find a match a 104 and 52, which isn’t quite right but is relatively close to the desired result. However, my sample implementation is much faster than a brute forced approach because I’d never loop anymore than b times, making it O(b). Thoughts, inputs?

  • 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-15T17:03:33+00:00Added an answer on May 15, 2026 at 5:03 pm

    I think this would work, and be pretty straightforward, if I’m not confused, it should find the largest sum(a,b):

    static Tuple Analyze(int a, int b)
    {
        if (a % b == 0)
            return new Tuple(a, b);
        else {
            // The algorithm starts here.
            // This is fairly easy to implement with tail recursion, but not optimal:
            // There are two cases to worry about:
            //   1) 'b' will never divide into 'a' evenly, but 'b-1' might.
            //   2) 'a' doesn't divide evenly by 'b', but 'a-1' might.
            if (a < b*2) return Analyze(a, b-1);
            else return Analyze(a-1, b);
        }
    }
    

    Finding the largest lexicographically is trivial. Just loop from b downto 1.

    You’ll be able to do better if you jump by more than one, obviously. Here’s an example in python (assuming a>b, if it’s not, swap them):

    >>> def analyze(a, b):
    ...   if 0 == a%b: return (a,b)          # If b is a factor, return them.
    ...   if a < b*2: return analyze(a,a/2)  # If b can't factor, adjust 'b' to next.
    ...   return analyze(a-(a%b),b)          # Otherwise, adjust 'a' to next.
    ... 
    >>> map(lambda p: analyze(*p), [(102, 53), (6, 4), (3,2)])
    [(102, 51), (6, 3), (3, 1)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an interesting challenge that I'm wondering if anyone here can give me
Here is a challenge question I've got from Linux system programming lecture. Any of
Here's my (code golf) challenge: Take two arrays of bytes and determine if the
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here's a coding problem for those that like this kind of thing. Let's see
I'm writing a Clojure implementation of this coding challenge , attempting to find the
I'm facing kind of a tricky challenge here, maybe one of you could help
All right, so here's a challenge for all you SQL pros: I have a

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.