Extra large numbers using character strings

I am trying to write a program in C, which performs the multiplication of two numbers without directly using the multiplication operator and must take into account sufficiently large numbers, so that even the usual addition of these two numbers cannot be performed by direct addition.

I was motivated by this, when I tried (and successfully did) to write a C program that does an add using character strings, I did the following:

#include<stdio.h>
#define N 100000
#include<string.h>
void pushelts(char X[], int n){
int i, j;
for (j = 0; j < n; j++){
    for (i = strlen(X); i >= 0; i--){
        X[i + 1] = X[i];
    }
        X[0] = '0'; 
    }
}

int max(int a, int b){
    if (a > b){ return a; }
    return b;
}

void main(){
    char E[N], F[N]; int C[N]; int i, j, a, b, c, d = 0, e;
    printf("Enter the first number: ");
    gets_s(E);
    printf("\nEnter the second number: ");
    gets_s(F);
    a = strlen(E); b = strlen(F); c = max(a, b);
    pushelts(E, c - a); pushelts(F, c - b);
    for (i = c - 1; i >= 0; i--){
        e = d + E[i] + F[i] - 2*'0';
        C[i] = e % 10; d = e / 10;
    }
    printf("\nThe answer is: ");
    for (i = 0; i < c; i++){
        printf("%d", C[i]);
    }
    getchar();
}

"N". , ? -, , , , n ( 0 <= n <= 9). , ; (*). - ( ) . k ( a1a2..... ak) :

a1a2...ak = a1 x 10^(k - 1) + a2 x 10^(k - 2) + ... + ak-1 x 10 + ak

, , , (*).

x1x2..... xn, - y1y2.... yk, :

x1x2...xn x y1y2...yk = (x1x2...xn) x y1 x 10^(k-1) + .....

(*) (x1x2... xn) y1, 10 ^ (k-1) -1 ; , k , . , , , , . (x1x2.... xn) yi x 10 ^ (i-1), , , , , , . , char int . , , ; , , , . , , .

- ? .

+4
3

, Small Factorials SPOJ.

. , . :

               1234
               x 56
         ------------
              7404
            +6170-   // - is denoting the left shift
         ------------
             69140  

:

  • num1 = 1234, num2 = 56, left_shift = 0;
  • char_array [] = num1
  • result_array []
  • (2)
    • n = num2% 10
    • num2/= 10
    • carry = 0, = left_shift, j = 0
    • (char_array [J])
         . partial_result = char_array [j] * n + carry
        II. partial_result + = result_array [i]
       III. result_array [i ++] = partial_result% 10
         carry = partial_result/10
    • left_shift ++
  • result_array .

, , num1 num2 . , char. . num1 num2 char. . :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    char num1[200], num2[200];
    char result_arr[400] = {'\0'};
    int left_shift = 0;

    fgets(num1, 200, stdin);
    fgets(num2, 200, stdin);

    size_t n1 = strlen(num1);
    size_t n2 = strlen(num2);   

    for(size_t i = n2-2; i >= 0; i--)
    {
        int carry = 0, k = left_shift;
        for(size_t j = n1-2; j >= 0; j--)
        {
            int partial_result = (num1[j] - '0')*(num2[i] - '0') + carry;
            if(result_arr[k])
                partial_result += result_arr[k] - '0';
            result_arr[k++] = partial_result%10 + '0';
            carry = partial_result/10;  
        }
        if(carry > 0)
            result_arr[k] = carry +'0'; 
        left_shift++;
    }
    //printf("%s\n", result_arr);

    size_t len = strlen(result_arr);
    for(size_t i = len-1; i >= 0; i-- )
        printf("%c", result_arr[i]);
    printf("\n");   
}

, , .

+1

( user300234 384 x 56):

Set result="0" /* using your character-string representation */
repeat:
    Set N = ones_digit_of_multiplier /* 6 in this case */
    for (i = 0; i < N; ++i)
      result += multiplicand  /* using your addition algorithm */
    Append "0" to multiplicand /* multiply it by 10 --> 3840 */
    Chop off the bottom digit of multiplier /* divide it by 10 --> 5 */
    Repeat if multiplier != 0.
0

Bignum arithmetic is difficult to implement efficiently. Algorithms are pretty hard to understand (efficient algorithms are better than the naive ones you are trying to implement), and you can find several books on them.

I would suggest using an existing Bignum library like GMPLib , or using some language that provides bignums natively (like Common Lisp with SBCL )

0
source

All Articles