Good day!
The question arose: as a very large decimal number (the number of decimal signs ~ 10 ^ 4) is quickly translated into a binary number system?
P.S. Since no type of such a number will be valid, the number is read in the string, well, or on the summer you need to process
P.P.S Third Party Luba Use Cannot
Answer 1, Authority 100%
In general, I decided so:
From the second volume d.knuta algorithm (thanks @vladd for the fact that I specified where to look for):
For a number having M + 1 & gt; 1 discharges:
ENT1 M-1 J ← M-1
LDA INPUT + M U ← U_M
2H MUL = 10 =
SLAX 5.
Add input, 1 U ← 10u + u_j
Dec1 1.
J1nn 2b repeat with m & gt; j ≥ 0.
and my code on C:
# include & lt; stdlib.h & gt;
#Include & lt; stdio.h & gt;
#Include & lt; String.h & gt;
Using Namespace STD;
INT MAIN () {
// Processing the case when the number of only 1 decimal
char first = getc (stdin), second = getc (stdin);
Ungetc (Second, Stdin); Ungetc (first, stdin);
if (second == '\ n') ungetc ('0', stdin);
// Select the memory "with the reserve" under the worst case - 10 ^ 10,000 decimal signs ~ = 32kb
BOOL * ARR = (BOOL *) Malloc (Sizeof (BOOL) * 34000);
BOOL * ARR1;
int ED = 0;
for (int i = 0; i & lt; 34000; i ++, arr [i] = false);
int a = (int) getc (STDIN) -48;
// We write down the oldest discharge of the number "How at school was taught" by division at 2
do {
ARR [END] = (BOOL) (A & AMP; 1);
a & gt; & gt; = 1;
End ++;
} While (a);
a = (int) getc (STDIN) -48;
// Processing the rest of the discharge
While (A! = - 38) {
// We implement multiplying the current accumulated value to 10:
// x * 10 = x * 8 + x * 2 == x & lt; & lt; 3 + x & lt; & lt; 1
arr1 = (bool *) Malloc (Sizeof (BOOL) * (END + 4));
arr1 [0] = false;
// write the number shifted by 1
Memcpy (Arr1 + 1, Arr, End + 3);
// We shift the number at 3
Memmove (Arr + 3, Arr, End);
arr [0] = false; arr [1] = false; Arr [2] = false;
END + = 3;
// summarize the above numbers
Bool NextOverhead, overhead = false;
For (int i = 1; i & lt; = end; i ++) {
NEXTOVERHEAD = ((ARR [I] & amp; & amp; arr1 [i]) || (Arr [i] & amp; overhead) || (arr1 [i] & amp; & amp; overhead));
arr [i] ^ = arr1 [i] ^ = overhead;
Overhead = NextOverhead;
}
if (ArR [END]) END ++;
// add the next discharge
Overhead = false;
For (int i = 0; A || NextOverhead; I ++) {
NextOverhead = ((Arr [i] & amp; & amp; ((bool) (a & amp; 1))) ||
(ARR [I] & amp; & amp; overhead) ||
(((BOOL) (A & AMP; 1)) & amp; & amp; overhead));
Arr [i] ^ = overhead ^ = ((Bool) (A & amp; 1));
Overhead = NextOverhead;
a & gt; & gt; = 1;
}
if (ArR [END]) END ++;
a = (int) getc (STDIN) -48;
FREE (ARR1);
}
for (int i = end-1; i & gt; = 0; i--)
PrintF ("% d", arr [i]);
Return 0;
}
Answer 2
@miramentis , catch.
# include & lt; stdio.h & gt;
#Include & lt; String.h & gt;
#Include & lt; stdlib.h & gt;
#Include & lt; ctype.h & gt;
int.
GetDecBits (Char Arr [])
{
int ED = -1, A;
if (isdigit (a = getchar ())) {
END = 0;
a - = '0';
do {
ARR [END ++] = A & AMP; 1;
} While (A & GT; & GT; = 1);
}
RETURN END;
}
// calculation y = x * 10 + d as X & GT; & gt; 3 + x & gt; & gt; 1 + D.
// folds x3 [], x1 [] and d []
// (binary, one bit in the pate, eldest on the right)
// (in x3 [] and x1 [] d.b. one hundred and senior zero bits)
// Returns The length of the result & gt; = max (L1, L2, L3)
int.
ADD3 (Char Res [], char x3 [], int l1,
Char x1 [], int l2, char d [], int l3)
{
int i, s = 0;
for (i = 0; i & lt; l3; i ++) {
s + = x3 [i] + x1 [i] + d [i];
res [i] = S & amp; 1;
S & GT; & GT; = 1;
}
For (; i & lt; l2; i ++) {
s + = x3 [i] + x1 [i];
res [i] = S & amp; 1;
S & GT; & GT; = 1;
}
For (; i & lt; L1 & amp; & amp; s; i ++) {
s + = x3 [i];
res [i] = S & amp; 1;
S & GT; & GT; = 1;
}
if (s)
do {
res [i ++] = S & amp; 1;
} While (S & GT; & GT; = 1);
ELSE FOR (; I & LT; L1; I ++)
res [i] = x3 [i];
Return I;
}
int.
Correct_Nbits (Char A [], Int L)
{
While (L & GT; 0 & amp; & amp;! A [L])
L--;
Return L + 1;
}
#Define N (64 * 1024)
int.
Main ()
{
char array1 [n], array2 [n], a [4], * res, * prev_res;
INT NBITS, CURBITS = 1;
Memset (Array1, 0, Sizeof (Array1));
Memcpy (Array2, Array1, Sizeof (Array1));
res = Array1 + 3;
prev_res = Array2 + 3;
While ((nbits = getDecbits (a)) & gt; 0) {
Curbits = Add3 (Res, Prev_res - 3, Curbits + 3,
Prev_res - 1, Curbits + 1, A, NBITS);
Memcpy (Prev_res, Res, Curbits);
}
printf ("result: (% d bits) \ n", curbits = correct_nbits (res, curbits));
For (int i = curbits - 1; i & gt; = 0; i--)
// for (int i = 0; i & lt; curbits; i ++)
Putchar (res [i] + '0');
Puts ("");
RETURN PUTS ("END") == EOF;
}
AVP @ AVP-UBU1: ~ / hashcode $ gcc -std = gnu99 bigdec-bin.c
AVP @ AVP-UBU1: ~ / Hashcode $ ./a.out
15
RESULT: (4 Bits)
1111.
End.
AVP @ AVP-UBU1: ~ / Hashcode $ ./a.out
32.
Result: (6 Bits)
100000.
End.
AVP @ AVP-UBU1: ~ / Hashcode $
AVP @ AVP-UBU1: ~ / hashcode $ gcc -std = gnu99 bigdec-bin.c -o3
AVP @ AVP-UBU1: ~ / Hashcode $ ./mkdigs 30
367535629127093606261879202375.
AVP @ AVP-UBU1: ~ / Hashcode $ ./mkdigs 3 | ./a.out.
RESULT: (9 BITS)
101101111
End.
AVP @ AVP-UBU1: ~ / Hashcode $ Time ./mkdigs 1000 | ./a.Out & gt; / dev / null
Real 0m0.288s.
User 0m0.284s.
SYS 0M0.000S.
AVP @ AVP-UBU1: ~ / Hashcode $
In the source Source size of 64kb arrays, sufficient to convert
avp @ avp-ubu1: ~ / hashcode $ time ./mkdigs 19700 | ./a.Out & gt; / dev / null
Real 0m0.890s.
User 0m0.892s.
SYS 0M0.000S.
AVP @ AVP-UBU1: ~ / Hashcode $
about 19700 decimal numbers. But the source code ./mkdigs is not preserved, but it is trivial, based on Rand ()% 10
.
Answer 3
Guys, which 10 trilliones of PI characters calculated somehow made for O (n * log (n) ^ 2). You can download their sources and try to understand how. Or ask. http://www.numberworld.org/y-cruncher/#algorithms
Perhaps they are divided with the remainder of the initial number by 10 ^ n, where n is such that the number of numbers in general and the residue is the same. And then – recursively for the obtained halves.
UPD: If you are not interested in algorithm O (n * log (n) ^ 2), a decent algorithm O (n ^ 2) –
static const int bitsincell = 28;
unsigned int data [32000 / BITSINCEL + 10];
int i, datalen = 1;
char ch;
Memset (Data, 0, Sizeof (DATA));
for (i = 0; i & lt; 8; i ++)
{
ch = getc (stdin);
If (! Isdigit (CH))
Break;
DATA [0] = DATA [0] * 10 + CH - '0';
}
if (isDigit (CH))
While (True)
{
ch = getc (stdin);
If (! Isdigit (CH))
Break;
// multiply by 10
DATA [0] = DATA [0] * 10 + CH - '0';
for (i = 0; i & lt; datalen; i ++)
{
unsigned int carryvalue = data [i] / (1U & lt; & lt; bitsincell);
Data [i]% = 1U & LT; & LT; Bitsincell;
DATA [I + 1] = DATA [I + 1] * 10 + CARRYVALUE;
}
if (Data [Datalen] & gt; 0)
DataLen ++;
}
// Bita output from DATA
Answer 4
- Translate the original number into the number system with the base 10 9 sup>(you can void, it is possible through SSCANF ()). “Figures” Store in a 32-bit format.
- translate the “corner” the resulting number to the number system with a base 2 32 sup>, when divided, use extended multiplications and shifts. Recording residual digits in an array of result goes in the reverse order.
- Translate every new “digit” in a 32-character binary string, starting with the last (for example, via Sprintf ()). In the last line, remove the leading zeros, in the rest – save.