【问题标题】:calculate n for nlog(n) and n! when time is 1 second. (algorithm takes f(n) microseconds)计算 nlog(n) 和 n!当时间为 1 秒时。 (算法需要 f(n) 微秒)
【发布时间】:2016-10-07 07:43:41
【问题描述】:

鉴于 CLRS 算法书中的以下问题。

对于下表中的每个函数 f(n) 和时间 t,确定 可以在时间 t 内解决的问题的最大大小 n,假设 解决问题的算法需要 f(n) 微秒。

  1. 当时间为 1 秒时,如何计算 f(n)=nlog(n) 的 n?
  2. 如果 f(n)=n,如何计算 n!什么时候是 1 秒?

【问题讨论】:

    标签: algorithm big-o clrs


    【解决方案1】:

    提到该算法需要 f(n) 微秒。然后,可以认为该算法由 f(n) 个步骤组成,每个步骤需要 1 微秒。

    给出的问题表明相关的 f(n) 值受 1 秒的限制。 (即 106 微秒)然后,由于您正在寻找可能满足这些条件的最大 n,您的问题归结为下面给出的不等式。

    1) f(n) = nlog(n) 6

    2) f(n) = n! 6

    我相信,其余的主要是通过代数和对数方程来寻找相关值。

    【讨论】:

      【解决方案2】:

      第一种情况,你可以参考Example of newtons method to calculate cube root牛顿法逼近根或者Lambert W Function。它可能有助于计算 n 的值。根据我的发现,大多数情况下没有其他分析方法可以提供帮助。

      在第二种情况下,python 脚本可以帮助手动计算 n。

      def calFact(n):
          if(n == 0 or n==1):
              return n
          return n*calFact(n-1)
      
      nVal=1
      while(calFact(nVal)<1000000):         # f(n) = n! * 10^-6 sec
          nVal=nVal+1                       # 10^6 = n!
      
      print(nVal)
      

      所以在这种情况下,我们试图找出 n 使得 n!等于或接近 10^6。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-01-01
        • 1970-01-01
        • 2015-06-29
        • 2015-08-13
        • 2015-10-08
        • 1970-01-01
        • 2015-05-25
        相关资源
        最近更新 更多