这个 C++ 的时间复杂度是多少?功能?

发布于 2024-11-02 05:59:19 字数 465 浏览 0 评论 0原文

我编写了一个函数来显示谁是质数以及特定数字 n 的因数。

bool PrimeFactor(int n){
    int count = 0;// count divisors

    for (int i = 2 ; i < n; i++){
        if ( n % i == 0 ){
            if ( PrimeFactor(i)){              
                cout << i << endl;
            }
            count ++;
        }
    }

    if (count > 0)//not prime
        return false;
    return true;
}

结果可能在某些地方重复,但这不是什么大问题。我不知道如何计算这个递归 func 的时间复杂度。

I have written a function to show both number who is prime and factor of a specific number n.

bool PrimeFactor(int n){
    int count = 0;// count divisors

    for (int i = 2 ; i < n; i++){
        if ( n % i == 0 ){
            if ( PrimeFactor(i)){              
                cout << i << endl;
            }
            count ++;
        }
    }

    if (count > 0)//not prime
        return false;
    return true;
}

The result may be duplicated in some place, but that's not the big matter. I don't know how to calculate the time complexity of this recursive func .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

是你 2024-11-09 05:59:19

这基本上是 Ben Voigt 答案的更扩展版本。

正如 Ben Voigt 所说,没有条件的版本是 O(n),这应该很简单。

现在,带有条件的版本将在 if 语句内执行递归,次数等于 n 的除数数 (= n 的除数函数值 = d(n))。

下限inf d(n) = 2,因为对于每个素数,这都是正确的,并且素数有无限多个,所以无论你使n有多大,您总能找到 d(n) = 2 的一个。这意味着对于素数,您的函数将递归 0 次,并且复杂度为 O(n)

上限更复杂(我需要咖啡),所以让我们暂时跳过它并计算平均复杂度。平均复杂度为 d(n) = O(log n) ,因此,正如 Ben Voigt 所说,原始函数的平均复杂度为 O(n log n loglog n 。 ..)。更详细地说:您有一个 for 循环,即 O(n),在这个 for 循环中,您将递归平均 d(n) = O(log n) > 次。现在您再次输入该函数并递归 O(log (log n)) 次,依此类推。

另请注意 DarkDust & 对您问题的评论。杰夫·福斯特。它也不会按照您想要的方式运行。此外,检查偶数是否整除 n 是没有用的,因为偶数永远不会是素数(当然 2 除外)。由于递归,您将在递归调用期间输入内部 if (带有 cout 的那个),因此您得到的输出不会是您想要的(我是这样的) 'm 假设 是 n) 的不同质因数。

另一种节省时间的方法是仅测试 floor(sqrt(n))。如果数字i 能整除n,请检查商j = n / i 是否也是质数。例如,对于 n = 6,您最多可以测试 floor(sqrt(6)) = 2。然后您发现 i = 2 是一个除数,并检查 j = 6 / 2 = 3。您会发现在这种情况下,ij 都是素因数。

This is basically a more extended version of Ben Voigt answer.

As Ben Voigt said, the version without the conditional is O(n), this should be straightforward.

Now, the version with the conditional will execute the recursion inside the if statement a number of times equal to the number of divisors of n (= value of the divisor function for n = d(n)).

The lower limit inf d(n) = 2, since for every prime, this will be true and there are infinitely many primes, so no matter how big you make n, you can always find one for which d(n) = 2. This means that for primes, your function will recurse 0 times and it has complexity O(n).

The upper limit is more complicated (and I need coffee), so lets skip that for a moment and calculate the average complexity. The average complexity of d(n) = O(log n), so, as stated by Ben Voigt, the original function will have an average complexity of O(n log n loglog n ...). More in detail: you have the for loop, which is O(n), in this for loop you will recurse an average of d(n) = O(log n) times. Now you enter the function again and recurse O(log (log n)) times, etc, etc.

Also note the comments to your question by DarkDust & Jeff Forster. It will not function the way you want it too. Furthermore, checking if even numbers divide n is useless, since even numbers will never be primes (except for 2 of course). Due to the recursion, you will enter the inner if (the one with cout) during recursive calls, so the output you get, will not be what you want (which I'm assuming is the distinct prime divisors of n).

Another way to save time is by only testing up to floor(sqrt(n)). If a number i divides n exactly, check if the quotient j = n / i is also a prime number. E.g. for n = 6, you'd test up to floor(sqrt(6)) = 2. Then you find that i = 2 is a divisor and you check j = 6 / 2 = 3. You find that both i and j are prime divisors in this case.

楠木可依 2024-11-09 05:59:19

这种简化的递归次数不会少于原始的次数,并且其复杂度为 O(n!)。所以这是一个上限。

bool PrimeFactor(int n)
{
    for (int i = 2 ; i < n; i++) PrimeFactor(i);
}

我认为原始的复杂度是O(n log n loglog n ...)

This simplification will recurse no fewer times than the original, and it has complexity O(n!). So that's an upper bound.

bool PrimeFactor(int n)
{
    for (int i = 2 ; i < n; i++) PrimeFactor(i);
}

I think the complexity of the original is O(n log n loglog n ...).

孤独岁月 2024-11-09 05:59:19

我尝试以正式的方式翻译(到递归关系)并解决您的算法,如下所示:

在此处输入图像描述

增长的阶数是非多项式,这是一种要避免的算法!

I have attempted to translate (to a recurrence relation) and solve your algorithm in a formal way, like the following:

enter image description here

The order of growth is non polynomial, which is an algorithm to avoid!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文