Solving Arithmetic on Large Positive Integers with Bignum Strings

Click For Summary
SUMMARY

This discussion focuses on implementing arithmetic operations on large positive integers using Bignum strings without the GMP library. The program supports addition, subtraction, multiplication, and division for integers represented as strings, with a maximum of 100 digits. Key functions include reading numbers, performing addition, and printing results, with specific attention to handling carry during addition. The code provided demonstrates how to increment and decrement Bignum strings by manipulating their string representations directly.

PREREQUISITES
  • Understanding of C programming language
  • Familiarity with data structures, specifically arrays
  • Knowledge of string manipulation in C
  • Basic concepts of arithmetic operations
NEXT STEPS
  • Implement subtraction for Bignum strings in C
  • Explore multiplication algorithms for large integers, such as Karatsuba multiplication
  • Learn about division algorithms for Bignum strings
  • Investigate optimization techniques for Bignum arithmetic operations
USEFUL FOR

Software developers, computer science students, and anyone interested in implementing custom arithmetic operations on large integers without relying on external libraries.

gruba
Messages
203
Reaction score
1

Homework Statement


Write a program (without using GMP library - https://gmplib.org) which performs arithmetic operations on large positive integers (addition, subtraction, multiplication and division). Maximum number of digits in one number is 100.
Large number is the number that can't be represented by standard data types (long long, ...), and it is represented as a string.
Then allow incrementing and decrementing of a string, and finding the smallest and the largest string in an array of n strings.

2. The attempt at a solution

How would you do incrementing, decrementing and comparing Bignum strings?
Here is the addition:
Code:
    #include<stdio.h>
    #define MAX 100//max. number of digits
    typedefstruct
    {
        int arr[MAX];//array for one number
    }NUMBER;
    void read(NUMBER *add_num)//read one number
    {
        int i,digit=0;
        char ch[101];
        scanf("%s",ch);

        while(ch[digit])//number of digits
            digit++;

        for(i=0;i < MAX;i++)
        {
            digit--;
            if(digit >=0)
                add_num->arr[i]=ch[digit]-'0';//int to char
            else
                add_num->arr[i]=0;
        }
    }

    void addition(NUMBER a,NUMBER b,NUMBER *add_res)//add two Bignum strings
    {
        int carry=0;
        int i,temp;
        for(i=0;i < MAX;i++)
        {
            temp=a.arr[i]+b.arr[i]+carry;//sum
            add_res->arr[i]=temp %10;//resulting digit
            carry=temp /10;//store carry
        }
    }

    voidprint(NUMBER *add_num)//print result
    {
        int i;

        for(i=MAX-1;add_num->arr[i]==0;i--);//skip the leading zeros

        for(;i>=0;i--)
            printf("%d",add_num->arr[i]);
    }
    int main()
    {
        NUMBER x,y,z;
        printf("enter two positive integers: \n");
        read(&x);
        read(&y);
        printf("addition result: ");
        addition(x,y,&z);
        print(&z);
        return0;
    }
 
Physics news on Phys.org
Incrementing is just adding 1, and decrementing is subtracting 1 (or adding 9999...99 and ignoring the overflow)

How do you compare numbers like those by eye? The same can be done in code.
Code:
35356959849283293
35846625235464635
 4735939384293894
 
There are a couple of missing spaces in your code, below.
typedefstruct needs a space after typedef
voidprint needs a space after void
gruba said:

Homework Statement


Write a program (without using GMP library - https://gmplib.org) which performs arithmetic operations on large positive integers (addition, subtraction, multiplication and division). Maximum number of digits in one number is 100.
Large number is the number that can't be represented by standard data types (long long, ...), and it is represented as a string.
Then allow incrementing and decrementing of a string, and finding the smallest and the largest string in an array of n strings.

2. The attempt at a solution

How would you do incrementing, decrementing and comparing Bignum strings?
Here is the addition:
Code:
    #include<stdio.h>
    #define MAX 100//max. number of digits
    typedefstruct
    {
        int arr[MAX];//array for one number
    }NUMBER;
    void read(NUMBER *add_num)//read one number
    {
        int i,digit=0;
        char ch[101];
        scanf("%s",ch);

        while(ch[digit])//number of digits
            digit++;

        for(i=0;i < MAX;i++)
        {
            digit--;
            if(digit >=0)
                add_num->arr[i]=ch[digit]-'0';//int to char
            else
                add_num->arr[i]=0;
        }
    }

    void addition(NUMBER a,NUMBER b,NUMBER *add_res)//add two Bignum strings
    {
        int carry=0;
        int i,temp;
        for(i=0;i < MAX;i++)
        {
            temp=a.arr[i]+b.arr[i]+carry;//sum
            add_res->arr[i]=temp %10;//resulting digit
            carry=temp /10;//store carry
        }
    }

    voidprint(NUMBER *add_num)//print result
    {
        int i;

        for(i=MAX-1;add_num->arr[i]==0;i--);//skip the leading zeros

        for(;i>=0;i--)
            printf("%d",add_num->arr[i]);
    }
    int main()
    {
        NUMBER x,y,z;
        printf("enter two positive integers: \n");
        read(&x);
        read(&y);
        printf("addition result: ");
        addition(x,y,&z);
        print(&z);
        return0;
    }
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K