Division Algorithm

This is a discussion on Division Algorithm within the C++ Programming forums, part of the General Programming Boards category; i know this post is old, but i have the exact same problem that i am working on.. i need ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    2
    i know this post is old, but i have the exact same problem that i am working on.. i need to divide 2^512 +1 by another 49 digit number, but so far only got 2 by 2 digits working.. anyone got any ideas?

    Code:
    // need to add for loops for array a
    #include <iostream.h>
     
    int main()
    {
        const int sizea = 2;
        const int sizeb = 2;
        
        int a[sizea] = { 5, 7 };
        int b[sizeb] = { 2, 5 };
        int counter[sizea] = { 0 };
        int remainder = 0;
     
    //    for (int i = 0; i < sizea; i+=sizeb)
        for (int i = 0; i < sizea; i++)
        {
    
            // Do division (if necessary)
            int count = 0;
             
            while (a[i] >= b[i])
            {
                if (a[(i+1)] < b[(i+1)])
                {
                    a[i] = a[i] - 1;
                    a[(i+1)] = a[(i+1)] + 10;
                }
                
                if (a[i]>=b[i])
                {
                    a[i] -= b[i];
                    a[i + 1] -= b[i + 1];
                    ++count;
                }
    
            }
            
            counter[i + 1] = count;
     
            // Handle remainder
            if (a[i]==0)
            {
                remainder += a[i + 1];
                a[i] = 0;
            }
            else
            {
                a[i + 1] = ( a[i] * 10 ) + a[i + 1];
                remainder = a[i + 1];
                a[i] = 0;
            }
            
        }
     
        // Output
        for (int j = 0; j < sizea; ++j)
        {
            cout << counter[j];
        }
        
        cout << " remainder " << remainder << endl;
     
        return 0;
    }

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    Next time start your own post, but why don't you check out the algorithms used by gmp for division
    http://www.swox.com/gmp/manual/Divis...n%20Algorithms
    or even look at the source code that they use

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    2
    thought id carry on the post as it was similar topic....
    but anyways....
    its part of a uni assignment, where i have to use arrays, so this is what i got so far, if you remove the comment from line 15 and comment out line 16 it works fine for 2 by 2, but i cant get it to work out right for 3 by 2.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Division Algorithm
    By author in forum C Programming
    Replies: 4
    Last Post: 04-15-2005, 12:02 PM
  2. Division Algorithm
    By LuckY in forum C++ Programming
    Replies: 27
    Last Post: 11-15-2004, 09:20 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21