wu :: forums
« wu :: forums - C question (find width and height of a rectangle g »

Welcome, Guest. Please Login or Register.
Jun 2nd, 2024, 7:32am

RIDDLES SITE WRITE MATH! Home Home Help Help Search Search Members Members Login Login Register Register
   wu :: forums
   riddles
   cs
(Moderators: william wu, ThudnBlunder, SMQ, Eigenray, towr, Grimbal, Icarus)
   C question (find width and height of a rectangle g
« Previous topic | Next topic »
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print
   Author  Topic: C question (find width and height of a rectangle g  (Read 726 times)
Spectre John
Guest

Email

C question (find width and height of a rectangle g  
« on: Jan 13th, 2005, 8:55pm »
Quote Quote Modify Modify Remove Remove

Given area of a rectangle, write an efficient C code that find the 'best' rectangle for the area. The 'best' rectangle is defined as the rectangle where the difference between its width and height is the smallest between all other possible rectangles (area, width, and height are all integers). For example, if area = 24, the 'best' width and height would be 4 and 6 and not 2 and 12 or 1 and 26.
IP Logged
Barukh
Uberpuzzler
*****






   


Gender: male
Posts: 2276
Re: C question (find width and height of a rectang  
« Reply #1 on: Jan 13th, 2005, 10:46pm »
Quote Quote Modify Modify

Interesting!  
 
Note that if you can give an efficient algorithm for that, you will break the RSA public key encryption.
IP Logged
Spectre John
Guest

Email

Re: C question (find width and height of a rectang  
« Reply #2 on: Jan 13th, 2005, 10:47pm »
Quote Quote Modify Modify Remove Remove

Really? I was aked this question during interview once, and it really stumped me...
IP Logged
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: C question (find width and height of a rectang  
« Reply #3 on: Jan 13th, 2005, 11:33pm »
Quote Quote Modify Modify

One simple solution would start at the squareroot of the area, and then work upwords (or downwards) untill you have a length that divides the area.
You could also try to find factors of the area, but using those appropriatley will definitely take more programming time.
IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
John_Gaughan
Uberpuzzler
*****



Behold, the power of cheese!

5187759 5187759   john23874   SnowmanJTG
WWW Email

Gender: male
Posts: 767
Re: C question (find width and height of a rectang  
« Reply #4 on: Jan 21st, 2005, 8:06pm »
Quote Quote Modify Modify

towr's idea reminds me of this thread. Maybe something there is applicable to this one? Namely to use the built-in sqrt() function and work from there Wink
IP Logged

x = (0x2B | ~0x2B)
x == the_question
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print

« Previous topic | Next topic »

Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board