Calculating products of large numbers using arrays?

回眸只為那壹抹淺笑 提交于 2021-02-08 09:11:20

问题


I have a project (for school) and I absolutely cant use any external libraries hence cannot use any big numbers library and I need to get the product of 2 (very) large numbers. So I thought I'll actually write my own code for it but I cant seem to get pass single digit multiplications.

How I've done it so far is I have an array of chars 'a'. And Ill multiply each of its digits with the other number (since no multiplication can go beyond 81 ie, 9*9). But I cant seem to figure out how Ill multiply two arrays with each other.

As in,

int a[] = {1,2,3};
int b[] = {4,5,6};

int r[200]; // To store result of 123x456. After processing should have value 56088

Heres my code so far...

#include <iostream>
using namespace std;

void reverseArray(int array[], int n)
{
    int t;
    for(int i=0;i<n/2;i++)
    {
        t = array[i];
        array[i] = array[n-i-1];
        array[n-i-1] = t;
    }
}

int main()
{
    int A[] = {1,2,6,6,7,7,8,8,8,8,8,8,8,8,8,8};
    int s = sizeof(A)/sizeof(int);
    int n = s-1;

    int R[50];


    int x = 2;

    int rem = 0;

    for(int i=0; i<s; i++)
    {
        R[i] = (A[n-i] * x) % 10;
        R[i] += (rem != 0) ? rem:0;
        rem = (A[n-i] * x) / 10;
    }

    reverseArray(R, s);

    for(int i=0; i<s; i++) cout<<R[i]; // Gives 2533557777777776

}

I also found a similar program here which calculates factorials of very large numbers. But I cant seem to understand the code enough to change it to my needs.

Sorry if the question is a little sketchy.

Thanks.


回答1:


Just do the same thing as you are doing now, but for each digit in the second array - in other words, instead of x use B[j], where j is a loop over all the digits in the array B.




回答2:


I'm not sure what algorithm you learned in elementary school to multiply two arbitrary numbers, but visually it goes something like this:

   43241
     621
   ----- *
   43241 <--   1 * 43241
  864820 <--  20 * 43241, basically do 2 * 43241 and append a zero
25944600 <-- 600 * 43241, basically do 6 * 43241 and append two zeroes
-------- +
26852661 <-- Add up results, remember to carry

So in this particular example, the arrays would be A[] = {1,4,2,3,4} and B[] = {1,2,6}. You can then just do a for loop, like

int tempArray[50]; // something big enough
for (int n = 0; n < max; n++)
{
    multiplyArrayWithNumber(A, B[i], tempArray, i);
    addArraysAndStore(resultArray, tempArray, resultArray);
}

where the functions multiplyArrayWithNumber and addArraysAndStore might have the signature

void multiplyArrayWithNumber(const int* array, const int number, int* resultArray, const int zeroesAppended);

void addArraysAndStore(const int* lefthandside, const int* righthandside, int* result);



回答3:


I have done in java, Here I am taking to numbers N1 and N2, And I have create an array of size 1000. Lets take an example How to solve this, N1=12, N2=1234. For N1=12, temp=N1%10=2, Now Multiply this digit with digit N2 from right to Left and store the result into array starting from i=0, similarly for rest digit of N1. The array will store the result but in reverse order. Have a looking on this link. http://ideone.com/UbG9dW#view_edit_box

//Product of two very large number
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

 class Solution {
    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        int N1=scan.nextInt();
        int N2=scan.nextInt();
        //int N=scan.nextInt();
        int [] array=new int[1000];
        Arrays.fill(array,0);
        int size=multiply(N1,N2,array);
        for(int i=size-1;i>=0;i--){
            System.out.print(array[i]);
        }
    }
    public static int multiply(int N1, int N2, int [] result){
        int a=N1;
        int b=N2;            
        int count=0, carry=0;
        int i=0;
        int max=0;
        if(a==0||b==0)
            return 1;
        while(a>0){
            int temp1=a%10;
            a=a/10;
            i=0;
            while(b>0){
                int temp2=b%10;
                b=b/10;
                int product=result[count+i]+temp1*temp2+carry;
                result[count+i]=product%10;
                carry=product/10;
                i++;
                //System.out.println("ii="+i);
            }
            while(carry>0){
                result[count+i]=carry%10;
                carry=carry/10;
                i++;
                //System.out.println("iiii="+i);
            }
            count++;
            b=N2;
        } 
        //System.out.println("i="+i);

        return i+count-1;
    }
}


来源:https://stackoverflow.com/questions/18357925/calculating-products-of-large-numbers-using-arrays

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!