Velocity Reviews > C++ > large integers

# large integers

Tim S Roberts
Guest
Posts: n/a

 10-12-2006
If I have a value, say 13849, I can assign it to x and test if it divisible
by 37 by something like
if (x%37==0)...
and I can test if it's a square by something like
int y;
y=sqrt(x);
if (x==y*y)...

But my question is, how can I do these two things (preferably as simply and
efficiently as possible) if my value is not 13849, but, say, 176457628349?

Tim

Victor Bazarov
Guest
Posts: n/a

 10-12-2006
Tim S Roberts wrote:
> If I have a value, say 13849, I can assign it to x and test if it
> divisible by 37 by something like
> if (x%37==0)...
> and I can test if it's a square by something like
> int y;
> y=sqrt(x);
> if (x==y*y)...
>
> But my question is, how can I do these two things (preferably as
> simply and efficiently as possible) if my value is not 13849, but,
> say, 176457628349?

If your compiler supports numbers with the range that includes your
176457628349, use that type. You would have to ask in the newsgroup
that?)

--

Tim S Roberts
Guest
Posts: n/a

 10-12-2006

"Victor Bazarov" <(E-Mail Removed)> wrote in message
news:eglkaj\$6fb\$(E-Mail Removed)...
> Tim S Roberts wrote:
>> If I have a value, say 13849, I can assign it to x and test if it
>> divisible by 37 by something like
>> if (x%37==0)...
>> and I can test if it's a square by something like
>> int y;
>> y=sqrt(x);
>> if (x==y*y)...
>>
>> But my question is, how can I do these two things (preferably as
>> simply and efficiently as possible) if my value is not 13849, but,
>> say, 176457628349?

>
> If your compiler supports numbers with the range that includes your
> 176457628349, use that type. You would have to ask in the newsgroup
> that?)

Thanks for this - but I don't think I have such a type available (I'm using
Microsoft Visual C++ 6.0). Are there any workarounds I can use? Or will I
have to use Mathematica, or something (the problem is, I'm using very large
numbers, *and* I need speed....)

Tim

Martin Steen
Guest
Posts: n/a

 10-12-2006
Tim S Roberts wrote:
> If I have a value, say 13849, I can assign it to x and test if it divisible
> by 37 by something like
> if (x%37==0)...
> and I can test if it's a square by something like
> int y;
> y=sqrt(x);
> if (x==y*y)...
>
> But my question is, how can I do these two things (preferably as simply and
> efficiently as possible) if my value is not 13849, but, say, 176457628349?
>
> Tim
>

Some compilers (e.g. g++) support the __int64 datatype.

__int64 BigIntVar = (__int64) 176457628349LL;
cout << (BigIntVar % 37) << endl;

- Martin

Puppet_Sock
Guest
Posts: n/a

 10-12-2006
Tim S Roberts wrote:
> If I have a value, say 13849, I can assign it to x and test if it divisible
> by 37 by something like
> if (x%37==0)...
> and I can test if it's a square by something like
> int y;
> y=sqrt(x);
> if (x==y*y)...
>
> But my question is, how can I do these two things (preferably as simply and
> efficiently as possible) if my value is not 13849, but, say, 176457628349?

There are two questions here:

The first is, how to efficiently do things like factoring, checking
if one integer factors another, checking for perfect square, etc.
For this you probably want to visit some math groups, or possibly
a group that has the word numerical or the word algorithm or
such in its name. Or possibly do some searches at

The other question is, how to represent very large integers in C++.
There are various libraries that allow extended precision, or precision
of arbitary degree. You need to be prepared for the fact that they
are going to get slower the longer the integer. Again, google is your
friend here. Search for the FAQ in this news group, and from the
FAQ look for libraries available. Also google for C++ arbitrary
precision libraries.
Socks

Victor Bazarov
Guest
Posts: n/a

 10-12-2006
Tim S Roberts wrote:
> "Victor Bazarov" <(E-Mail Removed)> wrote in message
> news:eglkaj\$6fb\$(E-Mail Removed)...
>> Tim S Roberts wrote:
>>> If I have a value, say 13849, I can assign it to x and test if it
>>> divisible by 37 by something like
>>> if (x%37==0)...
>>> and I can test if it's a square by something like
>>> int y;
>>> y=sqrt(x);
>>> if (x==y*y)...
>>>
>>> But my question is, how can I do these two things (preferably as
>>> simply and efficiently as possible) if my value is not 13849, but,
>>> say, 176457628349?

>>
>> If your compiler supports numbers with the range that includes your
>> 176457628349, use that type. You would have to ask in the newsgroup
>> write that?)

>
> Thanks for this - but I don't think I have such a type available (I'm
> using Microsoft Visual C++ 6.0).

Asking in 'microsoft.public.vc.language' might help. Hint: __int64.

> Are there any workarounds I can
> use? Or will I have to use Mathematica, or something (the problem
> is, I'm using very large numbers, *and* I need speed....)

Try looking on the Web for "arbitrary precision library" or "very large
integer library" or some such.

V
--

Clark S. Cox III
Guest
Posts: n/a

 10-12-2006
Martin Steen wrote:
> Tim S Roberts wrote:
>> If I have a value, say 13849, I can assign it to x and test if it
>> divisible by 37 by something like
>> if (x%37==0)...
>> and I can test if it's a square by something like
>> int y;
>> y=sqrt(x);
>> if (x==y*y)...
>>
>> But my question is, how can I do these two things (preferably as
>> simply and efficiently as possible) if my value is not 13849, but,
>> say, 176457628349?
>>
>> Tim
>>

>
> Some compilers (e.g. g++) support the __int64 datatype.
>
> __int64 BigIntVar = (__int64) 176457628349LL;
> cout << (BigIntVar % 37) << endl;
>
> - Martin
>

You sure about g++ supporting __int64?

[ccox-macbook:~] ccox% cat test.cpp
#include <iostream>
using namespace std;

int main()
{
__int64 BigIntVar = (__int64) 176457628349LL;
cout << (BigIntVar % 37) << endl;
return 0;
}

[ccox-macbook:~] ccox% g++ test.cpp
test.cpp: In function ‘int main()’:
test.cpp:6: error: ‘__int64’ was not declared in this scope
test.cpp:6: error: expected `;' before ‘BigIntVar’
test.cpp:7: error: ‘BigIntVar’ was not declared in this scope

--
Clark S. Cox III
http://www.velocityreviews.com/forums/(E-Mail Removed)

jjds101@yahoo.com
Guest
Posts: n/a

 10-12-2006

Clark S. Cox III wrote:
> Martin Steen wrote:
> > Tim S Roberts wrote:
> >> If I have a value, say 13849, I can assign it to x and test if it
> >> divisible by 37 by something like
> >> if (x%37==0)...
> >> and I can test if it's a square by something like
> >> int y;
> >> y=sqrt(x);
> >> if (x==y*y)...
> >>
> >> But my question is, how can I do these two things (preferably as
> >> simply and efficiently as possible) if my value is not 13849, but,
> >> say, 176457628349?
> >>
> >> Tim
> >>

> >
> > Some compilers (e.g. g++) support the __int64 datatype.
> >
> > __int64 BigIntVar = (__int64) 176457628349LL;
> > cout << (BigIntVar % 37) << endl;
> >
> > - Martin
> >

>
> You sure about g++ supporting __int64?
>
> [ccox-macbook:~] ccox% cat test.cpp
> #include <iostream>
> using namespace std;
>
> int main()
> {
> __int64 BigIntVar = (__int64) 176457628349LL;
> cout << (BigIntVar % 37) << endl;
> return 0;
> }
>
> [ccox-macbook:~] ccox% g++ test.cpp
> test.cpp: In function 'int main()':
> test.cpp:6: error: '__int64' was not declared in this scope
> test.cpp:6: error: expected `;' before 'BigIntVar'
> test.cpp:7: error: 'BigIntVar' was not declared in this scope
>

I got "long long" to work with g++ on solaris.

#include <iostream>
#include <limits>

int main () {
unsigned long long x = 55555555555555LL;
std::cout << "long long max: "
<< std::numeric_limits<long long>::max() << '\n'
<< "size of long long: "
<< sizeof(x) << '\n';

return 0;

}

long long max: 9223372036854775807
size of long long: 8

Note that you need the LL at the end of the constant or it doesn't work.

jjds101@yahoo.com
Guest
Posts: n/a

 10-12-2006
>
> I got "long long" to work with g++ on solaris.
>
> #include <iostream>
> #include <limits>
>
> int main () {
> unsigned long long x = 55555555555555LL;
> std::cout << "long long max: "
> << std::numeric_limits<long long>::max() << '\n'
> << "size of long long: "
> << sizeof(x) << '\n';
>
> return 0;
>
> }
>
> long long max: 9223372036854775807
> size of long long: 8
>
> Note that you need the LL at the end of the constant or it doesn't work.

Oops --- sorry, shouldn't have had the "unsigned" there!

Martin Steen
Guest
Posts: n/a

 10-12-2006
Clark S. Cox III wrote:

>
> You sure about g++ supporting __int64?
>
> [ccox-macbook:~] ccox% cat test.cpp
> #include <iostream>
> using namespace std;
>
> int main()
> {
> __int64 BigIntVar = (__int64) 176457628349LL;
> cout << (BigIntVar % 37) << endl;
> return 0;
> }
>
> [ccox-macbook:~] ccox% g++ test.cpp
> test.cpp: In function ‘int main()’:
> test.cpp:6: error: ‘__int64’ was not declared in this scope
> test.cpp:6: error: expected `;' before ‘BigIntVar’
> test.cpp:7: error: ‘BigIntVar’ was not declared in this scope
>

Ok, you're right. I am using the mingw-version
of g++, which is different.

If you replace "__int64" by "long long", the
code should work (it works on debian linux with
a "normal" g++ compiler).

- Martin