hi.
my code below makes use of NTL(a library for doin number theory available at www.shoup.net); im trying to print the double base number representation of any large number(32 bits and/or more) but my program gives me a segmentation fault. i was unable to find out where though. could some please tell me where the fault is and how to correct it?
Thanks!
my code below makes use of NTL(a library for doin number theory available at www.shoup.net); im trying to print the double base number representation of any large number(32 bits and/or more) but my program gives me a segmentation fault. i was unable to find out where though. could some please tell me where the fault is and how to correct it?
Thanks!
Code:
#include <NTL/ZZ.h>
#include <NTL/vec_ZZ.h>
#include <cstdlib>
NTL_CLIENT
#define SIZE_OF_INTEGER 10
#define BASE_1 2
#define BASE_2 3
#define NUM 10
struct entry_table {
ZZ num;
long m,n;
};
ZZ bs ( entry_table a[], long beg, long end, ZZ nbr )
{
long mid;
mid = ( beg + end ) / 2;
if ( nbr == a[mid].num )
{
cout << " 2{" << a[mid].m << "}*3{" << a[mid].n <<"} + ";
return a[mid].num;
}
else if ( beg > end )
{
cout <<" 2{" << a[mid].m << "}*3{" << a[mid].n << "} + ";
return a[end].num;
}
else if ( nbr < a[mid].num )
{
end = mid - 1;
return bs ( a, beg, end, nbr );
}
else if ( nbr > a[mid].num )
{
beg = mid + 1;
return bs ( a, beg, end, nbr );
}
}
ZZ alg ( entry_table a[], long x, ZZ numbr, ZZ count )
{
ZZ num1;
num1 = numbr - bs ( a, 0, x, numbr );
if ( num1 == 0 )
return count;
else
return alg ( a, x, num1, ++count );
}
entry_table quick ( entry_table a[], long begin, long end )
{
long num = end - begin + 1;
if ( num > 2 )
{
long left = begin + 1;
long right = end;
while ( left < right )
{
while ( left <= end && a[left].num <= a[begin].num )
left++;
while ( a[right].num > a[begin].num )
right--;
if ( left < right )
swap ( a[left], a[right] );
}
swap ( a[begin], a[right] );
quick ( a, begin, right - 1 );
quick ( a, right + 1, end );
}
else if ( num == 2 )
{
if ( a[end].num < a[begin].num )
swap( a[end], a[begin] );
}
}
entry_table quick ( entry_table a[], long x )
{
quick ( a, 0, x - 1 );
}
main()
{
cout<<"gdfhc";//this line isnt even printed so thats why i dont know where and why there is a segfault
srand( time(NULL) );
SetSeed( to_ZZ(rand()) );
ZZ d;
vec_ZZ b,c;
b.SetLength(NUM);
c.SetLength(NUM);
long size = SIZE_OF_INTEGER * SIZE_OF_INTEGER;
struct entry_table a[ size ];
ZZ v;
v=power_ZZ ( 2,SIZE_OF_INTEGER );
long x=0;
for ( long i = 0; i <= SIZE_OF_INTEGER; i++ )
{
for ( long j = 0; ( d = power_ZZ(BASE_1,i)*power_ZZ(BASE_2,j) ) <= v ; j++ )
{
a[x].num = d;
a[x].m = i;
a[x].n = j;
x++;
}
}
quick ( a, x );
ZZ cnt;
cnt = 0;
for ( long k = 0; k < NUM; k++ )
{
c[k] = 1;
b[k] = RandomBnd(v);
cout << endl << b[k] << " : ";
c[k] = alg ( a, x, b[k], c[k] );
cnt = cnt + c[k];
cout << endl;
}
cout << endl << "total no. of terms: " << cnt << endl;
cout << "avg.= " << cnt/NUM << endl;
}
Comment