Thread: Peasants' Algorithm for multiplication

  1. #1
    Registered User
    Join Date
    Jan 2020
    Posts
    8

    Cool Peasants' Algorithm for multiplication

    Q.7 Consider the Peasants' Algorithm for multiplication of two positive integers. It
    works in the following manner:
    • Write the two numbers in two columns. Keep updating according to the
    following procedure until the number in the first (i.e., left) column becomes 1.
    • Halve the number in the first column (integer division), double the number in
    the second column.
    • In the end, sum up all the numbers in the second column, for which, the
    corresponding number in the first column is odd.
    Example of multiplication using Peasants’ algorithm: 13 x 8:
    13 8 ←
    6 16
    3 32 ←
    1 64 ←
    Answer: 64 + 32 + 8 = 104
    Write a function to calculate multiplication using the Peasants’
    algorithm.

    Code:
    
    #include<stdio.h>
    int main()
    {
        int n1,n2,c1,c2,sum=0;
        printf("Enter two numbers: ");
        scanf("%d%d",&n1,&n2);
        c1=n1;c2=n2;
        while(c1>=1)
        {
            if(c1%2==1)
                sum+=c2;
            c1/=2;c2*=2;
        }
        printf("The product of %d and %d is %d",n1,n2,sum);
    }
    Do go through this, And thank you for constructive criticism.

  2. #2
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    277
    Quote Originally Posted by A.B.D View Post
    Q.7 Consider the Peasants' Algorithm for multiplication of two positive integers. It
    works in the following manner:
    • Write the two numbers in two columns. Keep updating according to the
    following procedure until the number in the first (i.e., left) column becomes 1.
    • Halve the number in the first column (integer division), double the number in
    the second column.
    • In the end, sum up all the numbers in the second column, for which, the
    corresponding number in the first column is odd.
    Example of multiplication using Peasants’ algorithm: 13 x 8:
    13 8 ←
    6 16
    3 32 ←
    1 64 ←
    Answer: 64 + 32 + 8 = 104
    Write a function to calculate multiplication using the Peasants’
    algorithm.

    Code:
    
    #include<stdio.h>
    int main()
    {
        int n1,n2,c1,c2,sum=0;
        printf("Enter two numbers: ");
        scanf("%d%d",&n1,&n2);
        c1=n1;c2=n2;
        while(c1>=1)
        {
            if(c1%2==1)
                sum+=c2;
            c1/=2;c2*=2;
        }
        printf("The product of %d and %d is %d",n1,n2,sum);
    }
    Do go through this, And thank you for constructive criticism.
    Formatting's not great, but it does look pretty kosher. You could however do things a bit more efficiently:

    Code:
    #include<stdio.h>
    int main()
    {
        int n1,n2,c1,c2,sum=0;
        printf("Enter two numbers: ");
        scanf("%d%d",&n1,&n2);
        c1=n1;c2=n2;
        while(c1>=1)
        {
            if(c1&1)
                sum+=c2;
            c1>>=1;c2<<=1;
        }
        printf("The product of %d and %d is %d\n",n1,n2,sum);
    }

  3. #3
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308
    Lmao:d:d
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

  4. #4
    Registered User
    Join Date
    Apr 2020
    Location
    Greater Philadelphia
    Posts
    26
    This peasant's algorithm is interesting. If one must repeatedly multiply numbers by a single known multiplier such as 40, it's at least tempting to use addition and shifting rather than actual multiplication in hopes of efficiency. Doing so involves no ifs, ands (or buts..). This algorithm is a kind of generalization of the same idea, but it comes at the cost of comparison and branching, which must make it slower in practice than a CPU's multiplication instruction. Could it be that the peasant's algorithm is how the multiplication instruction works in the hardware itself?

  5. #5
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    277
    Quote Originally Posted by Alogon View Post
    This peasant's algorithm is interesting. If I must repeatedly multiply numbers by a single known multiplier such as 40, I am at least tempted to use addition and shifting rather than actual multiplication in hopes of efficiency. Doing so involves no ifs, ands (or buts..). This algorithm is a kind of generalization of the same idea, but it comes at the cost of comparison and branching, which must would make it slower in practice than a CPU's multiplication instruction. Could it be that the peasant's algorithm is how the multiplication instruction works in the hardware itself?
    I have no idea, but I'm guessing most likely not. Even though the peasant's algorithm is basically equivalent to long multiplication, the latter is easier to implement. Older CPU's used that method almost exclusively. Modern chips tend to use more advanced approaches, and hence overall performance has improved dramatically.

  6. #6
    Registered User
    Join Date
    Jan 2020
    Posts
    8
    Quote Originally Posted by Sir Galahad View Post
    Formatting's not great, but it does look pretty kosher. You could however do things a bit more efficiently:

    Code:
    #include<stdio.h>
    int main()
    {
        int n1,n2,c1,c2,sum=0;
        printf("Enter two numbers: ");
        scanf("%d%d",&n1,&n2);
        c1=n1;c2=n2;
        while(c1>=1)
        {
            if(c1&1)
                sum+=c2;
            c1>>=1;c2<<=1;
        }
        printf("The product of %d and %d is %d\n",n1,n2,sum);
    }


    Thank you Brother.
    But I didn't get your Second code.
    Can u explain it.

  7. #7
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308
    Galahad's added only one code. The other code is part of his signature.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

  8. #8
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    277
    Quote Originally Posted by A.B.D View Post
    Thank you Brother.
    But I didn't get your Second code.
    Can u explain it.
    It's just a simple demonstration of the connection between the "coprimality" of all the integers and the mathematical constant "pi" (ie: 3.1415926[...]). It works even better if you're sampling from a truly random source. (Only reason I opted for rand() was to keep things as platform-neutral as possible.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-02-2018, 03:08 AM
  2. Replies: 5
    Last Post: 03-26-2014, 07:41 PM
  3. MxN Matrix Multiplication with Strassen algorithm
    By vishnudath in forum C Programming
    Replies: 0
    Last Post: 03-06-2014, 06:01 AM
  4. Replies: 2
    Last Post: 02-18-2010, 11:17 AM
  5. Replies: 1
    Last Post: 10-15-2006, 05:17 AM

Tags for this Thread