Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > large integers

Reply
Thread Tools

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


 
Reply With Quote
 
 
 
 
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
for your compiler about it, or read the manual (oops, did I just write
that?)

--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
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
> for your compiler about it, or read the manual (oops, did I just write
> 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


 
Reply With Quote
 
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

 
Reply With Quote
 
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
groups.google.com.

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

 
Reply With Quote
 
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
>> for your compiler about it, or read the manual (oops, did I just
>> 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
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)
 
Reply With Quote
 
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.

 
Reply With Quote
 
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!

 
Reply With Quote
 
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




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Handling large text streams of integers Comp1597@yahoo.co.uk C++ 7 04-01-2009 01:51 PM
very large integers bob@coolgroups.com C++ 13 10-09-2006 05:29 AM
ANN: DecInt 0.3 - Arithmetic for very large decimal integers casevh@comcast.net Python 0 11-30-2005 07:51 AM
Datatype's to store integers as large as 100 digits psanand@gmail.com Java 2 01-20-2005 12:31 PM
Large integers Michael Zielinski C++ 6 12-20-2004 09:19 AM



Advertisments