10 million digits

[b][red]This message was edited by DarQ at 2004-2-4 11:20:34[/red][/b][hr]Hi

i just started a week ago coding C, and i love it already! especially the fact that what i code in winNT, can be compiled on my linux box without a problem But i have a question now to which i haven't found an answer yet.

today i made a thing that finds prime numbers. As the numbers grow larger, the proccess takes longer, but there is a trick for that The problem now is, that i want to exceed the storage capacity of unsigned longs.. use numbers of about 10 MILLION digits.

now, 10 million might be a bit too much for now, but an unsigned long has certainly not enough digits.

Comments

The solution is NOT easy. The primitive data types are there to be used, and they have sizes that are supposed to be "reasonable". But sometimes you do have to go over the limit. A solution would be to create a struct that contains two unsigned longs in it. Then you modify your algorithm that finds prime numbers to work with this struct. Everything needs to be modified because no c runtime functions will be able to just work with your new data type.

[code]struct LARGE_NUM{ unsigned long num1; unsigned long num2;};

[/code]

num1 could be the primary storage area, and num2 is where the overflow bytes (the digits that don't fit into num1) go. This kind of stuff requires some knowledge of how the computer stores these data types. This can be platform specific...

: [b][red]This message was edited by DarQ at 2004-2-4 11:20:34[/red][/b][hr]: Hi: : i just started a week ago coding C, and i love it already! especially the fact that what i code in winNT, can be compiled on my linux box without a problem But i have a question now to which i haven't found an answer yet.: : today i made a thing that finds prime numbers. As the numbers grow larger, the proccess takes longer, but there is a trick for that : The problem now is, that i want to exceed the storage capacity of unsigned longs.. use numbers of about 10 MILLION digits.: : now, 10 million might be a bit too much for now, but an unsigned long has certainly not enough digits.: : can anyone kick my in the right direction?: : greets!: [size=5][italic][blue]Dar[RED]Q[/RED][/blue][/italic][/size]: NEW url--> http://mark.space.servehttp.com: : :

[green] 10 million digits in what? Decimal, hexidecimal, octal, binary?If decimal or any other numbering system said above is used then it's going to require [b][red]ALOT[/red][/b] of memory. Probably 10 to the what ever power(I'm not even going to figure it out) decimal converted into a binary equivalent of the greatest value you will ever need. On top of that, your variable usage would be astounding to even type by hand, so that would have to be automated also since the most a variable that I know of can hold is 80bit numbers on a PC. That's probably why those super puter's that analyze astronomical equations are so expensive because you need it to handle big numbers and be fast at the same time.

[red]: btw: i've seen something about unsigned long long's. how big are those?[/red]

64bit

Like I said before the biggest variable type I know of is 80bit or a tbyte also a real 10 which is of course a 10byte number, but I don't remember how it would be declared in C since I have never messed with numbers that big in c or anything else in that matter.

: : [b][red]This message was edited by DarQ at 2004-2-4 11:20:34[/red][/b][hr]: : Hi: : : : i just started a week ago coding C, and i love it already! especially the fact that what i code in winNT, can be compiled on my linux box without a problem But i have a question now to which i haven't found an answer yet.: : : : today i made a thing that finds prime numbers. As the numbers grow larger, the proccess takes longer, but there is a trick for that : : The problem now is, that i want to exceed the storage capacity of unsigned longs.. use numbers of about 10 MILLION digits.: : : : now, 10 million might be a bit too much for now, but an unsigned long has certainly not enough digits.: : : : can anyone kick my in the right direction?: : : : greets!: : [size=5][italic][blue]Dar[RED]Q[/RED][/blue][/italic][/size]: : NEW url--> http://mark.space.servehttp.com: : : : : : : : [green] 10 million digits in what? Decimal, hexidecimal, octal, binary?: If decimal or any other numbering system said above is used then it's going to require [b][red]ALOT[/red][/b] of memory. Probably 10 to the what ever power(I'm not even going to figure it out) decimal converted into a binary equivalent of the greatest value you will ever need. On top of that, your variable usage would be astounding to even type by hand, so that would have to be automated also since the most a variable that I know of can hold is 80bit numbers on a PC. That's probably why those super puter's that analyze astronomical equations are so expensive because you need it to handle big numbers and be fast at the same time.: :

===========================================================

hey...why dont u try changing a file called "limits.h" , which has the min & max values of almost all the things ( with values ) , and i have seen datatypes like "long long".

: : hey...why dont u try changing a file called "limits.h" , which has the min & max values of almost all the things ( with values ) , and i have seen datatypes like "long long".: :

[blue]Yes, Windows OS has a "long long" data type, which is a 64-bit number. It's twice the size of a long, but doesn't even come close to holding 10,000,000 digits. There is simply no way to do that on a PC using native data types and current archetecture. Maybe, just maybe, if you go to NASA they can help you.[/blue]

im aware that no datatype can hold plenty digits. but if you can do 16 bits divisions on an 8 bits cpu (asm), then with C it should work as well, i hope

i will try to figure it out by using quite a few longs.: : [blue]Yes, Windows OS has a "long long" data type, which is a 64-bit number. It's twice the size of a long, but doesn't even come close to holding 10,000,000 digits. There is simply no way to do that on a PC using native data types and current archetecture. Maybe, just maybe, if you go to NASA they can help you.[/blue]:

: [red]: : btw: i've seen something about unsigned long long's. how big are those?: [/red]: : 64bit: : Like I said before the biggest variable type I know of is 80bit or a tbyte also a real 10 which is of course a 10byte number, but I don't remember how it would be declared in C since I have never messed with numbers that big in c or anything else in that matter.:

[green]While using a Borland compiler the correct type for an 80bit variable is a long double. 80bits is quite a large variable, its assigned value can be anywhere from 3.4 x 10-4932 to 1.1 x 10+4932.[/green][hr][blue][size=4]JBaker[/size][/blue]

: : [red]: : : btw: i've seen something about unsigned long long's. how big are those?: : [/red]: : : : 64bit: : : : Like I said before the biggest variable type I know of is 80bit or a tbyte also a real 10 which is of course a 10byte number, but I don't remember how it would be declared in C since I have never messed with numbers that big in c or anything else in that matter.: : : : [green]: While using a Borland compiler the correct type for an 80bit variable is a long double. 80bits is quite a large variable, its assigned value can be anywhere from 3.4 x 10-4932 to 1.1 x 10+4932.: [/green]: [hr]: [blue][size=4]JBaker[/size][/blue]: : [blue]So, by that logic, say, value of 0.728672933993887857487376E+4930 will have all the 4930 digits as a significant ones? I doubt that. Mantissa is still limited to some 71 bit or so... and it is not even close to 1000 digits even...[/blue]

An avenue that I use when playing with large numbers is to work the digits into strings. Unfortunately, this entails writing your own math routines for the strings, and there is a great loss in memory usage, but the string length becomes the limit of the number of digits. A simple example would be to take the number 12345678901234567891123456789212345678931234567894 and multiply it by 3. This would give the math routines a bit of a challenge but all you really need to do is assign the above number to a string and then write your own routine which does the math based on each digit. For the above your routine will extract the first digit on the right, convert it to its numeric value (character 4 - 48 becomes numeral 4) and then multiply it by the 3 using normal math procedures. Capture any carry and place the converted "ones" result into the first place on the right of a second string (remember to add 48 to the new numeral before placing it in your second string). Work your way left one character at a time and you get the same result as if you were able to do direct math. Granted there is an awful waste in memory and conversion, but it is a solution.