Solving Arithmetic on Large Positive Integers with Bignum Strings

In summary: Incrementing is just adding 1, and decrementing is subtracting 1 (or adding 9999...99 and ignoring the overflow).Comparing Bignum strings can be done by looping through each digit and comparing them from left to right. Whichever string has a larger digit at a certain position is considered the larger string overall.
  • #1
gruba
206
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
  • #2
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
 
  • #3
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;
    }
 

1. What are Bignum strings?

Bignum strings are a data structure used in computer programming to store and manipulate large positive integers that cannot be represented by the standard integer data type. They are represented as strings of digits and can handle numbers of any length.

2. How are Bignum strings used to solve arithmetic on large positive integers?

Bignum strings allow for the storage of large positive integers that cannot be represented by standard data types, making it possible to perform arithmetic operations on these numbers. They also have built-in methods and functions for performing basic arithmetic operations such as addition, subtraction, multiplication, and division.

3. What are some advantages of using Bignum strings for arithmetic on large positive integers?

Bignum strings allow for the handling of arbitrarily large numbers, making it possible to solve complex mathematical problems that would be impossible with standard data types. They also have built-in error handling and can handle numbers with a high level of precision.

4. Are there any limitations to using Bignum strings for solving arithmetic?

One limitation of using Bignum strings is that they can only handle positive integers. They also require more computational resources than standard data types, so they may not be the most efficient solution for all problems.

5. How do Bignum strings compare to other methods of solving arithmetic on large positive integers?

Bignum strings are a popular choice for solving arithmetic on large positive integers because they are easy to use and offer a wide range of built-in functions for performing basic arithmetic operations. However, there are other methods such as using libraries or implementing custom algorithms that may be more efficient for certain types of problems.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
893
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
Back
Top