Thread: This codes written with TinyC multiplies strings. Still has some comment lines.

  1. #1
    Registered User
    Join Date
    Oct 2021
    Posts
    12

    This codes written with TinyC multiplies strings. Still has some comment lines.

    It is operational but should be edited for use.
    Code:
    /*
    This code is designed to multiply two numbers as *strings* not integers.
    The largest unsigned long long int is supposedly 18,446,744,073,709,551,615, around 2^64 – 1 
    This should be able to handle larger integers written as strings.
    Numbers larger than 18 digits.
    
    
    It was written with tiny c 927 and is designed for standard c.
    Microsoft says max string length in c is 2k ansi standard is 509.
    
    
    */
    
    
    #include <stdlib.h>                                                    // used by atoi and malloc
    #include <stdio.h>                                                    // printf library
    #include <string.h>                                                    // library manipulates strings.
    
    
    // subtract 48 do mult and  mul[j]='\0';
    // 1000 length for a string is 2k.
    // ANSI compatibility requires a compiler to accept up to 509
    
    
    int main(void)    
    {
        char            primep1[100]="87875285724157870424814863206395626902470222951900326730049585044816226015610272"; //part1
        
        // these are test numbers1
        // these are test numbers2
        char            mtest1[]="762777762777762777762777762777762777762777762777";            
        char            mtest2[]="438777762377762777762773762777762777762777762777";                                        
        
        char            ansreg[500];                                        // array of the calculated answer without trailing zeros
        char            anspad[500];                                        // array of the with added zeros in front to make all entries same length.
        char            anssum[500];                                        // array of string2array line to sum string for addition.
        char            anstot[500];                                        // array of the string2array totals reversed and then *final*
        
        char             ch[10];                                                // this is used to store integer digits as strings.
        
        /******************************* string2array[count1] = (char *)malloc(strlen(string3array)+1); *******************************/
        char             *string2array[400];                                    // REQUIRED number of string LINES array where the ascii values are stored. 
        
                                                                            
                                                                            
        int                num1=0, num2=0, num3=0, num4=0, num5=0, num6=0;
        int                i=0, j=0, t=0, v=0, n=0;
        int                sizestr1=0, sizestr2=0,  sizestrtot=0;
        int                rm1=0, carry1=0;
        int             mcount1=0;
        int                length1=0, length2=0;
        
        sizestr2=strlen(mtest2);                                            // length of number to evalute 1
        sizestr1=strlen(mtest1);                                            // length of number to evalute 1
        sizestrtot=sizestr2+sizestr1;                                        // length of both strings together
        
        /************************************This section does the multiplication ************************************/
        
        for (i=0; i<300; i++ )     {string2array[i] = (char *)malloc(sizestrtot+4);}    // initializes 300 string arrays dynamically based on strlen
        
        for ( i=strlen(mtest2)-1; i >= 0; i-- )
        {
            // printf("\ni:%d\n",i);
            for ( n=strlen(mtest1)-1; n >= 0; n-- )
            {
                
                // printf("\nn:%d\n",n);
                num1=0;
                num1=((mtest1[n]-48)*(mtest2[i]-48)+carry1);
                // printf("%d*%d", (mtest1[n]-48), (mtest2[i]-48));        
                // printf("\nnum1:%d\n",num1);
                rm1=num1%10;
                itoa (rm1, ch, 10);
                strcat(ansreg, ch);
                // printf("\n rm1:%d\n",rm1);
                // printf("\n ansreg:%s", ansreg);
                carry1=(num1/10);
                // printf("\n carry1:%d\n",carry1);
                // printf("\nn:%d\n", n);
                if ( n == 0 )
                {     
                    itoa (carry1, ch, 10); 
                    strcat(ansreg, ch);
                    strrev(ansreg);                                                        // reverses string with string.h library
                    // printf("\n ansreg:%s", ansreg);
                }
                
            }
            
            for (v=0; v<mcount1; v++) { strcat ( ansreg, "0"); }                        // this pads the answer with trailing zeros.
            // printf("\nmcount1:%d",mcount1);
            
            for ( v=strlen(ansreg); v<= sizestrtot+1; v++)  { strcat(anspad, "0"); }    // this pads the answer with leading zeros.
            
            strcat(anspad, ansreg);
            // printf("\npadded:%s", anspad);
            
            strcpy(string2array[mcount1], anspad);
            strcpy(anspad, "");                                                            // erases the string before another loop.
            strcpy(ansreg, "");                                                            // erases the string before another loop.
            // printf("\nlined:%s",string2array[mcount1]);                                    // test print
            carry1=0;
            rm1=0;
            mcount1++;
        }
        /***************************************does the addition************************************************************/
        for (t=0; t<mcount1; t++)    
            if ((strlen(string2array[0]))!=(strlen(string2array[t])))
                printf("\nERROR incorrect length at %s", string2array[t]);
        
        length2=strlen(string2array[0]);
                                                                        
        strcpy(anstot, "");                                                                
        for (v=length2-1; v>=0; v--)
        {        
            
            for (t=0; t<mcount1; t++)                                                        // test print of the full pointer array.
            {
                
                
                // printf("\nstring2arrayed:%s",string2array[t]);
                strcpy(anssum, string2array[t]);
                num6=(anssum[v]-48);
                
                // printf("\nnum6:%d",num6);
                
                // printf("\nnum5:%d\n", num5);
                
                // printf("\n%d+%d+%d\n",carry1, num5, num6);
                num5=carry1+num5+num6;
                carry1=0;
                
                
                
            }
            rm1=num5%10;
            // printf("\n rm1:%d\n",rm1);
            carry1=(num5/10);
            // printf("\n carry1:%d\n",carry1);
            itoa (rm1, ch, 10);
            
            strcat(anstot, ch);
            // printf("\n\n%s", anssum);
            // printf("\nfirstnum:%d", rm1);
            // printf("\n\nANSWER%s", anstot);
            num5=0;
        }
        for (t=0; t<mcount1; t++)
            printf("\nstring2arrayed:%s",string2array[t]);
        // printf("\n\nANSWER:%s", anstot);
        strrev(anstot);
        printf("\n\nANSWER:%s", anstot);
        strcpy(anstot, "");
        num6=0;
        free(string2array);
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,187
    The 509 limit is C89's limit for string literals, not arrays. It's raised to 4095 in C99. Implementations are likely to set it much higher or make it unlimited.

    A char array would presumably have a limit of at least 65535, although in an implementation it's likely to be much higher or unlimited. Still, you will eventually run out of stack space.

    This code is terrible and seems to get the wrong answer anyway.
    The correct answer is:
    762777762777762777762777762777762777762777762777 multiplied by
    438777762377762777762773762777762777762777762777 yields
    33468991994314270147625675870709226542582375938117 6420591167368409034853752403418845085286751729

    It is an insane strategy to do all the single-digit multiplications individually, store them in an array, and finally add them up. And constant use of strcat and itoa is inefficient and easily avoided. It looks like it was written by a freemason.
    We live as it were by chance, and by chance we are governed. - Seneca

  3. #3
    Registered User
    Join Date
    Oct 2021
    Posts
    12
    I wrote that code sometime in space it is not exemplary. Here is the manual for TinyC.

    Tiny C Compiler Reference Documentation (bellard.org)
    Last edited by cprogramnoob; 4 Weeks Ago at 06:13 PM.

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,187
    A simple example:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
    char *mult(const char *a, const char *b) {
        int alen = strlen(a), blen = strlen(b);
        char *c = calloc(alen + blen + 1, 1); // allocate and zero
        int k = 0;
        for (int i = alen; i-- > 0; ) {
            int ai = a[i] - '0', carry = 0;
            k = alen - i - 1;
            for (int j = blen; j-- > 0; ++k) {
                c[k] += ai * (b[j] - '0') + carry;
                carry = c[k] / 10;
                c[k] %= 10;
            }
            c[k++] = carry;
        }
        // remove leading zeros
        while (k > 1 && c[k - 1] == '\0') --k;
        // reverse and add '0'
        for (char *s = c, *e = c + k; s < e--; ++s) {
            char t = *s + '0'; *s = *e + '0'; *e = t;
        }
        return c;
    }
     
    int main() {
        char a[] = "762777762777762777762777762777762777762777762777";
        char b[] = "438777762377762777762773762777762777762777762777";
        char *c = mult(a, b);
        printf("%s\n", c);
        free(c);
        return 0;
    }
    We live as it were by chance, and by chance we are governed. - Seneca

  5. #5
    Registered User
    Join Date
    Oct 2021
    Posts
    12

    thoughts on breaking the prime factors for this?

    RSA-260 = 22112825529529666435281085255026230927612089502470 01539441374831912882294140200198651272972656974659 90859003300314000511707422045608592763579537571859 54298838958709229238491006703034124620545784566413 6645406842143612930176940208 46391065875914794251435144458199

    What are then chances this can be factored?

  6. #6
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    243
    Quote Originally Posted by john.c View Post
    A simple example:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
    char *mult(const char *a, const char *b) {
        int alen = strlen(a), blen = strlen(b);
        char *c = calloc(alen + blen + 1, 1); // allocate and zero
        int k = 0;
        for (int i = alen; i-- > 0; ) {
            int ai = a[i] - '0', carry = 0;
            k = alen - i - 1;
            for (int j = blen; j-- > 0; ++k) {
                c[k] += ai * (b[j] - '0') + carry;
                carry = c[k] / 10;
                c[k] %= 10;
            }
            c[k++] = carry;
        }
        // remove leading zeros
        while (k > 1 && c[k - 1] == '\0') --k;
        // reverse and add '0'
        for (char *s = c, *e = c + k; s < e--; ++s) {
            char t = *s + '0'; *s = *e + '0'; *e = t;
        }
        return c;
    }
     
    int main() {
        char a[] = "762777762777762777762777762777762777762777762777";
        char b[] = "438777762377762777762773762777762777762777762777";
        char *c = mult(a, b);
        printf("%s\n", c);
        free(c);
        return 0;
    }
    Now THAT is an elegant solution. Nice chops mate!

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    791
    Quote Originally Posted by cprogramnoob View Post
    RSA-260 = 22112825529529666435281085255026230927612089502470 01539441374831912882294140200198651272972656974659 90859003300314000511707422045608592763579537571859 54298838958709229238491006703034124620545784566413 6645406842143612930176940208 46391065875914794251435144458199

    What are then chances this can be factored?
    Quite high, though it'll take some time.

    Quoting Wikipedia:

    The factorisation of RSA-250 utilised approximately 2700 CPU core-years, using a 2.1GHz Intel Xeon Gold 6130 CPU as a reference
    And given that RSA-240 took 900 CPU core-years (a factor of 1/3), I estimate it'll take about 8100 CPU core-years to factor RSA-260, so perhaps it will be factored some time in the next few years.

  8. #8

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How many lines have you ever written in a day?
    By Poincare in forum General Discussions
    Replies: 13
    Last Post: 08-11-2010, 05:32 PM
  2. Your biggest project: how many lines have you written?
    By happyclown in forum A Brief History of Cprogramming.com
    Replies: 44
    Last Post: 04-29-2009, 07:30 AM
  3. Replies: 1
    Last Post: 07-12-2005, 09:03 PM
  4. Strings are written on same line
    By Roaring_Tiger in forum Windows Programming
    Replies: 5
    Last Post: 05-05-2003, 01:07 PM
  5. Pls comment some lines for me.
    By Nutshell in forum C Programming
    Replies: 0
    Last Post: 01-07-2002, 08:51 PM

Tags for this Thread