输入一个整数,找到最接近的两个整数其中,乘时,等于输入 [英] Input an integer, find the two closest integers which, when multiplied, equal the input

查看:184
本文介绍了输入一个整数,找到最接近的两个整数其中,乘时,等于输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧我的问题是数学的性质。
我有一个字节数组,其长度为X,我需要找到其相乘等于X的两个最接近的数字
我需要这样做,因为我是从bulding的字节数组位图,我需要做的位图看起来像一个方形尽可能。
我在C#中,但有关语法别担心这个编码,任何算法或伪code会做。
在此先感谢您的帮助。

Ok my problem is of mathematical nature. I have an array of bytes whose length is X, i need to find the two closest numbers which multiplied together equal X. I need to do this because i am bulding a bitmap from an array of bytes and i need to make the bitmap look like a square as much as possible. I am coding this in C# but don' t worry about syntax, any algorithm or pseudo-code will do. Thanks in advance for your help

推荐答案

有可能是这更好的算法,但把我的头顶部:

There's probably a better algorithm for this, but off the top of my head:

1) Take the square root of the number X; we'll call it N.
2) Set N equal to the ceiling of N (round up to the nearest integer).
3) Test for (X % N). If N divides evenly into X, we found our first number.
  if 0, divide X by N to get M. M and N are our numbers
  if not 0, increment N by 1 and start step 3 over.

这篇关于输入一个整数,找到最接近的两个整数其中,乘时,等于输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆