找到最大.任何给定数字的功率值小于另一个给定数字

Finding the max. value of power of any given number which is less than another given number

本文关键字:数字 功率 小于 另一个 任何      更新时间:2023-10-16

对于给定值'a'和y',如何找到最大x值,以使x = a^b<y对于b∈N和a> 0。例如,给出y = 14,a = 2,然后x必须为8。换句话说,对于[8.15]中的所有y值,x必须为8。同样,对于[9,26]中y的所有值,x必须为9。

您可以使用基础a使用日志。<cmath>中不存在这样的功能,但是如果您记得

的公式
log (base a, c) = log (base e, c) / log (base e, a)

您可以使用CMATH的日志(自然对数)函数进行操作。

int exponent = log(y)/log(a); //truncates to the floor, just what we need.
int answer = a to the power of exponent

相当明显的算法任务...取y的基础-A对数的整数部分,并将A提高到该力量:

#include <cmath>
int exponent = (int)(log(y) / log(a)); // base-a logarithm of y, truncated to a whole number
int x = (int)pow(a, exponent); // a raised to that power