-2

what I'm looking for is using simple math on number in string

I'm trying to find the factorial of 100 and it's really big to put it in int or long long so I searched and found that string is the best Solution but

I can't multiply numbers in string or even put in int the multiply then put it back in string and I can't use libraries not from the std of c++ and one can help me

Mahmoud Al-Qudsi
  • 28,357
  • 12
  • 85
  • 125
MD9
  • 90
  • 1
  • 7
  • 1
    Of every 1000 times someone says they can't use external libraries, 999 times they don't know what they want. – Mahmoud Al-Qudsi Jan 27 '13 at 17:39
  • Please ask a concise question. From what I grasped, you are looking for something like http://gmplib.org/ – Dan Jan 27 '13 at 17:54

2 Answers2

2

To solve this, you will have to write some code that multiplies a number with another number. Here's a function that multiplies the content of a string by two:

void times_two(char *str)
{
    int carry = 0;
    for(int i = DIGITS-2; i >= 0; i--)
    {
    int t = str[i] - '0';
    t *= 2;
    t += carry;
    str[i] = (t % 10) + '0';
    carry = (t > 9);
    }
}

It presumes that the string is DIGITS characters long and "adjusted" to the right of the string with zeros to fill it out.

Of course, if you try to multiply by "more than a single digit", you will have to loop over the length of the number you are multiplying by, and you also have to care about "carry" being more than one for anything over two. But the principle is the same.

[I'm intentionally not rewriting my function above to cope with those two scenarios, becuase the prupose of you doing the factorial of 100 isn't to find the answer, but to learn how to solve programming problems. If all you wanted to do is find the answer, you could just use a modern calculator!]

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • I don't know. You could always write your code such that it expands the string as needed, rather than having a fixed size. The size for a multiplied, X * Y, is the number of digits in X + digits in Y or less. (e.g. 12 * 12 = 3 digits, 99 * 99 = 4 digits, 12 * 100 = 4 digits, 999 * 999 = 6 digits, etc). – Mats Petersson Jan 27 '13 at 17:55
  • #include #include #include using namespace std; void times_two(string& str,int x); int main() { string ss; int x; ss = "1"; cin>>x; for(int i = 1 ; i <= x ; i++) times_two(ss,i); cout<= 0; i--) { int t = str[i] - '0'; t *= x; t += carry; tep[i] = (t%10) + '0'; carry = t/10; } ostringstream convert; convert << carry; if(carry !=0) str = convert.str() ; else str = ""; str += tep; } thx alot – MD9 Jan 27 '13 at 18:55
  • Very hard to read code when it's comacted like that. Shouldn't be called "times_two" when it's not times two! – Mats Petersson Jan 27 '13 at 19:15
  • I modified you code to be for all the number and used it in factorial – MD9 Jan 27 '13 at 19:27
  • Yes, my comment about the name is that in your posted code it's still saying "times_two", which of course isn't true. Question is: Does it work? – Mats Petersson Jan 27 '13 at 19:28
  • yes I could find the factorial of 100 in 3.2 sec – MD9 Jan 27 '13 at 19:30
  • Doesn't sound blindingly fast, but I guess the important thing is to get the right result. – Mats Petersson Jan 27 '13 at 19:32
  • yes, and thank you again for help I want to vote for you but I can't because I'm new – MD9 Jan 27 '13 at 19:33
1

Here's the code I once wrote runs in O(n^2) time. There are better algorithms though like fast fourier transformatons(fft) which runs in O(nlog n) time. The multiply functions accepts 2 strings(numbers) and returns their product.

#define itc(n) char(n+48)
#define cti(ch) (ch-48)
 string itos(lld n)
    {
        ostringstream convert;
        convert<<n;
        return convert.str();
    }   



string add(string s1, string s2)
{
    int len1=s1.length(), len2=s2.length();
    if(len1<len2)                                   //s1 should be of greater length than s2
        return add(s2, s1);
    string ans="";
    int carry=0, i, s;
    for(i=1;i<=len1;i++)
    {
        s = carry+cti(s1[len1-i]);

        if(i<=len2)
            s += cti(s2[len2-i]);

        ans = itc(s%10)+ans;                        //finding the character to be added to the ans
        carry = s/10;                               //finding the carry
    }
    if(carry!=0)
        ans = itc(carry)+ans;

    return ans;
}

     string multiply(string s1, string s2)
        {
            int len1=s1.length(), len2=s2.length();
            if(len1<len2)
                return multiply(s2,s1);
            int i,j,p, carry=0;
            string result, net="", c;
            for(i=len2-1;i>=0;i--)
            {
                carry=0;
                result="";
                c="";
                for(j=len1-1;j>=0;j--)
                {
                    p=cti(s1[j])*cti(s2[i]);
                    result = itc((p+carry)%10)+result;
                    carry=(p+carry)/10;
                }
                if(carry!=0)
                {
                    c=itos(carry);
                    result=c+result;

                }
                for(j=i;j<len2-1;j++)
                    result+="0";

                net=add(net,result);
            }
            return net;
        }
sudeepdino008
  • 3,194
  • 5
  • 39
  • 73
  • I guess @Mats Petersson's answer explains it all. I followed a similar logic. multiplying n1 by a single digit and shifting the results and then adding them. Similar to the we did multiplications in middle school. – sudeepdino008 Jan 28 '13 at 11:39