Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6057345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:28:42+00:00 2026-05-23T08:28:42+00:00

Ive tried to create my own implementation of a bignum library I cant seem

  • 0

Ive tried to create my own implementation of a bignum library
I cant seem to get the factorial to work. If I ask it to solve 4!, it gives out 96. It multiplies 4 twice. similarly, 5! is 600, not 120. I haven’t implemented division, so I cant/dont want to divide the answer by the number

//bignum project
#include <iostream>
using namespace std;

class bignum
{
      public:
      int number[100];
      int dpos;
      int operator/ (bignum);
      bignum operator- (bignum);
      bignum operator* (bignum);
      bignum operator+ (bignum);
      bignum operator= (string);     
      void output()
      {
           int begin=0;
           for(int i=0; i<=99; i++)
           {
                   if(number[i]!=0 || begin==1)
                   {
                                   cout<<number[i];
                                   begin=1;
                   }
           }
      }
};

bool num_is_zero(bignum k)
      {
           for(int a=0; a<=99; a++)
           {
                   if(k.number[a]!=0)
                   {
                                     return false;
                   }
           }
           return true;
      }

 bignum factorial(bignum a)
      {
             bignum j;
             bignum fact;
             fact="1";

             while(!num_is_zero(a))
             {
                                   j="1";
                                   fact=fact*a;
                                   a=a-j;                         
             }
             return fact;
      }

bignum bignum::operator= (string k)
{
       int l;
       l=k.length()-1;
       for(int h=0; h<=99; h++)
       {
                   number[h]=0;
       }
       for(int a=99; a>=0 && l>=0; a--)
       {
               number[a]=k[l]-'0';
               l--;
       }
}

bignum bignum::operator+ (bignum b) 
{
  bignum a;
  int carry=0;
  for(int k=0; k<=99; k++)
  {
                   a.number[k]=0;
  }
  for(int i=99; i>=0; i--)
  {
          a.number[i]= number[i]+b.number[i]+a.number[i];
          if(a.number[i]>9)
          {
                           carry=(a.number[i]/10);
                           a.number[i-1]+=carry;
                           a.number[i]=(a.number[i]%10);
          }

  }
  return (a);
}

bignum bignum::operator- (bignum c) 
{
  bignum a;
  int sign=0;
  for(int k=0; k<=99; k++)
  {
                   a.number[k]=0;
  }
  for(int i=99; i>=0; i--)
  {
          if(number[i]<c.number[i])
          {
                    number[i]+=10;
                    if(i!=0)
                    number[i-1]--;

          }
          a.number[i]=number[i]-c.number[i];
  }
  return (a);
}

bignum bignum::operator* (bignum b) 
{
  bignum ans;
  int ans_grid[100][100],x,lines=0,carry,sum[100];
  for(int a=0; a<=99; a++)
  {
          for(int b=0; b<=99; b++)
          {
                  ans_grid[a][b]=0;
          }
  }
  for(int i=99; i>=0; i--)
  {
          for(int j=i,x=99; j>=0; j--,x--)
          {       
                    ans_grid[lines][j]=(number[i]*b.number[x]);       
          } 
          lines++;        
  }

//------------------------------------------------Carry Forward and assign to ans------------------------------------------------//
  for(int j=99; j>=0; j--)
  {
          for(int i=99; i>=0; i--)
          {
                  if(ans_grid[j][i]>9 && i!=0)
                  {
                                      carry=(ans_grid[j][i]/10);
                                      ans_grid[j][i-1]+=carry;
                                      ans_grid[j][i]%=10;
                  }
          }
  } 
  for(int col=99; col>=0; col--)
  {
          for(int row=99; row>=0; row--)
          {
                  sum[col]+=ans_grid[row][col];
          }
  }
  for(int i=99; i>=0; i--)
          {
                  if(sum[i]>9 && i!=0)
                  {
                                      carry=(sum[i]/10);
                                      sum[i-1]+=carry;
                                      sum[i]%=10;
                  }
          }
  for(int l=0; l<=99; l++)
  ans.number[l]=sum[l];
//-------------------------------------------------------------------------------------------------------------------------------//
  return (ans);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:28:42+00:00Added an answer on May 23, 2026 at 8:28 am

    I think your problem is that sum is uninitialized in your operator*. I’m reluctant to give a partial answer, since I took the code and fiddled with it a bit, so here’s my version which appears to work (and also prints “0” correctly):

    Update: I couldn’t resist making several structural improvements. I didn’t touch your multiplication routine, so you should still be able to extract the bugfix even if you don’t care for the rest.

    #include <iostream>
    #include <string>
    #include <algorithm>
    
    using namespace std;
    
    class bignum
    {
    public:
      int number[100];
    
      bignum() { std::fill(number, number + 100, 0); }
      bignum(const bignum & other) { std::copy(other.number, other.number + 100, number); }
      bignum(const std::string &);
    
      bignum & operator-=(const bignum &);
      inline bignum operator-(const bignum & c) const { return bignum(*this) -= c; }
      bignum operator*(const bignum &) const;
      inline bignum & operator= (const std::string & k) { return *this = bignum(k); }
    
      inline operator bool() const
      {
        for (size_t a = 0; a < 100; ++a)
          if (number[a] != 0) return true;
        return false;
      }
    
    };
    
    std::ostream & operator<<(std::ostream & o, const bignum & b)
    {
      bool begun = false;
    
      for (size_t i = 0; i < 100; ++i)
      {
        if (begun || b.number[i] != 0)
        {
          cout << b.number[i];
          begun = true;
        }
      }
    
      if (!begun) o << "0";
    
      return o;
    }
    
    bignum::bignum(const std::string & k)
    {
      std::fill(number, number + 100, 0);
      for(size_t h = 0; h < std::min(k.length(), 100U); ++h)
        number[99 - h] = k[k.length() - 1 - h] - '0';
    }
    
    bignum & bignum::operator-=(const bignum & c)
    {
      for (int i = 99; i >= 0; --i)
      {
        if (number[i] < c.number[i])
        {
          number[i] += 10;
          if (i != 0) --number[i-1];
        }
        number[i] -= c.number[i];
      }
      return *this;
    }
    
    bignum bignum::operator*(const bignum & b) const
    {
      bignum ans;
      int ans_grid[100][100], lines = 0, carry, sum[100];
    
      std::fill(sum, sum + 100, 0);
      for (size_t i = 0; i < 100; ++i) std::fill(ans_grid[i], ans_grid[i] + 100, 0);
    
      for(int i=99; i>=0; i--)
        {
          for(int j=i,x=99; j>=0; j--,x--)
            {
              ans_grid[lines][j]=(number[i]*b.number[x]);
            }
          lines++;
        }
    
      //------------------------------------------------Carry Forward and assign to ans------------------------------------------------//
      for(int j=99; j>=0; j--)
        {
          for(int i=99; i>=0; i--)
            {
              if(ans_grid[j][i]>9 && i!=0)
                {
                  carry=(ans_grid[j][i]/10);
                  ans_grid[j][i-1]+=carry;
                  ans_grid[j][i]%=10;
                }
            }
        }
      for(int col=99; col>=0; col--)
        {
          for(int row=99; row>=0; row--)
            {
              sum[col]+=ans_grid[row][col];
            }
        }
      for(int i=99; i>=0; i--)
        {
          if(sum[i]>9 && i!=0)
            {
              carry=(sum[i]/10);
              sum[i-1]+=carry;
              sum[i]%=10;
            }
        }
      for(int l=0; l<=99; l++)
        ans.number[l]=sum[l];
      //-------------------------------------------------------------------------------------------------------------------------------//
      return (ans);
    }
    
    bignum factorial(bignum a)
    {
      bignum j; j = "1";
      bignum fact; fact="1";
    
      while(a)
      {
        fact = fact * a;
        a = a-j;
      }
      return fact;
    }
    
    
    int main(int argc, char * argv[])
    {
      if (argc < 2) return 0;
      bignum a;
      a = std::string(argv[1]);
      bignum b = factorial(a);
      cout << a << std::endl << b << std::endl;
    }
    

    The string assignment is still broken for strings with more than one digit, but that wasn’t your question I suppose…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is the first Python script I've tried to create. I'm reading a xml
I'm trying to create a table and I've tried so many times to figure
I've tried asp.net and jsp...and the programming language was powerful enough to create both
I'm tring to create a search based on date. I've tried so many different
I'm trying to create an image containing text. I've tried several examples I've found
I'm trying to figure out how to create a http CRAN-repository. I've tried to
Ive tried to hunt down the tutorial for notepad on android developers but it
Ive tried searching for hours now and cannot find out why my code (aka,
I've created a Windows library with an implementation of nsIModule (and nsIProtocolHandler) a while
I'm trying to create a templated composite control that would work in a similar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.