C++ int 数组指针递归查找质因数
我正在尝试创建一个函数,该函数可以返回数组(或多组,但我正在尝试使用数组)中给定数字的素因数。
例如,如果我输入 12,我想要得到 2、2 和 3,而不是像集合那样得到 2 和 3。这样我就可以使用它们来查看它是否是 Smith 数,所以我需要单独的数字。
另外,我正在采取递归方法。
我尝试了多种方法(但没有成功)返回数组,包括将初始指针传递到代码中,该指针指向存储数组的空间。
我尝试过在函数中初始化数组然后返回它。
据我所知,我可以从基本情况迭代中获取数组,然后当尝试构造一个大小为 oldArray+1 的新数组以将值复制到其中时,事情会变得混乱。这就是我迷路的地方。
根据我所读到的内容,尽管这不是最有效的实现,但我应该能够使其工作。
我有一个函数 nextPrime(int n)
,给定 n
将返回该数字的下一个素数。
请参阅下面的源代码:
int* find(int n, int p) {
int root = (int) floor(sqrt(n));
if (p > root) {
// Base case, array gets initialized and returned
// depending on value of n and p.
if (n > 1) {
factors = new int[1];
factors[0] = n;
return factors;
}
else {
factors = new int[0];
return factors;
}
}
else
if (n%p == 0){
// Inductive step if p is a factor
int newFloor = (int) floor(n/p);
factors = find(newFloor, p);
// Initialize new array.
int* newFactors;
newFactors = new int[(sizeof(factors) / sizeof(int)) + 1];
// Add p to first slot, fill rest with contents of factors.
factors[0] = p;
for (int i = 0; i < (sizeof(factors) / sizeof(int)); i++) {
newFactors[i+1] = factors[i];
}
return newFactors;
}
else {
// Inductive step p isn't a factor of n
factors = find(n, factors, nextPrime(p));
return factors;
}
}
正如我所说,错误在于返回数组并使用其值,但为什么它似乎从第一次迭代返回 OK?
I am trying to make a function that can return the prime factors of a given number in an array (or multi-set, but I'm trying to use an array).
For example, if I put in 12, I want to get 2, 2, and 3, not 2, and 3 like with a set. This is so that I can use these to see if it is a Smith number or not, so I need the numbers seperately.
Also, I am taking a recursive approach.
I have tried (to no avail) to return the array many ways, including passing an initial pointer into the code which points to a space to store the array.
I've tried just initializing the array in the function and then returning it.
From what I can tell, I can get the array back from the base case iteration and then when trying to construct a new array with size oldArray+1
to copy values to, things get messy. This is where I get lost.
From what I've read, although this isn't the most efficient implementation, I should be able to make it work.
I have a function, nextPrime(int n)
, which given n
will give back the next prime up from that number.
See source below:
int* find(int n, int p) {
int root = (int) floor(sqrt(n));
if (p > root) {
// Base case, array gets initialized and returned
// depending on value of n and p.
if (n > 1) {
factors = new int[1];
factors[0] = n;
return factors;
}
else {
factors = new int[0];
return factors;
}
}
else
if (n%p == 0){
// Inductive step if p is a factor
int newFloor = (int) floor(n/p);
factors = find(newFloor, p);
// Initialize new array.
int* newFactors;
newFactors = new int[(sizeof(factors) / sizeof(int)) + 1];
// Add p to first slot, fill rest with contents of factors.
factors[0] = p;
for (int i = 0; i < (sizeof(factors) / sizeof(int)); i++) {
newFactors[i+1] = factors[i];
}
return newFactors;
}
else {
// Inductive step p isn't a factor of n
factors = find(n, factors, nextPrime(p));
return factors;
}
}
As I say, the error is with returning the array and using its value, but why does it seem to return OK from the first iteration?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
像这样的东西可以工作。效率不是很高!!
调用函数factors后将仅包含素因数。
Something like this could work. Not terribly efficient !!
After you call the function factors will contain only the prime factors.
为此,您应该使用 std::vector 。您遇到的主要问题是指向数组的指针无法知道数组包含的项目数。具体来说,您说
sizeof(factors)
的部分是错误的。据我了解,您期望为您提供由factors
指向的数组中的项目数,但它实际上为您提供了存储指向int 的指针所需的字节数
。您应该返回一个
vector
或将其作为参考传递,并在每次找到因子时更新它。You should be using
std::vector
for this. The main problem you have is that a pointer to an array has no way of knowing the number of items the array contains. Concretely, the part where you saysizeof(factors)
is wrong. As I understand, you're expecting that to give you the number of items in the array pointed to byfactors
, but it really gives you the number of bytes needed to store a pointer toint
.You should be either returning a
vector<int>
or passing it in as a reference and updating it each time you find a factor.