LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-17-2006, 12:26 PM   #1
cdog
Member
 
Registered: Dec 2005
Posts: 65

Rep: Reputation: 15
Ideas to represent an integer of unlimited size


I have a new assignment and that is to write a C program that knows how to handle very long integers (no limits).
I'm looking for a better and more efficient than representing it as a string or linked list. Thanks.
 
Old 05-17-2006, 12:54 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
There's a GNU library that does this very well. It's called "libgmp". (I'm not going to do your homework, sorry)
 
Old 05-17-2006, 04:26 PM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
BTW, why not a linked list?
If you really need no limits integers, linked lists are the best (only?) way. At least in terms of effort/efficiency ratio.
 
Old 05-17-2006, 11:06 PM   #4
remissed
LQ Newbie
 
Registered: May 2006
Posts: 6

Rep: Reputation: 0
With a C-string, you trade space for complexity and computation, while a linked list is a lot easier to work with, but there is (slightly) more overhead (pointers for every node in the least).

There is no "silver bullet" though, at least not that I can think of. Actually maybe there is some sort of binary tree you could make that splits/creates children when something is overflowed, and then to display the number you can walk all of the nodes in ..some order?.. and just append all of the contents. That would change the performance from linear to logarithmic.
 
Old 05-17-2006, 11:32 PM   #5
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
I think that vectors would probably also be a good implementation.
 
Old 05-18-2006, 04:12 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by spooon
I think that vectors would probably also be a good implementation.
Yes, but a vector is nothing more than an object-oriented implementation of a linked list.

Moreover, since vectors are an object-oriented concept, they are not readily available in C. Only in C++.
 
Old 05-18-2006, 07:35 AM   #7
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by Hko
Yes, but a vector is nothing more than an object-oriented implementation of a linked list.
I'm not certain that is true. I thought that a vector was a malloc'd (or new in C++) array (usually with some spare elements), which when it ran out of space was realloc'd (again allocating some spare elements). Meaning that random access is possible but time to input elements is variable depending upon whether it extra elements need to be reallocated, and the whole array copied from one version to the next.

This could be an approach to use.
 
Old 05-18-2006, 08:34 AM   #8
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by graemef
I'm not certain that is true. I thought that a vector was a malloc'd (or new in C++) array (usually with some spare elements), which when it ran out of space was realloc'd (again allocating some spare elements). Meaning that random access is possible but time to input elements is variable depending upon whether it extra elements need to be reallocated, and the whole array copied from one version to the next.
Hmm, you are probably right. Unlike linked lists nodes, vector elements can also be accessed by index IIRC (true?). This may be useful for implementing no-limits-int's. But IMHO features like allocating memory in advance is not a valid argument, but just an efficient implementation decision, which can also be done for linked lists.

Quote:
Originally Posted by graemef
This could be an approach to use.
Well, yes and no. Yes: because it could prove handy for implementing no-limit-integers. No: because the OP asked for a C solution, not C++...
 
Old 05-18-2006, 09:30 AM   #9
cdog
Member
 
Registered: Dec 2005
Posts: 65

Original Poster
Rep: Reputation: 15
OK guys so it seems that I'm stucked with the linked list. How about every link contains more than 1 digit (of the number). I think this is more efficient (time and mem space) but more difficult to implement the 4 operations. What do you think? Thank you.
 
Old 05-18-2006, 09:35 AM   #10
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
First you need to figure out how to calculate things such as * and / (or just log, then form the others in terms of log and +/-.) If you can figure out how to do this with unlimited bytes, the representation then becomes a matter of trial and error.
ta0kira
 
Old 05-18-2006, 09:41 AM   #11
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
If you are using a linked list then in your root node include the sign and number of digits (which will roughly equate to nodes)

First look at operations to assign a value in and to display your number:
Next look at the comparison operators: > >= == < <=
Then tackle addition, and subtraction - relatively easy
Then multiplication, again fairly easy
Finally div and mod, which can be built using repeated addition and then subtraction.

Then all you need to do is test, document and relax

Last edited by graemef; 05-18-2006 at 09:45 AM.
 
Old 05-18-2006, 09:45 AM   #12
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by Hko
No: because the OP asked for a C solution, not C++...
But a vector can be implemented in C, just as a linked list can be implemented. I was just outlining how it may be implemented in a language neutral way
 
Old 05-18-2006, 09:58 AM   #13
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,671
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
Oh, that's babbling. The essential idea is that you must use a decimal representation and program the computer to do the math like they (used to) teach you in second and third grade.
 
Old 05-18-2006, 03:14 PM   #14
primo
Member
 
Registered: Jun 2005
Posts: 542

Rep: Reputation: 34
I would extend the binary size for these 2 reasons:

1- Best space use.
2- You may take advantage of the binary arithmetic of the CPU. Then transforming it from/to decimal would take less time than manual decimal math.

PS: I believe that a single realloc() on a linear buffer is better than linked lists or binary trees. I would use a flag to indicate whether a number is negative to know beforehand if there would be an "overflow" in addition
Code:
struct number {
  uint32_t *words;
  size_t size;
  int negative;
}
Also, you may store each word as big-endian to quickly implement the shift operations.

Last edited by primo; 05-18-2006 at 03:21 PM.
 
Old 05-18-2006, 03:28 PM   #15
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
Probably not applicable since this is an assignment, but the openssl library (installed on most linux boxes) has a bignum package which handles all the operations you could want on arbitrary length integers. type "man 3ssl bn" at your terminal to read about it.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to represent int in binary system in Java? mac1234mac Programming 4 03-14-2006 07:11 AM
how to set /tmp to unlimited size bcos it's blocked to 256MB msalimane Mandriva 4 01-30-2005 03:39 AM
* should represent any characters suchi_s Programming 6 07-31-2004 03:50 PM
what does this c represent shanenin Linux - Software 3 10-14-2003 01:45 PM
Come and get unlimited Wallpapers futurist General 8 04-03-2003 01:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration