Question

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

Was it helpful?

Solution

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!]

OTHER TIPS

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;
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top