algorithm - 找到最接近给定自然数,满足非线性条件

标签 algorithm integer

我有三个整数:ABC 我需要找到整数 X,它是最接近 C。如果N是任意自然数,则ABX应满足以下等式:

A*B*X=sqrt(N)

你能帮忙做算法吗?

最佳答案

我们可以对N的所有可能值进行二分查找,然后比较X = sqrt(N)/(A*B))的对应值来决定哪一半进行搜索。

在 python 中的可能实现可能是 -

A = 5
B = 2
C = 3

left = -10000000000000
right = 10000000000000 #assuming that's the maximum value N can take
while right-left>1:
    N = (left+right)//2
    X = N**.5/(A*B)
    if X>C:
        right = N
    else:
        left = N

N = (left+right)//2
print(N)

在这种情况下输出:900

关于algorithm - 找到最接近给定自然数,满足非线性条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47772284/

相关文章:

javascript - 如何将字符串拆分为给定数量的行?

javascript - 如何检测用户是否使用 canvas 和 javascript 在触摸设备上画了一个圆圈?

c - 将包含数字和字母的字符串放入结构中的 int 指针中

input - 如何在 Rust 1.0 中读取用户输入的整数?

c - Linux 的 GCC 替代方案,支持 OpenMP 和带 +、-、*、/和 % 的 128 位整数

javascript - 如何在javascript中将整数数组转换为字符串而不丢失方括号

python-3.x - python : How to replace "datetime" in a Dataframe with only the Day represented as integer value?

algorithm - 计算最小可能的树

sql-server - PK 和 FK 查找算法

算法 - 找到子数组中最小元素的索引