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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T09:59:25+00:00 2026-05-16T09:59:25+00:00

Given an integer N I want to find two integers A and B that

  • 0

Given an integer N I want to find two integers A and B that satisfy A × B ≥ N with the following conditions:

  1. The difference between A × B and N is as low as possible.
  2. The difference between A and B is as low as possible (to approach a square).

Example: 23. Possible solutions 3 × 8, 6 × 4, 5 × 5. 6 × 4 is the best since it leaves just one empty space in the grid and is “less” rectangular than 3 × 8.

Another example: 21. Solutions 3 × 7 and 4 × 6. 3 × 7 is the desired one.

A brute force solution is easy. I would like to see if a clever solution is possible.

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

    Easy.

    In pseudocode

    a = b = floor(sqrt(N))
    
    if (a * b >= N) return (a, b)
    
    a += 1
    if (a * b >= N) return (a, b)
    
    return (a, b+1)
    

    and it will always terminate, the distance between a and b at most only 1.

    It will be much harder if you relax second constraint, but that’s another question.

    Edit: as it seems that the first condition is more important, you have to attack the problem
    a bit differently. You have to specify some method to measure the badness of not being square enough = 2nd condition, because even prime numbers can be factorized as 1*number, and we fulfill the first condition. Assume we have a badness function (say a >= b && a <= 2 * b), then factorize N and try different combinations to find best one. If there aren’t any good enough, try with N+1 and so on.

    Edit2: after thinking a bit more I come with this solution, in Python:

    from math import sqrt
    
    def isok(a, b):
      """accept difference of five - 2nd rule"""
      return a <= b + 5
    
    def improve(a, b, N):
      """improve result:
        if a == b:
           (a+1)*(b-1) = a^2 - 1 < a*a
        otherwise (a - 1 >= b as a is always larger)
          (a+1)*(b-1) = a*b - a + b - 1 =< a*b
    
        On each iteration new a*b will be less,
        continue until we can, or 2nd condition is still met
      """
      while (a+1) * (b-1) >= N and isok(a+1, b-1):
        a, b = a + 1, b - 1
    
      return (a, b)
    
    def decomposite(N):
      a = int(sqrt(N))
      b = a
    
      # N is square, result is ok
      if a * b >= N:
        return (a, b)
    
      a += 1
    
      if a * b >= N:
        return improve(a, b, N)
    
      return improve(a, b+1, N)
    
    def test(N):
      (a, b) = decomposite(N)
    
      print "%d decomposed as %d * %d = %d" % (N, a, b, a*b)
    
    [test(x) for x in [99, 100, 101, 20, 21, 22, 23]]
    

    which outputs

    99 decomposed as 11 * 9 = 99
    100 decomposed as 10 * 10 = 100
    101 decomposed as 13 * 8 = 104
    20 decomposed as 5 * 4 = 20
    21 decomposed as 7 * 3 = 21
    22 decomposed as 6 * 4 = 24
    23 decomposed as 6 * 4 = 24
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

var i : integer; i := 1234567; Given the above, I want the string
Given the following Delphil DLL declaration function csd_HandleData(aBuf: PChar; aLen: integer): integer; stdcall; what
Given two integers a and b , how would I go about calculating the
I've got a System.Generic.Collections.List(Of MyCustomClass) type object. Given integer varaibles pagesize and pagenumber, how
Given an integer typedef: typedef unsigned int TYPE; or typedef unsigned long TYPE; I
You are given a 32-bit unsigned integer array with length up to 2 32
I need to pad the output of an integer to a given length. For
Given an array of integers, what is the simplest way to iterate over it
Given a specific DateTime value, how do I display relative time, like: 2 hours
Given a Python object of any kind, is there an easy way to get

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.