【问题标题】:Compile time fibonacci which handle large numbers处理大数的编译时间斐波那契
【发布时间】:2015-10-08 21:15:08
【问题描述】:

我在玩这个compile time implementation

我使用ttmath.org 来处理大量数字。 ttmath::UInt<SIZE> 适用于运行时 fib() 函数,但是 我不知道如何为我的元函数处理大数字,因为我必须更改非模板参数size_t

#include <iostream>
#include <omp.h>
#include <ctime>
#include "ttmath/ttmath.h"
#include <type_traits>

#define SIZE 1090

// how can I use ttmath here ?
template<size_t N>
struct fibonacci : std::integral_constant<size_t, fibonacci<N-1>{} + fibonacci<N-2>{}> {};

template<> struct fibonacci<1> : std::integral_constant<size_t,1> {};
template<> struct fibonacci<0> : std::integral_constant<size_t,0> {};


// ttmath here works well at run time !
ttmath::UInt<SIZE> fib(size_t n)
{
    ttmath::UInt<SIZE> a,b,c;
    a = 1, b = 1;
    for (size_t i = 3; i <= n; i++) {
        ttmath::UInt<SIZE> c = a + b;
        a = b;
        b = c;
    }           
    return b;
}

int main() {
    const size_t N = 500;
    if(1)
    {
    clock_t start = clock();
    std::cout <<  "Fibonacci(" << N << ") = " << fib(N) << std::endl;
    std::cout << "Time : " << (double)(clock() - start)/CLOCKS_PER_SEC << " s" << std::endl;
    }
    if(1)
    {
    clock_t start = clock();
    std::cout <<  "Fibonacci(" << N << ") = " << fibonacci<N>() << std::endl;
    std::cout << "Time : " << (double)(clock() - start)/CLOCKS_PER_SEC << " s" << std::endl;
    }
}

结果是:

斐波那契(500) = 139423224561697880139724382870407283950070256587697307264108962948325571622863290691557658876222521294125
时间:0.003006 秒
斐波那契 (500) = 2171430676560690477
时间:1.5e-05 s

那么是否可以轻松地为元斐波那契提供 ttmath ?还是我应该做不同的事情?

【问题讨论】:

  • 因为 ttmath 可以处理大于 size_t 的数字,但 fibbonachi 会尝试读取 size_t,这在您的系统上可能是 64 位,而您无法将 105 位数字放入 64 位
  • 是的,我知道为什么。我想知道如何解决这个问题。

标签: c++ metaprogramming fibonacci


【解决方案1】:

如果您查看 ttmath 源,则有一个 UInt&lt;N&gt;::Add 的定义,它遍历 uint 数组 table,表示 UInt&lt;N&gt; 值,添加每个元素对并将溢出带到下一次迭代。基于此迭代,我们可以定义一个递归模板实现,如下所示:

#include <array> 
#include <ttmathuint.h> 

typedef unsigned int uint; 
namespace ttmath { 

    uint AddTwoUInt(uint a, uint b, uint carry, uint * result) 
    { 
        uint temp; 

        if( carry == 0 ) { 
            temp = a + b; 

            if( temp < a ) 
                carry = 1; 
        } else  { 
            carry = 1; 
            temp  = a + b + carry; 

            if( temp > a ) // !(temp<=a) 
                carry = 0; 
        } 

        *result = temp; 

        return carry; 
    } 

    template<uint N> 
    uint Add(const uint * t0, const uint * t1, uint * t2, uint c); 

    template<> 
    uint Add<1>(const uint * t0, const uint * t1, uint * t2, uint c)  { 
        uint i; 
        c = AddTwoUInt(*t0, *t1, c, t2); 
        return c; 
    } 

    template<uint N> 
    uint Add(const uint * t0, const uint * t1 , uint * t2, uint c) { 
        c = Add<N-1>(t0, t1, t2, c); 
        c = AddTwoUInt(t0[N-1], t1[N-1], c, t2+N-1); 
        return c; 
    } 

} 
template<int N> 
ttmath::UInt<N> fib(size_t n) 
{ 
 ttmath::UInt<N> a,b,c; 
 a = 1, b = 1; 

 for (size_t i = 3; i <= n; i++) { 
    ttmath::Add<N>(a.table,b.table,c.table,0); 
    a = b; 
    b = c; 
 }            
 return b;                           
} 

int main(int argc,char ** argv) { 
 std::cerr << fib<15>(500) << std::endl;
} 

斐波那契只需添加即可

【讨论】:

  • 问题是我需要在当前设计的模板参数中传递一个 ttmath::Uint 。我想它可以通过不同的方法使用 constexpr 以某种方式实现,并调用具有非常大的返回值的斐波那契函数。
  • 您只需将您的原始文件中的a + b 替换为ttmath::Add&lt;N&gt;(a.table,b.table,c.table,0)。代码请参阅我的答案中的补充
  • 谢谢,但我看不到您的解决方案在编译时是如何完成的?您刚刚复制了我的 fib 函数并对其进行了修改。这个功能很好用~~ wtf
  • 在运行时在 ttmath 库中完成的对 UInt&lt;N&gt;::table[...] 的迭代已被我的方法中的递归模板扩展所取代。如果您还打算用模板 arg 将参数 n 替换为 fib(n) ,那么根本就没有什么是可变的,无论如何整个事情都是 constexpr 。它也可以递归地完成,但绝对不值得进行模糊处理(除了一个很好的家庭作业或脑力锻炼)。
  • error: ‘Add’ is not a member of ‘ttmath’ 不幸的是,它似乎在最后一个 ttmath 版本中不可用。我仍然不明白为什么您的解决方案是在编译时完成的?问题不在于fib 功能正常,而是在元fibonacci 上!
猜你喜欢
  • 2010-09-22
  • 1970-01-01
  • 1970-01-01
  • 2018-01-18
  • 2019-12-19
  • 2016-11-28
  • 2015-06-05
  • 2014-05-23
  • 2014-05-03
相关资源
最近更新 更多