编译处理大量数字的时间斐波那契
Compile time fibonacci which handle large numbers
我正在玩这个 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;
}
}
结果是:
Fibonacci(500) =
139423224561697880139724382870407283950070256587697307264108962948325571622863290691557658876222521294125
Time : 0.003006 s
Fibonacci(500) = 2171430676560690477
Time : 1.5e-05 s
那么是否可以轻松地为元斐波那契提供 ttmath?或者我应该以不同的方式做事吗?
如果您查看 ttmath 源,有一个 UInt<N>::Add
的定义,它遍历代表 UInt<N>
值的 uint 数组 table
添加每个元素对并将溢出传送到下一次迭代。基于这一迭代,可以定义一个递归模板化实现,如下所示:
#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;
}
加就是斐波那契
我正在玩这个 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;
}
}
结果是:
Fibonacci(500) = 139423224561697880139724382870407283950070256587697307264108962948325571622863290691557658876222521294125
Time : 0.003006 s
Fibonacci(500) = 2171430676560690477
Time : 1.5e-05 s
那么是否可以轻松地为元斐波那契提供 ttmath?或者我应该以不同的方式做事吗?
如果您查看 ttmath 源,有一个 UInt<N>::Add
的定义,它遍历代表 UInt<N>
值的 uint 数组 table
添加每个元素对并将溢出传送到下一次迭代。基于这一迭代,可以定义一个递归模板化实现,如下所示:
#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;
}
加就是斐波那契