# large integers

Discussion in 'C++' started by Tim S Roberts, Oct 12, 2006.

1. ### Tim S RobertsGuest

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

Tim S Roberts, Oct 12, 2006

2. ### Victor BazarovGuest

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?)

--

Victor Bazarov, Oct 12, 2006

3. ### Tim S RobertsGuest

"Victor Bazarov" <> wrote in message
news:eglkaj\$6fb\$...
> 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

Tim S Roberts, Oct 12, 2006
4. ### Martin SteenGuest

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

Martin Steen, Oct 12, 2006
5. ### Puppet_SockGuest

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

Puppet_Sock, Oct 12, 2006
6. ### Victor BazarovGuest

Tim S Roberts wrote:
> "Victor Bazarov" <> wrote in message
> news:eglkaj\$6fb\$...
>> 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
--

Victor Bazarov, Oct 12, 2006
7. ### Clark S. Cox IIIGuest

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

Clark S. Cox III, Oct 12, 2006
8. ### Guest

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.

, Oct 12, 2006
9. ### Guest

>
> 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!

, Oct 12, 2006
10. ### Martin SteenGuest

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

Martin Steen, Oct 12, 2006
11. ### Martin SteenGuest

Clark S. Cox III wrote:

> You sure about g++ supporting __int64?
>

I tested the code on a "mingw"-version of g++.
g++ --version: g++.exe (GCC) 3.4.2 (mingw-special)

Other versions of g++ seem to use "long long" instead
(testet on debian linux).

- Martin

Martin Steen, Oct 12, 2006
12. ### Martin SteenGuest

Clark S. Cox III wrote:

> You sure about g++ supporting __int64?
>

I tested the code on a "mingw"-version of g++.
g++ --version: g++.exe (GCC) 3.4.2 (mingw-special)

Other versions of g++ seem to use "long long" instead
(testet on debian linux).

- Martin

Martin Steen, Oct 12, 2006